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 extending the $wpdb object with properties for my custom tables.

function setup_wpdb()
{
    global $wpdb;

    // Create properties for custom tables
    $wpdb->my_plugin_table1 = $wpdb->prefix.'my_plugin_table1';
    $wpdb->my_plugin_table2 = $wpdb->prefix.'my_plugin_table2';
}

Is this a good practice? I guess as long as the properties are prefixed with the plugin name, it should be fine. Defining a constant may work too. What do you prefer and why? Thanks.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

As long as...

  1. You use $wpdb->prefix (or $wpdb->base_prefix) to prefix the table
  2. Your table name is someplace public, and easily available for other plugins, themes and end users to use and extend

I would say either setting the table as a property on $wpdb or using a constant is fine. If you choose to use $wpdb be aware that another plugin (or theme or whatever) can just overwrite your property in the same way you set it. For that reason, it's a bit more "safe" to use something like a constant.

Also, always carefully question whether or not you need to add a table.

I tend to use constants, but if I'm doing something like creating a termmeta table, then I would use a $wpdb property.

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.