2

I'm a beginner for WordPress. I have a database app written in PHP and MySQL and I need to convert it to WordPress and also I what to post the database rows info by shortcode were id="x" like:

[plugin id="1"]

It is a small database. Just give me a small demo code or link plz. I need it and help me to learn WordPress.

1 Answer 1

2

For the sake of the example, I'll assume you to have copied the relevant Table to the WordPress DB, to have named it wp_my_xtra_table and it to contain 3 columns (id,num_val,str_val).

function wpse106832_shortcode_demo( $atts ) {
    global $wpdb;

    extract( shortcode_atts( array(
        'id' => 1
    ), $atts ) );

    $table_row = $wpdb->get_row(
        'SELECT * ' .
        'FROM ' . $wpdb->prefix . 'my_xtra_table ' .
        'WHERE id = ' . $id, ARRAY_A
    );

    if ( ! empty( $table_row ) ) {
        return '<table><tr>' .
                 '<th>ID</th>' .
                 '<th>Numeric Value</th>' .
                 '<th>String Value</th>' .
             '</tr>' .
             '<tr>' .
                 '<td>' . $table_row['id'] . '</td>' .
                 '<td>' . $table_row['num_val'] . '</td>' .
                 '<td>' . $table_row['str_val'] . '</td>' .
             '</tr></table>';
    } else {
        return '<p>Nothing found.</p>';
    }
}
add_shortcode( 'wpse106832', 'wpse106832_shortcode_demo' );

With the above, [wpse106832 id=3] will display your third database entry.

Related Reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.