There are many reasons why you’d want to share users on multiple WordPress sites. WordPress Networks is a great solution, but unfortunately isn’t for everyone – especially if you’re converting an existing site into a network. The below guide offers an alternative to sharing users, and works for WordPress 4.1.
There are going to be two states for your sites: a Master site which stores all of the user data; and subsequent Slave sites, which pull the user data from the Master’s table. Decide now which will be your Master site.
1.
Ensure both your Master and Slave sites are running on the same version of WordPress, and that they share a database. Obviously if they’re sharing a database, they need to use different table prefixes. For example, our parent is wpparent_
and our slave is wpslave_
From here on, we’ll only be editing the Slave site.
2.
Find the wp-config.php
file on your Slave site (this should be in your WordPress root directory) and search for:
/* That's all, stop editing! Happy blogging. */
Just above this line, add the following:
define('CUSTOM_USER_TABLE', 'wp_users'); define('CUSTOM_USER_META_TABLE', 'wp_usermeta');
Make sure you replace the wp_
prefix with your parent prefix; so in this case we’d use wpparent_
Your two sites are now sharing the same user table! If you want to share users, but have different permissions for each site, then you can stop here. You’ll need to manually update a users permissions, as if they try and login to your Slave site, they’ll find that they don’t have sufficient permissions. This is because WordPress stores user permissions in another table.
3.
To copy over a users permissions as well, open up the wp_config.php
file for your Slave site again and add:
define('CUSTOM_CAPABILITIES_PREFIX', 'wp_');
Underneath the lines you added previously, but above "That's all, stop editing! Happy blogging."
. Again, make sure you change the WordPress prefix to your parent’s prefix.
4.
NOTE: As of WordPress 4.7.2 you will need to make the changes twice in wp-includes/class-wp-user.php
. Just search for the code below and replace both areas where it appears. If you’re still running a version of WordPress pre-4.7.2, you only need to make the change once in wp-includes/capabilities.php
.
Find the following code in either wp-includes/class-wp-user.php
or wp-includes/capabilities.php
:
$this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
Simply replace this line with:
if (defined ('CUSTOM_CAPABILITIES_PREFIX')) { $this->cap_key = CUSTOM_CAPABILITIES_PREFIX . 'capabilities'; } else { $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; }
And you should be golden!
It’s worth remembering that as we’re modifying core WordPress files, you may have to repeat these steps when you update your WordPress.
great! finally something which works, but there is a question: what about cookies and one time login for all websites? do you have some idea how to do it in WP 4.1 / 4.2?
I’ve got 2 WP installs:
xyz.com/abc
xyz.com/abc/efg
and I’m havig troubles with joining tchem in oe cookie session
Hi Damian! Thank you for your comment 🙂
This isn’t something we’ve had to do yet, but looking around on Google I’ve found this solution which might work: wordpress.stackexchange.com/questions/131399/share-login-data-cookies-between-multiple-installations
This only works if the two installations are on the same domain (which yours are). If they’re on seperate domains, the best solution may be to look into WordPress Networks