Multiple Wordpress Sites Share Login

by Oscar

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.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

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.

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

17 comments

Helmar 8th February 2019 - 9:06 am

“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
Nazmul Ahsan 8th December 2018 - 7:33 pm

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
Tejas 1st April 2016 - 8:37 am

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
Toon Gerritsen 1st January 2016 - 8:33 pm

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
Ishan 19th February 2016 - 10:43 am

Hello Toon,

Did you find any solution for this ?

Thanks

Reply
Kyle 22nd April 2015 - 9:06 pm

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
Martin 27th January 2016 - 6:04 pm

Hi Oscar , should i add it to first or second wordpress installation in wp-includes?

Reply
Dito Veloce 1st April 2015 - 8:38 pm

After hours of surfing I arrived to your page, thanks god, all worked fine at first try. THANKS !

Reply
avinash 29th December 2014 - 8:36 am

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
mmonroe 2nd December 2014 - 11:00 am

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
Ben Potter 9th November 2014 - 3:04 pm

I’m using WordPress 4.0, and there is no $this->cap_key = $wpdb->prefix . ‘capabilities’; line.

Reply
Lee Churn 3rd June 2014 - 1:27 pm

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
Kalle Pedersen 28th May 2014 - 1:39 pm

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
Manos 8th February 2014 - 8:25 pm

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
Shannan 19th December 2013 - 9:11 pm

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
vajrasar 23rd November 2013 - 8:03 pm

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
Oscar 23rd November 2013 - 8:24 pm

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