For my WordPress site, I would like to echo the value of a column in a row (determined by the user's ID) of a table in the database for my site. This table was created by a plugin so it is not accessible through the user meta. I have to try and get the value from the table created by the plugin.

For this example, the value is located in the "plugintable" table in the database and the column is "columnname". How get I get the value based on matching the user's ID with the "ID" row?

I've gotten here from the help of other online sources, but I'm stuck.

    global $wpdb, $table_prefix;
    $user_ID = get_current_user_id();
    $value = $wpdb->get_var('SELECT columnname FROM '.$table_prefix.'plugintable WHERE ID = '.$user_ID);

    echo $value;

1 Answer 1


OK, so first of all, you should always use proper escaping, when building the queries.

This is how your code can look after fixing:

global $wpdb;
$value = $wpdb->get_var( $wpdb->prepare(
    " SELECT column_name FROM {$wpdb->prefix}plugin_table WHERE ID = %d ",
) );

The things you have to take care of:

  1. You have to change column_name to proper column name you want to get.
  2. You have to change plugin_table to proper table name you want to get the value from.
  3. You have to change ID to proper name of column that stores the user id in given table.
  • I tried this, and replaced the values as instructed, but I don't see anything using echo $value; any ideas why?
    – Steven
    Jan 31, 2019 at 19:39
  • @Steven so you’re replacing something wrong... It’s hard to say anything else - I don’t know what’s the column names or table name, so I’m unable to construct final version of that query... Jan 31, 2019 at 19:40
  • so this should be working if the table is wp_plugin_details, the column is plugin_user_email, and the id column name is user_id code global $wpdb; $value = $wpdb->get_var( $wpdb->prepare( " SELECT plugin_user_email FROM {$wpdb->prefix}wp_plugin_details WHERE user_id = %d ", get_current_user_id() ) ); echo $value; code (Apologies for not knowing how to do the code box in comments!)
    – Steven
    Jan 31, 2019 at 19:45
  • It should be {$wpdb->prefix}plugin_details. Rest look OK Jan 31, 2019 at 19:49
  • That's it! I didn't know I had to remove the "wp-". Thank you very much!
    – Steven
    Jan 31, 2019 at 19:54

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.