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 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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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

global $wpdb;
$previousPrefix=$wpdb->prefix;
$wpdb->prefix="mainsiteprefix_";
//Database access goes here
$wpdb->prefix=$previousPrefix;

or you can create a new wpdb object like this:

$wpdb2 = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
share|improve this answer
    
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

 
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.