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. We 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?
Solution :
After some help from Mikk3lRo and others, i have managed to solve this issue. i am posting the solution for anyone who faces the same problem.
here you can find step by step guide for this :
step 1: use one database for both installations, install 2 wp by using 2 different prefix on installation time.
step 2: Make sure that the randomly generated secret keys and salts are also identical in both wp-config.php files.
step 3: paste these 2 lines in wp-config.php of second site.
//Share user tables
define('CUSTOM_USER_META_TABLE', 'SITE1_PREFIX_usermeta');
define('CUSTOM_USER_TABLE', 'SITE1_PREFIX_users');
step 4: share cookies with these lines . (write in both wp-config.php)
//Share cookies
define('COOKIE_DOMAIN', '.abc.com');
define('COOKIEHASH', 'aee53c017c29dc0d3ae37253fc8cbfd8');
step 5: now you will be able to auto login in second site when logged into first site. but your will an error message on second site “you do not have permission to access this page”, which is a good thing.
step 6: The reason is, WordPress checks the user capability (wp-includes/capabilities.php) so either you have directly add this capability in database (in case you only have few users) or to write a plugin for this. @Mikk3lRo writes a plugin for this in comments, which is Good.
Thanks
Alright – you are very close, but there are a few more things to be done.
All requirements are as follows:
wp1_
,wp2_
and so on.wp1_users
andwp1_usermeta
tables – you’ve done this – and actually you would have overcome this obstacle if only you had spelled the constants name correctly… it’sCUSTOM_USER_META_TABLE
(one more underscore than what you have)COOKIE_DOMAIN
andCOOKIEHASH
– you’ve done thisprefix_capabilities
entry for each user for each site in the sharedusermeta
table – I don’t think you’ve done this, simply because you haven’t yet reached the point where you realize it’s necessary.Complete solution:
This goes in
wp-config.php
:This is enough to get you logged in on both sites – but there’s still that last annoying bullet left on the list.
The problem is that your permissions (“capabilities”) are only good on one of the sites because the
meta_key
is prefixed with the table prefix of the site. If you google around a bit you will find lots of solutions recommending to modifywp-includes/capabilities.php
to just use a common prefix instead – I strongly recommend against that! (not for security reasons, but because you will need to make this patch / hack after every update… and it’s just insanely bad practice to modify core files)Instead to remedy this obstacle you need to duplicate the
wp1_capabilities
row in thewp1_usermeta
table (for each user!), giving it a newumeta_id
and substituting the table prefixwp1_
withwp2_
in themeta_key
column. You need to do this for each site, so you have one row withmeta_key
wp1_capabilities
, one withwp2_capabilities
and so on.If you and a friend of yours are the only users who’ll ever log in to the sites, then just do it by hand through phpMyAdmin or something – if you need it to work dynamically, then it should be quite possible to automate with a small plugin (see edit below).
I’ve always hated this design – a table prefix has no business inside a table row! I think it is needed for multisite installs, though I’m sure there would be other (better) ways to solve it…
Update: Plugin to keep user roles synchronized between all sites
This simple plugin will duplicate and keep the required rows in the
usermeta
table updated when users are created or edited.One thing worth noting is that it probably won’t work with multisite installs because they have some special capabilities / roles. I have not tested this.
It may need refinement for specific use cases (please do comment), but it does the job fine for my limited test case that only includes a few users. It will be inefficient for a site with thousands of users, but as it only runs when a user is modified, and only does updates if they are needed I doubt this will be a major concern. However it should be relatively easy to adapt to only read and modify the user that was just added / edited. It would complicate initial setup a bit though, as pre-existing users would not automatically get duplicated on the first run.
Create the folder
wp-content/plugins/duplicate-caps
and inside put the following induplicate-caps.php
– and don’t forget to activate underplugins
in wordpress admin. It needs to be installed on all sites.I believe the easiest solution for you would be to make use of one of WordPress’ single-sign on (SSO) plugins.
There’s a large amount of plugins listed here.
You could use one of them or base your authentication on one of them.
Alternately there’s multi-site which will let you create a network of sites, if you decide to create a multi-site, then please read this first.
I think define cookies is not enough to login to wp site.
So my solution is, Create a plugin to restrict access to those who don’t login to one of the site.
Set a cookie when user login to one of the site.
when user view the site. check the cookie value, and redirect or return to the site.
Sample Code:
Main drawback with this plugin is poor security. anyone create this cookie manually. so its high security one, don’t use this. try to hash the cookie as possible so it can’t easily created manually.
( if you want full code for the plugin ill put somewhere )
Update (full plugin):
go to your wordpress installation plugins directory ({{wp install folder}}->wp-content->plugins and make a new file with extension .php {{file-restrict.php}} copy and paste this code
do this for both sites
go to wordpress plugins and activate the plugin. (both sites)
once you login to one of the site you can view either of site. and once you logout one site it gives a message with a link to login to the site.
(after you activate just logout and login to the site)
i’ll email the code for you too.