I have multiple wordpress websites that uses the same files and database but with different table prefixes, I use a plugin "Shortcode Exec PHP" and I would like to use the same shortcodes on all the sites, but this plugin doesn't use it's own table, it used the update_option function from wp to store the data.

So I tried just adding this on top of the plugin's code:

$wpdb->prefix = "mainsiteprefix_";

But I get the error:

You do not have sufficient permissions to access this page.

On all the admin pages of the sites that don't uses that prefix and have the plugin activated.

So, how could I make the plugin to use any db prefix I want without having permission problems?


You will need to restore the database prefix after using it , with something like this

global $wpdb;
//Database access goes here

or you can create a new wpdb object like this:

$wpdb2 = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
  • I tried changing the prefix on the class constructor and restoring it on the plugin class destructor but still got the same problem, I guess I will have to change/restore it inside every function on the plugin, cause I think your suggestion should work so I will keep trying, thanks. – todgil Oct 5 '11 at 14:02
  • It didn't work, I am changing prefix with 'global $wpdb; $wpdb->prefix="mainsiteprefix_";' right before running get_options() to get the shortcodes from the db, and they are always from the current site db, changing the prefix doesn't affect it. – todgil Oct 5 '11 at 14:31
  • I solved it, the problem was that wp was getting the options from wp_load_alloptions() and that was cached, so I had to define WP_INSTALLING while retrieving the options. Also wpdb->prefix didn't affected wpdb->options so I had to set ->options instead of ->prefix. – todgil Oct 5 '11 at 15:05
  • executing wp_cache_flush(); before retrieving the options seems to be a better solution than defining WP_INSTALLING since a constant cant be undefined later. – todgil Oct 5 '11 at 15:12

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.