2

I've manually setup a new table in the wp-db database. Where do I add the name of this table in the wpdb class or anywhere else?

I've noticed that WordPress uses queries like: $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" ); where the actual table name isn't used.

If I were to do: $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->friends" ); it will not work.

I also tried adding the name like var $friends; inside the wpdb class, but that would not work.

2
  • Please show us how you added the table $friends to $wpdb. Probably that's your problem.
    – kaiser
    Oct 3, 2013 at 16:09
  • I added it manually to the db, since I only need to select some stuff from it and display. That's why I asked where I could simply add it's name, so I could use it like $wpdb->myTable. I'm just beginning with this thing.
    – Norman
    Oct 3, 2013 at 16:18

3 Answers 3

3

$wpdb->tables[] = 'friends'; is the basic code you're looking for.

Please note that you'll want to test for the existence of the table in the list so you don't wind up with 100 copies of the table in the tables array, especially if there's a chance your code could be repeated within a page load.

UPDATED with example:

add_action( 'wp_loaded', 'add_table' );

function add_table() {
    global $wpdb;
    if ( ! in_array( 'friends', $wpdb->tables ) ) {
         $wpdb->tables[] = 'friends';
    }
}
3
  • 1
    Please wrap that in the proper hook.
    – kaiser
    Oct 3, 2013 at 16:09
  • Good call, @kaiser - updated.
    – murdaugh
    Oct 3, 2013 at 16:52
  • If your table is global on multisite, you can use $wpdb->global_tables instead. (Please note that both of these are marked with @access private though.)
    – J.D.
    Apr 2, 2014 at 19:36
1

Eugene Manuilov answer is a good solution. However if you want use the $wpdb->table_name syntax you can

add_action('after_setup_theme', 'add_table_name');

function add_table_name() {
  global $wpdb;
  $wpdb->friends = $wpdb->prefix . 'friends';
}
2
  • @murdaugh's answer is better, because the prefix for the table will get updated properly when using switch_to_blog() on multisite. Whereas if you use this method, you will still be using the old table prefix, meaning that your code won't actually switch to the other blog. It is stuck on the original blog.
    – J.D.
    Apr 2, 2014 at 19:39
  • 2
    @J.D. to be multisite compatible just add add_action( 'switch_blog', 'add_table_name' );
    – gmazzap
    Apr 2, 2014 at 19:47
1

Do not modify your wpdb class at all! It's a bad habit. Next time when WordPress update it's version you will loose all your changes.

Instead of modifying wpdb class, create a table name variable (or constant) withing your plugin and use it when you need it.

For instance, in your myplugin.php file add following:

global $wpdb;
define( 'MYPLUGIN_TABLE_FRIENDS', $wpdb->prefix . 'friends' );

And then in your queries use it like this:

$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM " . MYPLUGIN_TABLE_FRIENDS );

UPDATED:

Ok. Fine. But just for my knowledge, since I'm new to all this, how does WordPress do it, and where are they stored?

Those tables are set when $wpdb->set_prefix() method is called. Take a look at this method and you focus on 673-685 lines.

5
  • Ok. Fine. But just for my knowledge, since I'm new to all this, how does WordPress do it, and where are they stored?
    – Norman
    Oct 3, 2013 at 15:18
  • 1
    I have updated my answer. Oct 3, 2013 at 15:25
  • +1 but IMO no need to define (another) constant. Should be enough to simply use "{$wpdb->prefix}friends"... imo
    – kaiser
    Oct 3, 2013 at 16:10
  • @kaiser it's just and example, like i said: create a table name variable (or constant) Oct 3, 2013 at 16:21
  • 1
    One reason not to use a constant is that your code will not switch over to another blog when using switch_to_blog() on multisite. Your table name will still be defined with the old prefix.
    – J.D.
    Apr 2, 2014 at 19:43

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.