Share users database on different WordPress installs

Share Users Database in WordPress

It’s pretty useful to be able to share the same users database over two WordPress installations. This method is used by many important websites to keep sessions opened between different websites, domains or sub-domains. Here is a simple way to share users database:

Share users database to create a great user experience

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.

With this code you’re asking the second website to use the first website users table. Isn’t it great?

Now we are facing a small problem. Cookies… Yes, 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', '.yoursite.com'); //replace with the 1st website url
define('COOKIEPATH', '/');

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

53 responses to “Share users database on different WordPress installs”

  1. jeremy buller

    I’ve wondered recently whether this was possible! Thanks for sharing.

    I have one WP installation at a particular domain, and a second at one of its subdomains (like – domain.com and assets.domain.com). When I tried to implement this in assets.domain.com’s wp-config.php, I started getting the message ‘You do not have sufficient permissions to access this page.’ for all users at domain.com.

    Undoing these definitions doesn’t seem to be helping. Have you seen this before, by any chance?

    1. Hi, this might happen, if this case, simply create a new account as administrator, this should work.

      1. jeremy buller

        All good, Rémi. Thanks!

    2. rickky

      Hi Rémi,

      After 4-5 hours search I found your site. I hope your code will help me. Right now I am trying it in my localhost but its not working for me.. I am using woocommerce and one event plugin in my wordpress websites. I have 2 website on server. But right now I am testing your code in my localhost. I installed 2 wordpress different folders name under localhost. Both have same woocommerce and event plugin. Now I want if I update in my first website then my second website will be show that update on page. I want to do this with my event plugin If I update my event on my first website then it will update on the second website automatically.

      I tried your code but get nothing. Can you please give me little time to solve my issue.

      I use this code under my config file
      define(‘CUSTOM_USER_TABLE’, ‘wp_users’);
      define(‘CUSTOM_USER_META_TABLE’, ‘wp_usermeta’);

      Now I can’t login under my second website admin. I update content in my first website but nothing update on same page of my second website. Please let me know where I am doing mistake. Thanks

      Rickky

  2. Aaron

    Is it possible to share users between a site with a standard wordpress install and a site with a multisite install? Would this cause any issues?

    1. Actually when you’re having a multisite install, all sub-sites are using the same SQL table for the users, so it shouldn’t be a problem.

  3. Rıfat

    i think something wrong with this.

    define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’);

    must be;

    define(‘CUSTOM_USER_META_TABLE’, ‘wp_usermeta’);

    it worked for me like this.

    But i have another problem. When i try to login to 2nd wordpress. Nothing is happening. Its just refreshing the page and thats all.

    In my situation i have some users on my 1st WP installation. Then i connect it to the second one.

    Any suggestion?

    1. Rıfat

      Fixed it.

      Its because of the cookie thing. That didnt worked for me. When i delete the lines:

      define(‘COOKIE_DOMAIN’, ‘.yoursite.com’); //replace with the 1st website url

      define(‘COOKIEPATH’, ‘/’);

      It just worked.

      Anyway, thanks.

  4. Ken

    How about when using a plug in like Pippins Restrict content will the user DB sync on both installs to restrict the pages correctly?

    1. i’m not sur it would work, that’s a pretty good question, i think we have to test to know!

    2. manoj

      how can i use it with wordpress install in two databases
      i need to use same user DB
      how can i archive it

      1. Can you please clarify? i’m not sure i understand

        1. manoj

          suppose i have two WordPress install on different databases, which are in the same server. how can i archive user sharing for both sites….
          if some registers to site A then he would be also registered to site B automatically.

  5. VK Jain

    I don’t see this anywhere in my wp-config.php file
    define(‘COOKIE_DOMAIN’, ‘.yoursite.com’); //replace with the 1st website url
    define(‘COOKIEPATH’, ‘/’);

    Where should I find this to change the COOKIE_DOMAIN?

  6. Kriss

    Hi Remi, I really could do with some help.

    I current have two wordpress servers, with separate IP’s (so the server are not linked so far).

    I would love to have both wordpress installs share the same user information. How can I do this, as the server aren’t even connected?

    Is it similar to your instructions above, but somewhere I need to add an IP or URL of the server?

    Any help would be extremely appreciated.

    1. The two websites HAVE TO use the same users table. If your servers allow distant connections that shouldn’t be a problem, but unfortunately i’m not a server expert!

  7. Morten

    Hey,

    Tried adding and changing, but when I go to second site it just opens a blank page.

    So where am I to add the cooking thing, and how should the site url be shown?

    Hope you can help.

    Br,
    Morten

    1. What do you get if you enable debug mode?

  8. Rob

    Hi Remi,

    This is just the information I need – but I can’t make it work – unless I’m misunderstanding.

    What I’d like to do is be able to log into my primary domain – mysite.com and from there click on a link that will bring me to the subdomain – XYZ.mysite.com – and be automatically logged into the subdomain.

    In other words, I want my users to be able to just log in once – on either domain – and be able to go back and forth between the domain and the sub-domain without having to login more than once.

    I do have things configured right now so that the users of my domain can log into my subdomain – but they have to log into each one individually. Is this the best I can get?

    I’ve tried, I think, every combination possible – even copied the keys/salt/auth/nonce from my domain’s config and tried it in the subdomain.

    Right now I just the “wp_users” and the “wp_usermeta” lines and my users can log into each domain individually. The “cookies” lines didn’t seem to have any impact.

    Any ideas?

    Thank you very much!
    Rob

    1. Hey Rob, i guess it’s a matter of cookies/sessions that needs to be fixed, nothing more. If you create two cookies/sessions (one for each site) when a user logs in from one domain, you should be fine.

  9. Rob

    Hi Remi,

    Thanks for the quick response….so it can be done – that’s good to know.

    Can you tell me how do I do that? :)

    Thank you again!
    Rob

  10. no idea right now! ;-)

  11. Rob

    Cool – now I don’t feel so stupid ;)

    I’ll muddle through it and post if I can figure it out!

    Thanks Remi!!

  12. vajrasar

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

    I din’t get the gist of “using the same database”? Can you give some example?

    We have xyz.com and xyz.com/abc (as subdomain). on xyz.com we post news and on xyz.com/abc we do reviews. Both are separate wordpress installs with their own databases. How can I use your awesome solution with this setup?

    1. What i meant is that the two websites need to share the users tables, so the two databases have to be on the same server. I confess it could have been clearer.

      1. vajrasar

        Thank you for the reply. So if I have

        xyz.com’s db as – wp_xyz

        and xyz.com/abc’s db as – wp1_abc

        then can I make it work with this technique?

  13. cardened

    Hi Remi.

    Thanks for sharing. Something is not clear for me though.

    I have two wp installs on the same domain.
    i.e.
    WP Install 1: xyz.com
    WP Install 2: xyz.com/members

    define(‘COOKIE_DOMAIN’, ‘.yoursite.com’);

    should be like this:
    define(‘COOKIE_DOMAIN’, ‘.xyz.com’);

    Is that correct?

    And SECOND:
    You have this:

    >>define(‘CUSTOM_USER_TABLE’, ‘wp_users’);
    define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’);

    Make sure you replace “wp_” by the first website table prefix. < WP Database Name: _wrdp1
    WP Install 2: xyz.com/members –> WP Database Name: _wrdp2

    Where or how should I change this?

    And THIRD:
    How can I make sure I’ve done the changes correctly? When I get on the second site and look at USERS, should I see all the users as in the first wp install?

    Thank you very much for your kind support!

    Best,
    Eduardo

  14. cardened

    Hi Remi!

    Thanks for this post. I managed to do everything that you mentioned, BUT I am having the following problem.

    It is not passing the “MEMBERSHIP LEVEL” that I setup on the main blog. How can I do this change now that I’ve already installed the site and wp-config.php is already configured?

    Thanks.
    Best,
    Eduardo

  15. pkapila

    Hello Remi,

    I had followed you steps defined above, and am facing a problem with only how to set cookie for the second site to validate.

    this code is not working for me can you please help me out…

    define(‘COOKIE_DOMAIN’, ‘.yoursite.com’); //replace with the 1st website url
    define(‘COOKIEPATH’, ‘/’);

    for example my first site is at http://xyz.com and the second site is at http://xyz.com/directory

    the second site uses the user table of the first site but is not logges in the user if the is already logged in the first site.

    Thanks Prashant

  16. chocco

    Hi How Can i Share User profile Images After Sharing tables …

    1. profile images are not stored locally if you use gravatar

  17. chocco

    i am using userpro… .. Can U Suggest Me How can i Share user profile images between Multiple sites..

  18. shareef

    Hi Remi,
    I tried all these steps you mentioned here except cookies things and i succeed for two of my sub-domains running WP. Now i can login using same username and password for admin and front of both sub-domains. But i am facing a problem that is i have to login separately for the front of both sites.
    What is need is to login once only on any site and remained login for second site when go to that..There is some problem in cookies but no idea where it is?
    Any help praised??

  19. iMJohanes

    Hi Remi,

    Could you teach me how to do it if my main site database isn’t same with the subdomain database. But they’re both in the same server.

    Thx

    1. what do you mean by “not the same”?

      1. I’m using Hostinger as my hosting provider.
        Each of them have their own database.
        How to make [sub.mysite.com’s users and usermeta] be the same as [mysite.com’s users and usermeta]?

  20. AnDr3yy

    Hi there,

    I need some help. I try your method, I put the 4 lines of code in wp-config.php, but I can’t login with any user on my second wordpress site.

    Ai have this message every time:

    You do not have sufficient permissions to access this page.

  21. estiner

    Okay, I think I’m so close on this one, but I can’t quite get over the hump. I have installed both WordPress sites using the same database and changed the $table_prefix value in site B to a different value. Everything is working grand. But…when I then add the two define function lines given above (my first site’s $table_prefix value is wp_) into site B’s wp-config file and I refresh my site, it no longer lets me log into my WordPress dashboard for site B. The site appears fine, but its like my user credentials no longer work. Any thoughts on this?

  22. marieneige

    Hi I wonder how that works really because to share a database I think you have to run a multisite but if running a multisite then there is no place to add a special prefix in wp-config.php for each of the multisite. I run a multisite, would this work ? if not, how ? thank you

    1. Nop, multisite isn’t needed for this ;-)

      1. marieneige

        oh but when I switch from a subfolder site to another the cookies are not shared, so the cart and wishlist content are not synchronized. If I isert this code : define('COOKIEPATH', '/'); I have trouble login-in.
        I would like also to share users on two sister-sites wich don’t share the same database, one is old and one is going to be launched soon but I already made a lot of databases in it and.. they have the same table prefix. How I can share the users? What would happen if in my new site on define('DB_USER', 'database_name'); I would insert the database name of my old site? thank you so much!

  23. Sanjeevan

    Hi

    I am in the process of setting up a online store (woocommerce), with a lot of other additional features such as blog, business directory (business directory plugin), classified (AWPC plugin), event listings (Events Manager Plugin) & Forum (bbpress plugin)

    I realize that by having all this my site is a bit slow. I was thinking of splitting the site into multiples installs, such as blog.xyz.com, forum.xyz.com, classifieds.xyz.com etc.. and keeping the woocommerce shop on the main url xyz.com

    What is your advice ?

    I should be able to use one user registration across all installations. and once logged in the user should be able to visit any of the sub domains, and it should show as logged in..

    Are there better ways to do this ?

    Or should i stick to one install and work on improving they site speed using other methods such as caching etc..

    Appreciate your reply.

    Thanks
    Sanjeevan

  24. […] a search i stumbled upon this here!, was wondering could this be used for my problem and if anyone know the list of tables i would […]

  25. […] a search i stumbled upon this here!, was wondering could this be used for my problem and if anyone know the list of tables i would […]

  26. Gaurav

    Hi All

    I have 2 WordPress sites in 2 different sub-domains like test1.abc.com and test2.abc.com . Both sites have wp-require plugin activated and only logged-in users can see the site. I want to make a system where if a user logged into one site, he should be auto-login into the other one.

    What i try :

    After some searching I know I need to use one database for both sites. So I have done these steps:

    I have download the whole database of test2.abc.com site and change all prefix wp_ to wpmo_, replaced it in whole database and upload it into first site’s database. I added these 2 lines in wp-config.php of the second site, to define that second site should use first site’s user table not its own.

    define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’);
    define(‘CUSTOM_USER_TABLE’, ‘wp_users’);
    Now, the second site is using the first site’s users and I am able to login to the second site by the user details of first site.

    The next problem is cookies, so I added these lines in wp-config of both sites.

    define(‘COOKIE_DOMAIN’, ‘.abc.com’);
    define(‘COOKIEPATH’, ‘/’);
    define(‘COOKIEHASH’, ‘aee53c017c29dc0d3ae37253fc8cbfd8’);
    Now I logged in into test1.abc.com and when I go to test2.abc.com , it asks me to login. That means cookies are not passing from the first site to the second one. However, i tried to be print $_COOKIE and it gives me same encrypt values, but user still not auto login on second site. also when i came back to first site , it automatic logout. i feel like both sites are related somehow on cookies and i am close but still not reached to my goal of auto login into second site.

    Any help?

  27. Intervik

    Hi Remi
    I try to get a question-mark about this topic to be straighten out, all over Google and forums.

    In many certainstances myself and other people looking for a way to make an excisting wordpress installation using a shared user table as a slave to another “master” install, on diffrent domains. All are saying just like your post here on your site.

    But, after changing the slave database connection, the excisting slave installation wanna re-install, and after that, the install is a new blank wordpress from scratch – with sharing usertables.

    This means (?): You CAN NOT connect an old install – it will be NEW anyway. And then, in many cases, it is better to make a clean salve install from the beginning – to make rid of the fuzz to re-name the table prefix on the slave installs and more…

    Im I correct or am I missing something obvious here?

    Thanks for a nice knowledge-bank you got here…

    / Jonas, Intervik

  28. askindia1996

    Hello Remi,

    I am facing issues with login/logout session sharing. However, users are able to login manually after i pasted define(‘CUSTOM_USER_TABLE’, ‘wp_users’); and define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’); in my second website’s Wp-Config file, but the session sharing is not working. Could anyone please help me ?

  29. digosw

    I’ve tried to do that “define(‘CUSTOM_USER_TABLE’, ‘wp_users’); and define(‘CUSTOM_USERMETA_TABLE’, ‘wp_usermeta’)” on the second web site… But it didn’t work.

    I have to do something more?

    (sorry for the bad english… i’m getting rusty… :P)

  30. Mark Ransome

    Hi Remi,

    I am reading all this with great interest as I have a slightly different situation. I have a stand alone script on my main website (https://www.example.com) and my WordPress site in the directory blog (https://www.example.com/blog), can I still synchronize the user profiles, just the user names and passwords?

    Kind regards,

    Mark Ransome

  31. Cyrill

    Hi there,

    thank you for the explanation. Its quite a smart solution. We tried it but on the subdomain the cookie always adds a . in front of the domain when logging in on the subdomain.

    We are trying now to create a second cookie upon login but this means we have to tweak wordpress core files where the changes might be gone. Do you have any example script how to do that and how best to deal with future updates of wordpress versions?

Leave a Reply