store custom WP table names in a global variable

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 Answer
1

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).

Leave a Comment