Multiple WordPress sites with same users

Today, I’ll be showing you how to setup multiple WordPress sites with same users such that a user only requires a single account to be able to log into the multiple sites. Also, once a user logs into one of the WordPress sites, she is automatically logged into the others.

In practise, you will have to be able to decide between using the approach detailed below and WordPress multisite. In some instances, WordPress multisite might just be the better way to achieve your objective.

Requirements

These requirements are not sacrosanct. I spent about 4 hours trying to figure out the solution – most of the guides online were outdated, addressing WordPress version 2.6, 3 etc., so did not work for 4.3.1 which I was running. So first, this guide works for WordPress version 4.3.1.

Also, your site will need to have a main WordPress installation, which should be running at the root of your domain. The users and usermeta tables in the database of this main site will be the reference database for the other secondary WordPress installations. The secondary should be installed at a sub-directory to the main site. It should be possible to make it work from a sub-domain installation, but I cannot confirm this. You can have as many secondary sites as possible.

Thirdly, you should be familiar with using WebDeveloper in chrome (to clear cookies), phpMyAdmin (to modify database tables), and have FTP access to both the main and secondary sites.

Lastly, both the primary site and secondary site(s) must use the same database, but different table prefixes. In this guide, I am assuming you installed the primary site using the prefix ‘wp_‘, and the secondary site with prefix ‘siteb_‘.

Multiple WordPress sites with same users.

Alright, here are the steps you need to setup multiple WordPress sites with same users. I am assuming that you have installed the main site at http://example.org and the secondary site at http://example.org/siteB.

  1. Comment out all the following defines in wp-config.php of siteB, then copy and paste those from the wp-config.php of the main site. I prefer you just comment out with

    [php]/*      */[/php]

    , than outright deleting the defines, just in case you need to revert back to your original config – not like you cannot regenerate random characters for the keys and salts.

    [php]define(‘AUTH_KEY’, ‘random-characters-main-site’);
    define(‘SECURE_AUTH_KEY’, ‘random-characters-main-site’);
    define(‘LOGGED_IN_KEY’, ‘random-characters-main-site’);
    define(‘NONCE_KEY’, ‘random-characters-main-site’);
    define(‘AUTH_SALT’, ‘random-characters-main-site’);
    define(‘SECURE_AUTH_SALT’, ‘random-characters-main-site’);
    define(‘LOGGED_IN_SALT’, ‘random-characters-main-site’);
    define(‘NONCE_SALT’, ‘random-characters-main-site’);[/php]

  2. Just after

    [php]define(‘WP_DEBUG’, false);[/php]

    in the secondary site’s wp-config.php file, enter

    [php]define(‘CUSTOM_USER_TABLE’, ‘wp_users’);
    define(‘CUSTOM_USER_META_TABLE’, ‘wp_usermeta’);[/php]

    What you are doing here is telling siteB to no longer use its own users and usermeta tables, but rather use those of the main site. Remember that both primary and secondary sites should use the same database. Also, all users in the secondary database will no longer be able to log in. You should now be able to log into both sites using the same user information.

  3. Next, you will need to insert capabilities into the database for the main site which siteB will work with, otherwise siteB will complain of lack of permissions/roles, specifically “You do not have sufficient permissions to access this page.,” and although you will be able to login, you will not be able to do anything further. Enter new capabilities for the admin of the main site. If you edit in phpMyAdmin, your entries should look like this.wordpress phpmyadmin
  4. Once the admin is able to successfully log in at http://example.org/siteB, you can visit the users menu, edit each user and then update the roles. You will not need to edit your database by hand anymore as wordpress will setup the appropriate capabilities(role) for each user.

Now, you should be able to log into both the main and secondary sites with the same user details. You will notice however that you always need to log into each individual site separately – logging into one does not automatically log you into the other(s).

To get this to work, make the following edits to the wp-config.php file of both the main site and secondary site(s). Just before

[php]/* That’s all, stop editing! Happy blogging. */[/php]

, enter

[php]$cookiehash = md5("http://example.org/");

define(‘COOKIEHASH’, $cookiehash);
define(‘COOKIE_DOMAIN’, ‘.example.org’);
define(‘COOKIEPATH’, ‘/’);
define(‘SITECOOKIEPATH’, COOKIEPATH);
define( ‘ADMIN_COOKIE_PATH’, COOKIEPATH );[/php]

Now, once you log into either the main or secondary site, your login will be automatically detected by every other site and you will not have to log in again.

There you have it! If this saves you some time, kindly share using any of the widgets below.

Leave a Reply