Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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