Are you looking to share users and login sessions between two or more WordPress websites? If so, you can learn how to do it step-by-step by following this guide.
Recently, I found myself in a situation where I needed to add a bbPress and BuddyPress “section” to a website (we’ll call this site B), although the main website (site A) and site B were supposed to have different WordPress installs. Lucky for me, both websites were hosted on the same server so this made it the whole scenario easier.
After I finished it and got it working, I decided to write an article on it to prevent you from some of the headaches I got while doing this. Also, the process could be extended to as many websites as you need. In my case, I was only going to need to synchronize login details and cookies between two WordPress installs.
I’ve recently discovered this plugin, which seems to do the same thing and without all the work required in my tutorial. I now recommend giving WP Remote Users Sync a try instead.
Requirements/Scenario:
- Site A and Site B must be on the same top level domain (subfolders or subdomains)
- Site A and Site B will be using the same Database
Probably obvious, but you will also need FTP access to both websites, we will be opening and editing wp-config.php for both websites. If you meet the requirements, we can move on.
(Optional) Take a full website Backup
Since we’ll be working with wp-config.php and the databases, it’s best to be safe and run a full website backup, or at least the database and wp-config.php. For this I recommend using Updraft Plus or WP Time Capsule, but just about any other solution will work. I recommend them because I’ve used them many times and never failed me.
Share Users and User Meta
In order to have the same login and password on both websites, we’ll be using the same database tables. If you’re using the default prefix, these should be called wp_users and wp_usermeta.
1. Check Site A’s table prefix
If you don’t know for sure your site’s table prefix, open Site A‘s wp-config.php and look for this line:
$table_prefix = 'wp_';
Code language: PHP (php)
In most situations, it will be wp_, but if yours is different just remember it because we’ll be using it for Site B‘s wp-config.php too. Keep wp-config.php open, we’ll be needing more info later, as well as to edit it.
2. Install WordPress for Site B
Keep in mind we’ll be using the same database as Site A, so you will have to use a different table prefix for Site B. In my example, I decided to use com_, short from Community. You can use whatever you see like, it doesn’t matter.
If you’re using tools for automated WordPress installation that do not allow you to select the database and prefix, don’t worry about it as you can change them later from wp-config. Just make sure you have WordPress installed in a Subfolder or Subdomain.
Open Site B‘s wp-config.php, and replace database name, user, and password to be the same as Site A.
define('DB_NAME', 'siteAdatabase');
define('DB_USER', 'siteAuser');
define('DB_PASSWORD', 'siteApassword');
Code language: JavaScript (javascript)
Now both sites will be using the same Database. Make sure Site B uses a different table prefix.
$table_prefix = 'com_';
3. Share WordPress Users between the websites
To do this, all we need to do is to make sure Site B is using the same tables for users and usermeta as Site A. Open Site B‘s wp-config.php and add the following lines right before
/* That’s all, stop editing! Happy blogging. */
define( 'CUSTOM_USER_TABLE', 'wp_users' );
define( 'CUSTOM_USER_META_TABLE', 'wp_usermeta' );
/* That's all, stop editing! Happy blogging. */
Code language: JavaScript (javascript)
Please note that wp_ is Site A‘s table prefix. If yours is different you need update these lines.
Great job, both sites are now sharing the same users (if you did everything right).
Share Login Cookies
Now that both websites are using the same login details, you will notice that if you switch websites you will have to log in again. That can be fixed by sharing login cookies, here’s how to do it.
1. Edit Site A’s wp-config.php
Right before /* That’s all, stop editing! Happy blogging. */, add the following lines:
define( 'COOKIE_DOMAIN', 'yourwebsite.com' );
define( 'COOKIEHASH', md5( 'https://yourwebsite.com' ) );
/* That's all, stop editing! Happy blogging. */
Code language: JavaScript (javascript)
In the first line, you will have to replace yourwebsite.com with your actual domain. In the second line, you have to put your WordPress Site Address (URL), which you can get from Dashboard -> Settings -> General
Don’t close Site A‘s wp-config.php yet, we’ll still need some info for Site B.
2. Edit Site B’s wp-config.php
Similar to how we did for the database name, user and password, we will now copy the following lines from Site A and replace those existing in Site B:
define('AUTH_KEY', 'stringof40characters');
define('SECURE_AUTH_KEY', 'stringof40characters');
define('LOGGED_IN_KEY', 'stringof40characters');
define('NONCE_KEY', 'stringof40characters');
define('AUTH_SALT', 'stringof40characters');
define('SECURE_AUTH_SALT', 'stringof40characters');
define('LOGGED_IN_SALT', 'stringof40characters');
define('NONCE_SALT', 'stringof40characters');
Code language: JavaScript (javascript)
Obviously, you won’t actually find anything in there names stringof40characters, but some different random strings of 40 characters.
Next, we need to make sure Site B is sharing login cookies with Site A.
Similar to the previous step we will now edit Site B‘s wp-config.php and add the same lines we added for Site A.
define( 'COOKIE_DOMAIN', 'yourwebsite.com' );
define( 'COOKIEHASH', md5( 'https://yourwebsite.com' ) );
/* That's all, stop editing! Happy blogging. */
Code language: JavaScript (javascript)
Get Administrator rights on your second site (Site B)
After you’ve done the steps above, you will notice that your websites are indeed synchronizing login sessions, but you are not allowed to access the admin dashboard.
You will be greeted with this error message: Sorry, you are not allowed to access this page.
To gain access to it, we’ll need to manually edit wp_usermeta database table. There’s plenty of options to do this, most of you will have phpMyAdmin installed on your server. Personally, I’m using Adminer (a.k.a. phpMinAdmin) because I rarely ever edit the database and I don’t need it permanently installed.
Next, open your Database and go to the wp_usermeta table. If you’re using Adminer too, you’ll have to click Select data to find out wp_capabilities meta_key.
Copy the meta_value from wp_capabilities of your admin user, create a New item and set:
- umeta_id (leave it blank, it will auto increment)
- user_id: 1 (for most of you, 1 will be user_id of the administrator on Site A)
- meta_key: com_capabilities (com_ is the prefix for Site B. Replace it with your prefix)
- meta_value: Copy-paste the same meta_value from wp_capabilities for user_id = 1
When done, it should look like this.
And now you’re done! Your websites are sharing users, logins and you’ve given yourself Administrator
user role on site B too.
all coding done then when i login in example.com/shop/wp-admin its say’s Sorry, you are not allowed to access this page. help me
Hi Yash,
You are right, I forgot to add the last part about giving admin privilleges to your account on the second website.
Give me about 30 minutes, I’ll update this article. It’s a pretty quick fix, you will need PHPMyAdmin or something similar to edit the database.
I’ve just updated to article, thanks for letting me know I forgot about that part.
When I perform this on my two sites I get the white screen of death. Luckily I had a backup so I could revert, but do you have any idea where I might have gone wrong? I followed everything step by step.
Not sure, but I would start by disabling plugins on both websites.
Just rename the plugins folder for each website and try again.
Wouldn’t it be easier to achive the same result using a multisite installation?
It would, but if your website is not part of a multisite network already, then you still have to go through the extra work of migrating it.
In my case, I thought it will be easier to just sync login sessions between different installs than migrating the website to a multisite network. Plus, it’s more interesting :)
Quick question! when you log into site A are not supposed to be logged into Site B automatically?
If you are logged into any of your sites, you should also be logged in on the other ones too.
If it did not work for you, then you might have skipped a step (or made a mistake in it), most likely the login cookies.
Thank you so much for sharing this tips, I have tried the tips from others but it does not work at all, the others tips is just wasting time but after I followed your step and it is work like a Charm.
my recommendation to others, please just follow the step on this page so you are not wasting your times trying other tips, please follow “carefully” the step:
Get Administrator rights on your Site B
The main issue is with the admin: “sorry, you are not allowed to access this page.”
I install Adminer and follow the step and all done.
Thank you.
I’m glad you managed to get it working. Cheers!
Hi there, thank you for this!
However I still have a problem and the logged in users are not being shared between the 2 websites.
Question – On Site B config.php, shall we have :
define( ‘COOKIE_DOMAIN’, ‘SITEByourwebsite.com’ );
or define( ‘COOKIE_DOMAIN’, ‘SITEAyourwebsite.com’ ); ???
Both (or all, if you use it on more than 2 websites) should have the same COOKIE_DOMAIN.
So you can just copy-paste these 2 lines from Site A’s wp-config.php to Site B’s wp-config.php.
I hope this makes it clear.
Hey,
Thank’s for the tutorial.
If you want create administrators whitout change capabilities on phpmyadmin, use this hook.
add_action(‘user_register’, ‘user_capabilities’, 10, 1);
add_action(‘profile_update’, ‘user_capabilities’, 10, 2);
function user_capabilities($user_id) {
$user_role = $_POST[‘role’];
$permissions = array($user_role => 1);
//MAIN
update_user_meta($user_id, ‘wp_capabilities’, $permissions);
//COM
update_user_meta($user_id, ‘com_capabilities’, $permissions);
}
Regard’s
Great article Marius.
Quick question.
I’m using the multisite setup…do you know of a way to share the logins and auto login the user? Ultimately keeping the same roles?
I’ve tried a couple plugins but none really transfer the buddypress and bbpress content. Please advise, thanks.
Kendell.
That was such an excellent tutorial. It saved me a lot of time and resources. Thank you very much :)
I was hovering between https://kinsta.com/blog/share-logins-wordpress/ and this post to make sure everything went well. Luckily, it wasn’t that complex!
Describing the process to sync User Roles would have made this a complete solution (Automatically Duplicate Caps and Levels with a Function https://kinsta.com/blog/share-logins-wordpress/)
Thanks!
Great article, and much shorter than others I found.
I have a question though, just to make sure. Are role capabilities and levels also shared on both site?
I’m using Paid Membership Pro in both sites, would this still work?
Thanks .
Good Morning.
I make the configuration in the indicated files, and add the record in the database. but I still get the error “sorry, you are not allowed to access this page.”
If you can help me please
Good Morning.
I make the configuration in the indicated files, and add the record in the database. but I still get the error “sorry, you are not allowed to access this page.”
If you can help me, please.
Great tutorial. Unfortunately this seem to work perfectly for fresh installs but I need to accomplish the same with 3 existing sites (the main site and two on subdomains) that using the same wp table prefix. Any tips on how to tackle this? Thanks!
So 3 different databases but they all have the same prefix correct? If so, for the 2 subdomain databases change the prefix to something else, there should be a find and replace sql code to make the transition quick. Make sure you also update the prefix in the WP config. Check to make sure sites are still working fine. Afterwards merge those two databases with the main domain database. Now you have one big database with the 3 prefixes and you should be able to follow the tutorial to get the results you need.
Thank for your trick.
But I still stuck when I logged in site A and then I opened site B’s home, It’s showed Admin bar. But when I clicked to open dashboard. I refresh me to login page. What’s problem?
my Site A’s address site is “www.mysite.com” (wp is in http://www.mysite.com/wp)
my SiteB’s address site is “www.mysite.com/class”
Hi guys, i tried to do that but this is the result : Error establishing a database connection .
When i change DB_Name on site B to DB Name of site A, thats the error.
Anyone here can explain what i have to do.
Thank you :)
Thanks for the article.
Will it not work on local host?
After I am defining the 2 lines of cookie parameters login is not happening.it is redirecting to same login page.If i remove the cookie lines from config file then it is working.
Hi, I followed every detail, but when I implement it, it prompt me to create a new wp site. When I installed, it happens that I able to access the dashboard but all the things I did from the previous site is gone. How can I able to transfer those? The Themes and Plugins that I used in site B? Thanks.
Hi there – I find your tutorial really intresting, and makes a lot of logical sense. However, I must admit, that because we have to change configurations and table pre-fixes – it can feel intimidating. Making one mistake can spell doom.
You mentioned trying wp-remote-users-sync – which I was happy to try. But it doesnt work as expected.
> If user logs into one site, they are not necessary logged into the other site.
> If user changes password on one site, it’s not immediately changed on the other site.
> I can not get it to work seamlessly
I am considering trying the approach suggested in your post – sharing a database.. and having the same user and user-meta table between both. Makes more sense- and leaves little room for poor experiences between sites.
Questions:
1. Obviously I will require the same user details on both sites? If I install ACF on one site, and add additional user fields – it will affect the other site ? SO I would need ACF on both sites?
2. If a user signs in on Site A – he is free to jump between Site A and Site B without the need for login repeatedly ?
3. if both sites A and B have woo commerce installed – How will it affect purchases on Site A and Site B ? Where will he see his orders ?
4. Can user purchase items from SiteA and SiteB simultaneously ? or will it have to be separate purchases ?
Hi Manesh,
I’m sorry, but I haven’t used this setup in a long time and my scenario was a lot simpler than use. Unfortunately, I don’t have an useful answer to your questions.