Sharing users amongst different WordPress sites is easy. Please keep in mind three things mainly:
All the changes are done in the WP-CONFIG file in the root of the sites that need to share the users. It can be sites in sub-domains or in folders under the main site. Now make the following changes to the wp-config files in your CHILD sites which will sit under the main site.
1. Share the databases (it helps if the sites are on the same server as you can use LOCALHOST otherwise you should use the right path to your MySQL database:
// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘shared database name’);
/** MySQL database username */
define(‘DB_USER’, ‘shared database user’);
/** MySQL database password */
define(‘DB_PASSWORD’, ‘shared database password’);
/** MySQL hostname */
/** Database Charset to use in creating database tables. */
/** The Database Collate type. Don’t change this if in doubt. */
In this file where you define the database tables and link to it you will need to use the same information in all the blogs which are going to share the users.
2. Insert this code just before and after the : /* That’s all, stop editing! Happy blogging. */ as shown below:
Now here you need to note that the table identifier wpmainsite_ is what you define when you are installing the WordPress on your server. This can be changed as well using the Better WP Security plugin. This has to be unique for all the sites and here in the code below you will use the table preffix for the main site for all the wp-config files in your blogs.
define(‘CUSTOM_USER_TABLE’,’wpmainsite_users’); // shared user db
define(‘CUSTOM_USER_META_TABLE’,’wpmainsite_usermeta’); // shared usermeta
/* That’s all, stop editing! Happy blogging. */
$baseurl = ‘http://yourmainsite.com’; // replace with the actual domain name for your root site
$cookiehash = md5($baseurl);
define (‘AUTH_COOKIE’, ‘wordpress_’.COOKIEHASH);
define (‘SECURE_AUTH_COOKIE’, ‘wordpress_sec_’.COOKIEHASH);
define (‘TEST_COOKIE’, ‘wordpress_test_cookie’);
3. Share the same Authentication Unique Keys in the wp-config: These keys can be generated from the following link:
Now once you get the keys use them and copy paste them in all the wp-config files under each CHILD site and here is the code in that file which you will replace with the keys generated from above link. Make sure you use the same keys for all sites.
define(‘AUTH_KEY’, ‘ ‘);
define(‘SECURE_AUTH_KEY’, ‘) ;
4. Use this plugin: WP-ORPHANAGE to assign roles to the added users as the wp-meta table wont have that even if you are sharing the users and you will end up with the issue of Permissions to access the admin panel.
That is it this should work for you now and all your sites should be sharing the users and the users dont even have to re-login from site it site journey. The cookie hash code above insure that the users remain logged in and are picked up from the cookie that is placed in the browser. Happy WordPressing ….