WordPress Shortcode show database row

admin2025-06-02  0

Good day,

I was wondering if can do a shortcode that gets all the fields in a row stored in a database, with the difference that I do not want to do many shortcodes for the speed of the site.

The row of the database with separate fields, as you will see in the image below.

Or if there is another way to do it and it is better, you're welcome.

Good day,

I was wondering if can do a shortcode that gets all the fields in a row stored in a database, with the difference that I do not want to do many shortcodes for the speed of the site.

The row of the database with separate fields, as you will see in the image below.

Or if there is another way to do it and it is better, you're welcome.

Share Improve this question edited Mar 3, 2019 at 6:04 Sebastián asked Mar 3, 2019 at 5:59 SebastiánSebastián 1113 bronze badges 2
  • Could you explain what do you mean by “like an array or something like that”? – Krzysiek Dróżdż Commented Mar 3, 2019 at 6:03
  • Sorry, I'm wrong, I've been thinking many things with this problem – Sebastián Commented Mar 3, 2019 at 6:05
Add a comment  | 

1 Answer 1

Reset to default 1

I’m not entirely sure if that’s what you wanted, but... Here’s the code that will register a shortcode, which will get given row and output all the fields of it:

function shortcode_db_row_cb( $atts ) {
    $atts = shortcode_atts( array(
        'id' => false,
    ), $atts, 'db_row' );

    global $wpdb;
    $row = $wpdb->get_row( $wpdb->prepare( "select * from {$wpdb->prefix}TABLE_NAME where id = %d", $atts['id'] ), ARRAY_A );
    $res = '';

    if ( $row ) {
        $res = '<table>';
        $res .= '<tr>';
        foreach ( $row as $k => $v ) $res .= '<th>' . esc_html($k) . '</th>';
        $res .= '</tr>';
        $res .= '<tr>';
        foreach ( $row as $k => $v ) $res .= '<to>' . esc_html($v) . '</td>';
        $res .= '</tr>';
        $res .= '</table>';
        return $res;
    }

    return 'no such row';
}
add_shortcode( 'db_row', 'shortcode_db_row_cb' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748846581a314215.html

最新回复(0)