2

I was being creative and put this in one of the files loaded with my plugin. Are there implications with storing the table names this way?

global $wpdb;

if(!defined('DB_ARTISTS'))
    define('DB_ARTISTS', $wpdb->prefix . "artists");

if(!defined('DB_RELEASES'))
    define('DB_RELEASES', $wpdb->prefix . "releases");

EDIT: I had originally not included all of the information I had meant to. Tables and variables are prefixed with ABC ( ABC_DB_ARTISTS & $wpdb->prefix . 'abc_artists' ).

1
  • It will have issues on multisite.
    – gmazzap
    Jun 10, 2014 at 1:27

1 Answer 1

2

The DB_ constant prefix in WordPress is generally considered reserved for DB_NAME, DB_HOST, DB_USER and DB_PASS. Using it for plugin-specific constants is, in my opinion, not a great idea. The only implication it might pose is if other plugins try to use the constants, but that's purely theoretical.

The proper way to do this is to store the table names in the WPDB object stored in the global $wpdb.

global $wpdb;

if ( ! isset( $wpdb->myplugin_artists ) && ! isset( $wpdb->myplugin_releases ) ) {
    $wpdb->myplugin_artists = $wpdb->prefix . 'myplugin_artists';
    $wpdb->myplugin_releases = $wpdb->prefix . 'myplugin_releases';
}

It's important to use a proper prefix (in this case myplugin_ for your plugin). For example, for Advanced Custom Fields, this is usually acf_ (more on prefixing).

4
  • I removed my prefixes when posting this. I shouldn't have, clearly. I prefixed the DB_ARTISTS constant as well. Using your method above, I would still need to call global $wpdb every time I wanted to access a table name, so I don't know how much it makes, if anything easier. Jun 10, 2014 at 17:34
  • It doesn't make anything any easier, I'm not trying to make your life easier :-). I'm answering your question, "Are there implications with storing the table names this way?" :-).
    – engelen
    Jun 10, 2014 at 18:29
  • to summarize then, there aren't any, but there could be, so I shouldn't do it. Thanks :) Jun 10, 2014 at 18:37
  • Exactly and no problem!
    – engelen
    Jun 10, 2014 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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