Our goal is to set up two WordPress websites which will share logins and the same users. Once a user has subscribed one website, she would be able to access the other website with the same role and capabilities.
To achieve this goal, we should be able to edit WordPress configuration file and update database tables. A general understanding of WordPress architecture and database structure is essential, as well as a basic knowledge of WordPress development. No worries if you’re not a pro. Just follow this post directives and ask your questions in the comments.
Before we start coding, we need to know where WordPress user roles and capabilities are stored. So, our first step is to dive deep into database tables.
Important: The following will not work out of the box in the Kinsta environment due to the fact that we only allow one installation of WordPress for each site (unless you’re running WordPress multisite). It may be possible to get this working on our platform, but it would require some additional setup or development. We recommend discussing this with a WordPress developer.
- User Data and Metadata
- Defining Custom User Tables – Share Logins
- Installing WordPress
- Roles and Capabilities
- Automatically Duplicate Caps and Levels with a Function
User Data and Metadata
By default, WordPress stores user-related data into three tables: {$pref}options
, {$pref}users
and {$pref}usermeta
.
- The
{$pref}options
table stores the full list of available roles and capabilities in a row whose option_key field is{$pref}user_roles
. - The
{$pref}users
table stores basic user data, like login, password, email, url, etc. - The
{$pref}usermeta
table stores user metadata.
When working on new WordPress installations, we don’t have to care about {$pref}user_roles
row in {$pref}options
table, because the corresponding option_value field has always the same value. We should consider this row just in case we’re working on existing installations where roles or capabilities have been changed.
No worries about {$pref}users
table, as well, because it stores basic user data that we won’t change when sharing users between websites.
The {$pref}usermeta
table is the only table we’re going to update to achieve our goal.
{$pref}usermeta
stores user metadata in key/value pairs. In this table five rows store the data that we have to consider.
The first row has the meta_key field set to {$pref}capabilities
, and the corresponding meta_value field is a serialized array holding the user role. The second row stores the user level (note that user levels are deprecated from WordPress 3.0). The remaining three rows concern dashboard settings we won’t dive into in this post.
User role, level and settings are specific to the WordPress installation and are identified by the same $pref
value. It is an important bit of information when our goal is to share users between websites, because we will have to duplicate these rows and change the meta_key
field accordingly.
That’s all we have to know about user tables when we aim to share logins and users between new WordPress installations. When working on existing websites, we should consider that many plugins add extra rows to {$pref}usermeta
, and we may be required to have a deeper look at database tables.
That being said about user tables, we can move a step forward. Now we have to define two specific constants into wp-config.php file.
Defining Custom User Tables – Share Logins
WordPress allows us to set custom tables instead of {$pref}users
and {$pref}usermeta
. This means that if two (or more) WordPress websites share one database, we can set the same users and usermeta tables for all of them. As a consequence, all websites which share these table will share the same users.
We just need to define CUSTOM_USER_TABLE
and CUSTOM_USER_META_TABLE
into wp-config.php file, as shown in the following code:
// custom users and usermeta tables
define( 'CUSTOM_USER_TABLE', 'my_users_table' );
define( 'CUSTOM_USER_META_TABLE', 'my_usermeta_table' );
Now that we know what has to be done, it’s time to run our two WordPress installations.
Installing WordPress
For convenience, I will name the WordPress root folders first and second. first_
and second_
will be the respective table prefixes.
Now let’s run the first installation.
When the first WordPress website is up and running, we can edit its configuration file. Open /first/wp-config.php and add the following lines above the ‘stop editing’ comment:
$table_prefix = 'first_';
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
// custom users and usermeta tables
define( 'CUSTOM_USER_TABLE', $table_prefix . 'users' );
define( 'CUSTOM_USER_META_TABLE', $table_prefix . 'usermeta' );
/* That's all, stop editing! Happy blogging. */
We have enabled debug mode forcing WordPress to store error notices and warnings into debug.log file (read more about this topic in An in-depth view on how to configure WordPress).
Then, we have defined CUSTOM_USER_TABLE
and CUSTOM_USER_META_TABLE
constants to first_users
and first_usermeta
tables. This way we’re not changing WordPress default settings.
We are done with the first installation. Next we have to copy wp-config.php from the first installation folder and paste it into the root folder of the second installation. Be careful to change the $table_prefix value accordingly:
$table_prefix = 'second_';
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
// custom users and usermeta tables
define( 'CUSTOM_USER_TABLE', 'first_users' );
define( 'CUSTOM_USER_META_TABLE', 'first_usermeta' );
CUSTOM_USER_TABLE
and CUSTOM_USER_META_TABLE
are set to the first installation’s values: first_users
and first_usermeta
. That’s all for the first installation.
When running the second installation, we should set a non-existent email address for admin user as WordPress finds a number of existing users from first_users
table.
Log into the second installation admin panel as admin and list WordPress users. You’ll find the new admin user and all users from the first website (this allows them to share logins). At this point, users from one site won’t be able to log into the other website.
To grant users the same capabilities in both websites, we have to update {$pref}usermeta
table.
Roles and Capabilities
If you are running new WordPress installations, you have not to care about {$pref}options
table. You just need to update {$pref}usermeta
table.
In our example, when a new user is created in the first website, WordPress adds first_capabilities
and first_user_level
rows in first_usermeta table
. To give access to the second website, these rows should be duplicated, as shown in the image below:
When a new user is created in the second website, second_capabilities
and second_user_level
rows will be added to first_usermeta
table.
In order to give the same roles and caps to users across websites, first_capabilities
and first_user_level
rows should be duplicated in second_capabilities
and second_user_level
. With these two pairs of rows in the same first_usermeta
table, users would be able to access both websites with the same privileges.
To update all existing usermeta rows you may run a SQL query or update tables from phpMyAdmin. But what for users that will subscribe our websites from now on? According to WordPress Codex, we’d use a plugin or build a custom function.
And there we go!
Automatically Duplicate Caps and Levels with a Function
set_user_role
is an action hook which triggers anytime a new user is created or an existing user’s role has been edited. Thanks to this action, we can automate usermeta table updates.
So, in the main file of a plugin add the following function:
function ksu_save_role( $user_id, $role ) {
// Site 1
// Change value if needed
$prefix_1 = 'first_';
// Site 2 prefix
// Change value if needed
$prefix_2 = 'second_';
$caps = get_user_meta( $user_id, $prefix_1 . 'capabilities', true );
$level = get_user_meta( $user_id, $prefix_1 . 'user_level', true );
if ( $caps ){
update_user_meta( $user_id, $prefix_2 . 'capabilities', $caps );
}
if ( $level ){
update_user_meta( $user_id, $prefix_2 . 'user_level', $level );
}
}
add_action( 'set_user_role', 'ksu_save_role', 10, 2 );
The callback function keeps three arguments, two of which are required: $user_id
and $role
.
What the function does is quite self-explanatory. get_user_meta returns the specified user meta field value. We have called this function twice to retrieve first_capabilities
and first_user_level
fields. Then we have used these values to add second_capabilities
and second_user_level
fields to first_usermeta
table.
Upload ad activate this plugin into the first website.
To make installations work symmetrically, we just need to upload and activate the plugin in any installation, but setting the right values to prefixes. For instance, if we would activate this feature in the second website, we just have to declare the variables as follows:
$prefix_1 = 'second_';
$prefix_2 = 'first_';
So, edit and install the plugin into the second website and create a new user or change an existing user role. Then check the first website. The user roles will be exactly the same as the second website.
Summary
In this post, I’ve explained how to grant the same privileges to users across independent WordPress installations. Once registered into a website, the user will be able to access all websites sharing the same users and usermeta tables.
I’ve supposed to work with new installations. If you’re working on existing websites, you should consider that some plugins could have updated usermeta table, or even created new tables storing user related data. In this case, a more accurate analysis of the database would be appropriate.
If you have any questions about how to share logins in WordPress, or you’d like to share your experience with us, feel free to join the conversation posting your comments.
The full code of our plugin is available in this public Gist
Hi Carlo. Thanks for a very good explanation of what goes on. I am trying to get two sites using the same users and this has helped greatly to understand the process. I’ve followed your example and now need the abilty to sign in on one site and be automatically logged-in on the other. Have you taken this step?
Hi Steve. Actually, two independent installations could share user tables but they do not share the same login session. I’ve not delved into this aspect of the topic, but I suppose a multisite installation would be the best option to share login sessions.
If your websites reside on the same domain, you could try to define the same cookie-path for both websites as suggested here: http://wordpress.stackexchange.com/a/131403/10012
Hey Steve, in case you didn’t test this already, I just did it and for the same domain it works by sharing cookie paths and domain keys and salts. I wrote a brief guide here if you need help: https://trickspanda.com/share-users-login-wordpress/
I had same problem and used plugin called User Session Synchronizer I had multisite network so had some issues I told him and he instantly replied to me in about a day and solved my problem and become friends
( setup is as simple as copy and paste website domain or subdomain in a box )
How can we do this but on different databases? Unfortunately on high-end sites like Pantheon sites so not share a database.
Thanks,
Dan
Hi Daniel. I think there is no easy way to share users and usermeta tables between installs that do not share the same database. But you have other options to achieve your goal. The first and easiest is to create independent site sections based on custom post types or categories.
Alternatively you may opt for a multisite + BuddyPress install where subsites share users thanks to BuddyPress. This is a more complicated solution and you should consider that the user management would be more complex than it is in a standard install.
Here is a good post that could help you decide: https://halfelf.org/2011/dont-use-wordpress-multisite/
Thanks for the tips! I’m weary of using multisite because the main website gets about 1 million uniques a month, which will surely overpower the smaller sites.
I dreamt a solution last night that we’re gonna try. Essentially using the wpmu user sync plugin to sync basic user info across multiple sites while also using the rest API to handle editing custom fields made with ACF across servers. It’s a heck of a work around but I think this will work.
It sounds good. I’m curious about the way you will handle custom fields via REST API. Keep us updated!
I can login but can’t access admin.
Hii Carlo, thanks for helping I have 5-6 multisite and I want to share user across all multisite networks is it possible? If yes can you please tell me what changes I need to work it properly ( I don’t have 5-6 subsite in multisite network I have 5-6 multisite network )
Hi Maharshi. Actually, this plugin has been developed with independent installations in mind. It forces installations to share custom users and usermeta tables. That’s not required in WordPress multisite, as it uses the same tables (see Multisite table overview). Unfortunately, the administrator of a network has to manually enable each registered user to any site of the network and this could be unfriendly when you have to deal with many users and many sites.
But luckily, a number of plugins allow to automatically share users among the sites of a network. Have a look at the following free plugins:
https://wordpress.org/plugins/join-my-multisite/
https://wordpress.org/plugins/multisite-user-management/
https://premium.wpmudev.org/project/add-existing-users/
https://premium.wpmudev.org/project/add-new-users/
Hey Carlo I don’t have five sites in one multisite network I have five-six multisite network with too many sites in it
So, your question is “How to share users between multiple WP networks?”. Actually, it’s a complex topic for a comment, and it would require a much deeper view. But I’ve found this plugin that could be of help: https://github.com/stuttter/wp-multi-network
I checked this but for my case it’s not working. I need something like you posted in this post I use multisite and it use one table as single WordPress site uses. if changing some code will make it working ? I already synced accounts across all network only issue I have is I can’t sync role if you can help me in that .. thank you for helping I followed this tutorial to sync account
Tutorial: http://mikemclin.net/single-users-table-multiple-wordpress-sites/
Thank you for a terrific and useful article!
What should I do if I DON’T want users to have the same privileges across sites? Eg, an editor on site A should not automatically be an editor on site B. Can I simply skip syncing caps and levels, or will that create problems?
Thank you for any advice!
Hi. It’s a complex task. You can filter user capabilities through the user_has_cap filter, but I think this would require a lot of coding and testing. Instead, I would opt for a multisite install, which allows to easily admin user roles in subsite admin panels
I know that I am extremely late to the table but in case anyone else comes here wondering the same as Ariel: I have just followed the above guide but skipped adding the code that automatically duplicates caps and levels. What I ended up with was multiple sites that shares users but I can define what role each user should have on each site. Which is exactly what I wanted and what i think is what Ariel is asking about as well.
The only thing I had to do for every site was to manually copy the row for the user meta “first_capabilities” for one of the first sites administrators and rename it to “second_capabilties” in order to have at least one administrator for the second site. I can then log in to the second site where all other users are marked as “No role for this site” and assign roles that can be totally different from their roles in the first site.
Do note that this was for brand new sites but I guess it would be possible even if you were merging multiple existing sites users.
Also: thank you to Carlo for the guide tha made my life a lot easier today :)
I followed above instruction and added role capabilities manually but it says on 2nd site:
Sorry, you are not allowed to access this page.
Hello Kuldip,
Yes, this is also what I get when tried to enter second/wp-admin, even the original Admin created for Second/wp-admin
However, the first/wp-admin is okay to let “second” user to enter the “first” wp-admin.
Can you please share with me did you managed to solve it? Thanks.
I’ve successfully followed the installation paths and managed to make the two installations communicte with the same database, however…
How exactly can we automate the capabilities and levels with the function? Can we simply copy and edit the above code and add to to *ANY* plugin (say Akismet)?
Hi Carlo. Thanks for your sharing. It works perfect. I just have one problem.
I install the plugin on the first site, then i go to the first site admin user and i click “update” and nothing is created in the database, so the user’s capabilities and user_level are not being created for the second website.
If i create a new user it works, even when i update the user level on that user, that changes for both websites, not working just when i update an already existing user, what can be wrong or am i missing something? Thanks for everything!
Hi Carlo!
That’s a very nice article, well documented and understandable. Thanks man!
A question, you maybe can answer: Would that work, if the 1. WP is multisite and 2. WP is singlesite? According to the WP-Codes, WPMU adds 2 fields, so maybe this could work, too.
Thanks a lot, like it
OLAF
Hi Olaf. In my experience this only works in one-site installations. When dealing with multisite, you should look for specific plugins that manage users for you
Hi Carlo !
Can you write here the SQL query for to duplicate capabilities, roles, levels between two different tables please ?
Hi Florent. I think you don’t need to write a custom query, but I’m not sure about your goal. Please, provide more details on your purpose
Thanks for this post – very helpful! The roles function works great when sharing roles from my home site to one subdomain but I’m having trouble figuring out how to get it to send roles to multiple subdomains without crashing my home site. I tried creating separate plugins for each but if I have more than one on my home site, it crashes the site. I tried copying & pasting the function twice into the same plugin, adjusting the prefix for each, but that also crashes. I’m not a coder but I would think this could be done – I’m just not sure how to tweak it. Can you help?
Never mind… I’m happy to say I figured it out myself! :)
Hi Kristin. I’m glad to hear that from you :)
Hello carlo, thanks for the post. Still i have a problem, i perfomed this actions given on two sites that were already running and now i can’t login to either of them? is there anything i can do about that sir?
Hello!
Will this method work even if I have different types of websites with different user roles?
EG:
Main website –> Where users will create accounts
Sub-website in sub-domain –> E-learning platform (WPLMS) where there are 2 different accounts: teacher and student. Each of the user types have it’s own dashboard (teachers see their commission from course sales, they can add courses from front-end, evaluate assessments etc… while students will see their purchased courses, marks at quizzes or exams etc)
Sub-website in sub-domain –> Marketplace. Using wordpress multivendor (Dokan). So there will also be different types of users: Sellers and Buyers. Each with different options and dashboards.
Basically what I need is to redirect all the register buttons from the sub-websites to the main website.com/register. They register there and then navigate to any of the sub-websites and they will be logged in (if possible) or if not, they can login using the same credentials (user / pass). So there needs to be a default role and admin will need to manually change the role after the user sends a request for that.
Another resource that I found is Join My Multisite plugin but I’m not sure if that plugin will fix my problem.
I’m hoping to get an advice from this community.
Thank you!
Kind regards,
Catalin
Does the regular registered user will have access to the site if we don’t make this function ? I mean, i just need only one special role, admin and it’s mine.
Ok, so I am new to WP and I am working on a project and I have spent two month reading and slowly building a test site.
But I am a little confused.
My understanding is that: If you install a multisite (network), each site on the network has access to all the users on the network and each user has potencially access to all the sites they sign onto.
The user needs to be joined to each new site for example; by a click button.
It does not matter how many sites the user connects to (signs up to), there is only one user profile shared by all the sites. There is no duplication of users.
Am I wrong? Am I missing anything?
Hi!
thanks for such clear and comprehensive explanation. I have tested this solution and it works great. But I am having issues with syncing user roles (multiple user roles per user).
“Automatically Duplicate Caps and Levels with a Function” works great when only a single user role is assigned to a user. But if a user is assigned multiple user roles then it doesn’t work. I mean, it doesn’t sync all user roles.
Is there a way to copy entire ‘meta_value’ for a ‘user_id’ from “first_capabilities” to “second_capabilities”? If we can copy the entire meta_value from “first_capabilities” to “second_capabilities” then all the user roles assigned to a user can be synced.
Thanks.
Manju
Hi Manju,
I suppose you’re using a plugin that adds some extra functionalities to user roles and caps. Which plugin have you installed? Have you had a look at how it stores user data into the database?
Oh! that was quick :) just like your hosting Carlo Daniele. Thanks!
I am trying to do it manually and it didn’t work. I mean it worked for single user role but not when there are multiple user roles assigned to assigned to a user.
I had issues with syncing user roles (multiple user roles per user).
After burning midnight oil for more than two nights I found the silly magical solution :)
I simply changed ‘set_user_role’ to ‘add_user_role’ in “add_action( ‘set_user_role’, ‘ksu_save_role’, 10, 2 );” [Automatically Duplicate Caps and Levels with a Function]
The end code after the small magical tweak
function ksu_save_role( $user_id, $role ) {
// Site 1
// Change value if needed
$prefix_1 = ‘first_’;
// Site 2 prefix
// Change value if needed
$prefix_2 = ‘second_’;
$caps = get_user_meta( $user_id, $prefix_1 . ‘capabilities’, true );
$level = get_user_meta( $user_id, $prefix_1 . ‘user_level’, true );
if ( $caps ){
update_user_meta( $user_id, $prefix_2 . ‘capabilities’, $caps );
}
if ( $level ){
update_user_meta( $user_id, $prefix_2 . ‘user_level’, $level );
}
}
add_action( ‘add_user_role’, ‘ksu_save_role’, 10, 2 ); // THE MAGIC MODIFICATION
Add this to functions.php and you are great to go.
It is compatible with role changer plugins like “Woocommerce Subscriptions” and “YITH Automatic Role Changer for WooCommerce Premium”
You can set and change as many roles as you want.
That’s great news Manju! Thanks for sharing
seems there is a even much easyer way when using MySQL, i havent fully tested it yet, but the idea is that they basicaly use the same table (and wp does not even know it):
Setup two wp-installations, with same login user.
The Database Setup pointing for each wp-installation to a own database located on the same database server.
For each wp-installation a db-user which has access to the two databases.
Primary database “MyMainWPDB” (leave untouched)
Secondary database “MySecondaryWPDB”
drop wp_users
drop wp_usermeta
CREATE VIEW wp_users AS SELECT * FROM MyMainWPDB.wp_users
CREATE VIEW wp_usermeta AS SELECT * FROM MyMainWPDB.wp_usermeta
Secondary WP Instance
copy the following wp-config.php info from Primary to Secondary wp-instance
define(‘AUTH_KEY’, ‘xxx’);
define(‘SECURE_AUTH_KEY’, ‘xxx’);
define(‘LOGGED_IN_KEY’, ‘xxx’);
define(‘NONCE_KEY’, ‘xxx’);
define(‘AUTH_SALT’, ‘xxx’);
define(‘SECURE_AUTH_SALT’, ‘xxx’);
define(‘LOGGED_IN_SALT’, ‘xxx’);
define(‘NONCE_SALT’, ‘xxx’);
Aaron, did you ever try this? Let us know how it turned out if so. I may try this myself otherwise. Thanks!
I have tried it, it is working great and very creative for sharing as much data you want to pass. I will test WooCommerce orders.
I like the idea of giving members of my website the ability to login on the other domains as well. But, what would the effect be on the speed of the websites? Isn’t it a fact that a larger database makes a WP website slower than a smaller database?
Second, how could this work with a WP multisite network?
Thanks
In case anyone is having issues with syncing user roles due to caching layers like Redis copy the below code into functions.php files of both the installs.
function ksu_save_role( $user_id, $role ) {
// Site 1
// Change value if needed
$prefix_1 = ‘first_’;
// Site 2 prefix
// Change value if needed
$prefix_2 = ‘second_’;
$caps = get_user_meta( $user_id, $prefix_1 . ‘capabilities’, true );
$level = get_user_meta( $user_id, $prefix_1 . ‘user_level’, true );
if ( $caps ){
update_user_meta( $user_id, $prefix_2 . ‘capabilities’, $caps );
}
if ( $level ){
update_user_meta( $user_id, $prefix_2 . ‘user_level’, $level );
}
}
add_action( ‘add_user_role’, ‘ksu_save_role1’, 10, 2);
add_action( ‘wp_login’, ‘ksu_save_role1’, 10, 2);
For those who are looking for an SQL query to duplicate the _capabitilies and _user_level here is one way to do it:
INSERT INTO `yourdatabase_name`.`first_usermeta`
(
`umeta_id`,
`user_id`,
`meta_key`,
`meta_value`
)
SELECT
NULL,
`user_id`,
‘second_user_level’ AS first_user_level,
`meta_value`
FROM `yourdatabase_name`.`first_usermeta`
WHERE `first_usermeta`.`meta_key` = ‘first_user_level’
WARNING: Backing up your database before hand is a must!
Syntax error.
Can you please elaborate on what needs to be replaced where.
Try this plugin https://wordpress.org/plugins/share-logins/
Looks like it does exactly the same that you are looking for. You can share logins across multiple sites. NO coding etc required!
Hey, I just say your plugin and it looks awesome! I’m so excited to test it out! Woohoo! :-)
Thanks. Please let us know if it helps.
I have been facing this problem for quite a long time and haven’t found any solution yet. I want to build a site that comprises of different features which is directory, classifieds, forum, jobs, news. I could have just installed separate sites for each feature but that would not allow users to login separate sites with the same login info. I don’t know if this is the issue with the wp the way it sets up the tables. Would it be possible if we were to make tables for the user information manually for each wp sites.
Google, for example, allows users to use the same login info for youtube, gmail, google, goggle+ despite the fact that they didn’t own the youtube first and which had many users already.
Hey Gozang,
Did you check the plugin I mentioned in my last comment?
Check it out https://wordpress.org/plugins/share-logins/?
Probably not gonna get an answer but I’ll try anyway – I followed this guide and the one from trickspanda but I’m still getting “sorry youre not allowed to access this page” on site B’s admin page. I already duplicated the capabilities and level for site B.
When I try to share cookies between them I’m able to login then but I don’t have any admin access.
Fatal error in logs
CRITICAL Uncaught ArgumentCountError: Too few arguments to function ksu_save_role(), 1 passed in /wp-includes/class-wp-hook.php on line 286 and exactly 2 expected in /wp-content/themes/my-theme/functions.php: function ksu_save_role( $user_id, $role )
Hi, I am just looking at options as I have a potential need for this. Just a few questions please:
– There may be around 15 separate WordPress installations on the one server, so although they are on the same hosting account I think I read that they need to all be on the same database as well but can have different prefixes? Is that correct?
– I have the main admin account (e.g. ID1) and then using “User role editor” there is one account for a staff member on each site (e.g. ID2), but they are asking for a user to access all sites for updating content etc.. (hence the need to share login, lets say this is ID3), do all user accounts need to be shared across or can I just duplicate ID3?
– And it is literally just _capabilities and _user_level that I would duplicate, meaning that if user ID3 changes their password on any site, it will be updates in first_user and the change will be reflected for all sites?
Thanks for your time.
HI there thanks for the tutorial! I followed these steps on 2 installs with the second install on a sub-directory of the main install. I’m having an issue with the roles not being transferred over to the second install. Are there any other tables or pieces of the puzzle that could be missing?
Thanks a lot but may I ask you to remove code about usermeta table which made wordpress crash in my experience. Because it only works without it.
Hi. Thanks for the article. Just want to share my improved version based on your code. Just add new prefix to the array and paste it to other site without worrying the first/second prefix. Suitable for those with more than 3 site since user will created first to any one of them.
add_action( ‘set_user_role’, function ( $user_id, $role ) {
$prefixes = array(‘site1_’,’site2_’,’site3_’,’site4_’);
$caps = ”;
$level = ”;
// Grab existing user meta
foreach( $prefixes as $prefix ) {
// Skip if user meta found
if( $caps != ” || $level != ” )
continue;
$caps = get_user_meta( $user_id, $prefix . ‘capabilities’, true );
$level = get_user_meta( $user_id, $prefix . ‘user_level’, true );
}
// Add/update user meta
foreach( $prefixes as $prefix ) {
update_user_meta( $user_id, $prefix . ‘capabilities’, $caps );
update_user_meta( $user_id, $prefix . ‘user_level’, $level );
}
}, 10, 2);
Love this post. If we needed to sync users between 2 Woocommerce installs would it make more sense to do that via a multi-site install or by using this method with 2 separate single sites? Both sites will be owned and managed by the same umbrella company. I’d love to get some feedback on this.
Hey Leland, thank you for your comment. Actually, the configuration described in this post is not thought for WooCommerce installations. Anyway, you could run your own test, but be careful. First, you should run your tests on staging environments mirroring your production environment but never do that directly on live WooCommerce websites. If you’re not a developer, I would suggest contacting a WooCommerce developer. And backup, backup, backup before any change to your live websites!
Hi,
Works super for having user sync in multiple website. Also is there a way having different roles for same user in different website.
For eg When user created in first website the same user in second website should be automatically assigned with second website’s default role.
In a way one user having same login for both website but having different roles. I did not add the function.php code but that leaves the role to none in the second website.
How can i achieve user having default role in second website once user added in first website
Thanks
Do not change this in the second config file:
// custom users and usermeta tables
define( ‘CUSTOM_USER_TABLE’, ‘users’ );
define( ‘CUSTOM_USER_META_TABLE’, ‘usermeta’ );
Don’t add the table prefix like it was stated above.
As this will create a new user table.
Followed the tutorial with two sites with the same domain (using sub-folder and sub-domain) and it works perfectly with Kinsta hosting.
Unfortunately we need to use this with two different domains sharing the same users (users and usermeta tables) and this doesn’t work because Kinsta Hosting doesn’t allow the second site to remotely connect to the first site. You have to use ‘localhost’ in the wp-config.php.
Is there any way around this?
Hi there very good content and stuff here. Really !
But what if I have to share login between 3 or more sub-domains (7 in may case),for a WP intranet where multi-sites installation is not relevant.
Thank you
bV
Sorry I must be more specific in my request.
Does this method is relevant when users logout from one site are they logged out from all sites they passed through and 2) can they register on any sites or only on site 1 ?
Hey Bruno, when dealing with 3 or more websites, your code would quickly grow in complexity and would be really hard to maintain. Instead, you may want to consider a multisite installation: https://kinsta.com/blog/wordpress-multisite/
Hi great tutorial, I manage to link my user and usermeta between my main site and my secundary inside a subdirectory.
But if I change the user role after the user creation, the role doesn’t change in the other site. Manju solution of using ‘add_user_role’ instead of ‘set_user_role’ does actually change the role form one site to the other, but I need to erase the old user role and replace it with the new one instead of stacking them.
Is there a way to erase the previous role and add the new one when I change roles? I’m using the YITH Automatic Role Changer and I have a snippet that puts the role of the user as a class in the body tag, so I can make things appear or disappear by the user role logged in.
Hello Carlo,
Thank you for this post.
We are looking for a solution to share users log in and data from a reward program between two websites.
We are ahead of your post because our two websites are already developed and are launch in Beta for one week ago.
Would it be possible to install two new websites and implement your solution for the user login sharing and then migrate our websites onto these two new websites?
If you have any ideas or know a plugin for the sharing of our reward program data please let me know.
Thank you…
If possible please explain me where to put this code snippet:
https://kinsta.com/blog/share-logins-wordpress/#duplicate-caps-and-levels
to put it in function.php of the theme?
Looks like the post suggests making your own plugin. That code should go in the main file of the plugin.
Hi Carlo,
Thank you for sharing really useful code and information.
I use your code and it works very well.
I would just like to know if there is a way that I can add sharing of password changes for users please because I find that if they change it one one site, they need to repeat the change on the other site as well.
Will really appreciate your insight here.
Hi Those who have a problem in syncing login cookie between sites
add this
define( ‘COOKIEHASH’, md5( ‘yourdomain.com’ ) );
define( ‘COOKIE_DOMAIN’, ‘yourdomain.com’ );
define( ‘ADMIN_COOKIE_PATH’, ‘/’ );
define( ‘COOKIEPATH’, ‘/’ );
define( ‘SITECOOKIEPATH’, ‘/’ );
define( ‘TEST_COOKIE’, ‘thing_test_cookie’ );
define( ‘AUTH_COOKIE’, ‘thing_’ . COOKIEHASH );
define( ‘USER_COOKIE’, ‘thing_user_’ . COOKIEHASH );
define( ‘PASS_COOKIE’, ‘thing_pass_’ . COOKIEHASH );
define( ‘SECURE_AUTH_COOKIE’, ‘thing_sec_’ . COOKIEHASH );
define( ‘LOGGED_IN_COOKIE’, ‘thing_logged_in’ . COOKIEHASH );
This is exactly what i’m looking for, I have multiple wordpress site with different capabilities, but i don’t want my user to signup every time in every site the’re visit. Thank you
You’re welcome! We’re glad you found the article helpful.
Hey all,
Thanks for the tutorial.
I’m not able to login as admin on the second install.
All logins work properly but without admin rights.
Any ideas why?
Hi Pedro, are you sure the user on the second install was created with admin rights?
Hi Brian,
Thank you so much for your time.
Yes. In fact the second install is a clone of the first where all users are the same.
I’ve checked and first_usermeta first_capabilities meta key value for the user is a:1:{s:13:”administrator”;b:1;} and first_userlevel is 10
Found the issue.
wp_user_roles was empty on the second install.
Hi, thank you for your amazing article.
I have one issue:
The two WordPress instances already exist, not fresh instals, when I carry out the instructions here, I am able to login to the second website, however, I don’t have access to the admin section at all:
You need a higher level of permission.
Sorry, you are not allowed to list users.
Am I missing anything here?
wp2 has already wp2_user_roles row, in wp2_options if that is of significance
Hello, nice solution and works on my domain.
I reccommend, if you install some advanced user roles managemen plugins (such as User Role Editor), to install the same plugin in all the websites, or it could happen it has some incongruencies.
Could you suggest me a solution to implement the search in all the installations that share the same db/users but with different tables for posts/pages? At the moment each installation has is own search results limited to the single installation and its tables
Thank you for this article. Now you can have a main WordPress website and infinite sub websites with the same users from the main site.
A little addition on add_action( ‘set_user_role’, ‘ksu_save_role’, 10, 2 );
This is triggered by wp_create_user , wp_update_user and wp_insert_user.