Case Study and How-to: Setup Multiple WordPress Sites To Share a Single Database and Users

Near the end of developing a feature-filled WordPress website, I was asked by a client to setup a way for site visitors to review clinics. After lots of back and forth, we settled on a vendor which provided a Yelp clone as a WordPress theme. The problem? We already had a massive site developed and themed. There didn’t seem to be a better solution with the budget and time constraints.

I needed a way to use the existing theme with the new theme, which probably would have been better off if the vendor had made it a plugin – oh well.

At first, Multisite seemed to be a good way to go. However, some of the plugins didn’t quite support Multisite, and subdomains would have been required. We wanted to use subdirectories for SEO reasons. There were a couple other solutions which got ruled out.

Eventually, I came to the conclusion the easiest way to share users and use two different themes was to setup a second WordPress installation. I would use the same database and make a new table prefix. Inside of wp-config, I’d map out the shared users to the original table prefix.

There are a lot of articles out there which gave me a pretty good idea of how to do this, but without any live examples or confirmation on what actually worked, I set out to document my process and publish the results.

As of writing this, both sites are on a development server, which is accessed by a subdomain on the Frog Stone Media website.

Server Info:

  • CentOS 6.6
  • Apache 2.2.x
  • PHP 5.5.x
  • MySQL 5.5.x
  • phpMyAdmin 4.0.x
  • cPanel 11.48.x

The Existing WordPress Website:

  • WordPress 4.1.x
  • 48 posts
  • 69 pages
  • 26 plugins
  • 4 users
  • 18,172 database records
  • 122 home page queries
  • 99 inside page queries

The Second WordPress Website:

  • WordPress 4.1.x
  • 0 posts
  • 0 pages
  • 5 plugins
  • 14,057 database records
  • 88 home page queries
  • 96 inside page queries

Architecture:

existingwebsite.frogstonemedia.com
existingwebsite.frogstonemedia.com/secondsite

Step 1

Create a new directory via FTP

Step 2

Upload the latest version of WordPress

Step 3

Rename wp-config-sample.php to wp-config.php

Step 4

Copy the wp-config settings from the existing website to the new WordPress site

Step 5

Change the table prefix in the new wp-config to something different.

Step 6

Run through the WordPress installer on the new site. Use the same username and password on the first site, although it won’t end up mattering.

After you finish setting up WordPress, go ahead and login.

Step 7

By default, users on the new site will have their role set to “Unassigned” and be unable to login. We’ll need to fix that. This will set a specific user’s role to Administrator upon logging in, and everyone else to Contributor.

Add the following function to your new site’s theme’s functions.php or create a basic plugin and install it.

Be sure to change ‘Mike’ to whatever your administrator username is for the old site.

Also, change the wp_capabilities to the correct table prefix you are using.

function check_user_role($user_login, $user) {
 if ( $user_login == 'Mike' ) {
 $user->add_role('administrator');
 } else {
 $user->add_role('contributor');
 }
}

// Run action on login
add_action('wp_login', 'check_user_role', 10, 2);

Now logout of WordPress.

Step 8

Add the following two lines to wp-config via FTP:

define('CUSTOM_USER_TABLE', 'tableprefix_users');
define('CUSTOM_USER_META_TABLE', 'tableprefix_usermeta');

Where tableprefix is, replace it with the original site’s table prefix.

Login and you’ll see the administration menu on the left displaying admin capabilities.

For other users, you’ll have to manually assign roles for those other than a Subscriber.

 

Comments 1

  1. hi there
    this is great but the problem is that if we change the original sites user role of a given user it doesnt update on the second site
    any ideas?
    thanks
    p

Leave a Reply

Your email address will not be published. Required fields are marked *