Sharing users across multiple WordPress installations

Featured Image

After reading Justin Tadlock's article Installing two WordPress blogs with the same users I have decided to implement this method in two of my test installations. Well... it did not work. Why?

Justin, in his article stated that the following two lines in wp-config.php of the blog using prefix_users and prefix_usersmeta from another blog, will be enough:

define('CUSTOM_USER_TABLE', 'prefix_users');
define('CUSTOM_USER_META_TABLE', 'prefix_usermeta');

After adding these two defines to wp-config.php in the root of second blog I ran the installation. Everything went smoothly but when I tried to access Dashboard of second blog (login was successful) I got the message: "You do not have sufficient permissions to access this page.”. In WordPress terminology permission means capability. Obviously, neither administrator from first blog nor administrator from second blog had proper capabilities to run second blog's Dashboard.

I did some digging into this problem. As a result I found this link in WordPress Support Forum: WordPress 2.5 Share users table. To make this long story shorter I've put a complete solution below:

COMPLETE SOLUTION:

In this example we will use two WordPress blogs:

  • A
  • B

Both will use the same database "ab". The table prefixes are:

  • a_
  • b_

In wp-config.php of blog B we will add four defines:

define('CUSTOM_USER_TABLE', 'a_users');
define('CUSTOM_USER_META_TABLE', 'a_usermeta');
define('CUSTOM_CAPABILITIES_PREFIX', 'a_');

define('OTHER_BLOGS_PREFIXES','a_');

just after line:

define ('WPLANG', '');

Now we need to modify /wp-includes/capabilities.php in function _init_caps in blog B. Line:

$this->cap_key = $wpdb->prefix . 'capabilities';

should be replaced by:

      if (defined ('CUSTOM_CAPABILITIES_PREFIX')) {
            $this->cap_key = CUSTOM_CAPABILITIES_PREFIX . 'capabilities';
      }
      else {
            $this->cap_key = $wpdb->prefix . 'capabilities';
      }

In function update_user_level_from_caps after line:

update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );

add:

if (defined ('OTHER_BLOGS_PREFIXES')) {
        $a = split( ",", OTHER_BLOGS_PREFIXES );
        foreach ($a as $value) {
                update_usermeta( $this->ID, trim($value).'user_level', $this->user_level );
        }
        unset( $value );
}

That's all. Now both blogs share the users tables of blog A.

My next article Login once to access multiple WordPress installations will show how to login into any blog which shares user tables and be logged in into all of them.

2 thoughts on “Sharing users across multiple WordPress installations”

  1. For WP 4.3.1
    $wpdb->prefix must be $wpdb->get_blog_prefix() CMIW

    But it still “You do not have sufficient permissions to access this page.”
    Any solution?

Leave a Reply

Links are not allowed in comment field!

Your email address will not be published. Required fields are marked *