Multiple WordPress Sites Share Login

I have setup multiple wordpress sites installation recently on the same domain name, and I thought it would be awesome to have these wordpress sites to share the user logins, which means users only need to register and login once on one site and it should work on all these wordpress sites.

This turned out to be very easy, I found the original detail on this page sharing user tables. This method is used by many important websites to keep sessions opened between different websites, domains or sub-domains.

How to Share user logins with Multiple Wordress Sites Installations

Let’s say you have an existing WordPress installation, and you want to share the existing users database with a second website.

First of all make sure you are using the same WordPress version on the two websites and make sure your two websites are using the same database. To be able to have the two websites using the same database, you have to use different table prefix for each install.

To change the default WordPress table prefix, you have two choice. You can do it during the installation or editing the wp-config.php file where you must change $table_prefix value.

Once the second website has been installed, add these two lines in wp-config.php file, just before this line:

/* That's all, stop editing! Happy blogging. */

Add these two lines:

define('CUSTOM_USER_TABLE', 'wp_users');
define('CUSTOM_USERMETA_TABLE', 'wp_usermeta');

Make sure you replace “wp_” by the first website table prefix.

Don’t have sufficient Permissions Error on Profile page

With this code you’re asking the second website to use the first website users table.But you are likely to get this error:

You do not have sufficient permissions to access this page.

In your second WordPress Installation, “wp-config.php”, under “define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’); “add:

define('CUSTOM_CAPABILITIES_PREFIX', 'wp_');

Then you have to find this line in “wp-includes/capabilities.php”

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

and replace it with

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

That’ s all!

How to stay logged on all sites? Answer is Cookies!

To avoid having login on every site, we need to set all the wordpress sites to use the same cookies. At the moment each website has its own cookies parameters, so your two websites have to use the same cookies parameters. So, still in wp-config.php of the second website, change the COOKIE_DOMAIN with the url of the first website:

define('COOKIE_DOMAIN', '.domain.com'); //replace with your domain name (the first blog)
define('COOKIEPATH', '/');

And now your’re done, now logged users will have their sessions opened accross your two websites.

17 thoughts on “Multiple WordPress Sites Share Login

  1. Helmar

    “define(‘COOKIE_DOMAIN’, ‘.domain.com’); //replace with your domain name (the first blog)
    define(‘COOKIEPATH’, ‘/’);”

    If I do this, I can’t even log in on the 2nd site anymore. It simply loops the login form. So in order to prevent this, I used the details of the 2nd domain here, but I now have another issue that crops up every now and then, and that is that my user_capabilities are being reset on the first domain, forcing me to manually update them in order for my customers to access their products (which are based on said capabilities/roles). Any idea

    Reply
  2. Nazmul Ahsan

    You can now do this with a plugin wordpress.org/plugins/share-logins/ It’ll automatically share logins accross multiple sites. It’s not mandatory for the sites to be on the same database or domain. No coding required.

    Disclaimer: I’m the author of the plugin.

    Reply
  3. Tejas

    Hello Everyone,

    I am on WordPress 4.4.2, I have multiple wordpress installations (Not Multisite)

    1st Installation :- www. example.com
    2nd installation :- www. example.com/blog/

    I have pasted this in my Wp-Config file of 2nd Installation :-
    define(‘CUSTOM_USER_TABLE’, ‘wp_users’);
    define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’);
    define(‘COOKIE_DOMAIN’, ‘.example.com’);
    define(‘COOKIEPATH’, ‘/’);

    Problem :- The issue is that after pasting above code in 2nd WordPress Installation’s Wp-Config file, login table is shared, that means users are manually able to login. However, when we sign in to first installation the session is not shared on to the second installation. I feel there is some problem with the cookies. Could anyone please help me ?

    Regards,
    Tejas

    Reply
  4. Toon Gerritsen

    Hi Oscar,

    with the upgrade to WordPress 4.4 the capabilities.php file has been changed significantly. There is no longer the possibility to modify the line
    $this->cap_key = $wpdb->prefix . ‘capabilities’;
    because it no longer figures in that file.
    Your solution, that worked so well, seems no longer possible; or do you know of an alternative solution for WordPress 4.4?

    Kind regards,
    Toon

    Reply
  5. Kyle

    I found a solution to the permission error that does not require you to modify WordPress core files or manually update the database. Just add the following code to your functions.php file.

    `add_action( ‘user_register’, ‘duplicate_user_info’, 10, 1 );
    function duplicate_user_info( $user_id ) {
    $user_info = get_userdata($user_id);
    $user_cap = $user_info->wp_capabilities;
    $user_lvl = $user_info->wp_user_level;
    add_user_meta( $user_id, ‘PREFIX_capabilities’, $user_cap ); // Use site B’s prefix instead of PREFIX_
    add_user_meta( $user_id, ‘PREFIX_user_level’, $user_lvl ); // Use site B’s prefix instead of PREFIX_
    }`

    Reply
  6. avinash

    Hello,
    i using user synchronization plugin for transferring the wp-user and wp-user-meta data on all websites database table , i used same pre fix for all database , i add this code :-

    define(‘COOKIE_DOMAIN’, ‘.masterwbsite.com’); //replace with your domain name (the first master website)
    define(‘COOKIEPATH’, ‘/’);

    but after adding this code in my second website login is not working and come the error you cookies is disable .
    please help me.

    with thanks and regards

    Reply
  7. mmonroe

    I too am using WP 4.0.1 and there is no place in wp-includes/capabilities.php to edit to fix the issue. Will test without this update and see if I get error.

    Reply
  8. Lee Churn

    Hi,

    I’m using WordPress 3.9.1.

    I found
    “$this->cap_key = $wpdb->prefix . ‘capabilities’;”

    in this part

    function _init_caps( $cap_key = ” ) {
    global $wpdb;

    if ( empty($cap_key) )
    $this->cap_key = $wpdb->get_blog_prefix() . ‘capabilities’;
    else
    $this->cap_key = $cap_key;

    $this->caps = get_user_meta( $this->ID, $this->cap_key, true );

    if ( ! is_array( $this->caps ) )
    $this->caps = array();

    $this->get_role_caps();
    }

    what should I do here?

    Reply
  9. Kalle Pedersen

    Hi Oscar,

    thanks for the nice tutorial. I can aaaaalmost get it to work!

    That means: can’t get the shared cookies to work :-(

    I’ve added the

    “define(‘COOKIE_DOMAIN’, ‘.domain.com’); //replace with your domain name (the first blog)
    define(‘COOKIEPATH’, ‘/’);”

    ..but it doesn’t work. What gives? :-(

    Reply
  10. Manos

    Hello, I tried to use the code you gave for cookies but unfortunately I couldn’t make it work. I got an installation on the root folder and the other wordpress inside another folder on the root. I used this code:

    define(‘COOKIE_DOMAIN’, ‘.domain.com’); //replace with your domain name (the first blog)
    define(‘COOKIEPATH’, ‘/’);

    Is this correct?

    Ps. Keep up the good work! Really nice tutorials!!

    Reply
  11. Shannan

    Hey There. I discovered your blog the usage of msn. This is a really
    neatly written article. I will be sure to bookmark it
    and come back to read extra of your useful information. Thank you for the
    post. I will certainly comeback.

    Reply
  12. vajrasar

    Can you please explain this in detail – ” your two websites are using the same database.?”

    What do you mean by ‘using the same database”? Can you please give some example.

    Thanks a ton.

    Reply
    1. Oscar

      when creating a wordpress website, you need to create a database to hold all the “tables”. Usually if you have more than one wordpress sites, you will have to create an extra database to hold the tables.

      But if you want to share the logins from one wordpress site with the other, you will have to share the same table (the user table), and that’s why you need to use the same database.

      Got it?

      Reply

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

Are you Robot? *

For prompt technical support, please use our forum IntoFPV.com. I check blog comments weekly.