Home » WordPress » How to share users and logins between WordPress websites
How to share WordPress Users and Login Cookies between multiple websites

How to share users and logins between WordPress websites

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.

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.

Sharing 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_';

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. Installing 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');

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. */

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).

Sharing 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. */

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');

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. */

Get Administrator rights on your 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 this 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 role on site B too.

26 thoughts on “How to share users and logins between WordPress websites”

    1. 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.

  1. 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.

    1. 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 :)

    1. 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.

  2. 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.

  3. 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’ ); ???

    1. 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.

  4. 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

  5. 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.

  6. 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!

  7. Raúl Maguiña

    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 .

  8. Jonnathan Berbesi

    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

  9. Jonnathan Berbesi

    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.

  10. 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!

    1. 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.

  11. Chonlatit Prateepmanovong

    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”

  12. 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 :)

  13. 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.

  14. 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.

Leave a Comment

Scroll to Top