Bobcares

WordPress Multitenancy | Setup Guide

by | Oct 24, 2022

Read this article to find out more about WordPress multitenancy. Bobcares, as a part of our WordPress Support Services, offers solutions to every query that comes our way.

WordPress Multitenancy

WordPress multitenancy refers to the use of the same WordPress code-base by several WordPress sites. Multitenancy enables independent WordPress sites to share a single code base.

How To Set Up WordPress Multitenancy?

To set up multitenancy, we use WordPress (version 3.4.1) in its directory. This was not web accessible and its wp-content directory is also deleted. Also, create two sites, with their web-accessible directories containing only the wp-content directory. Now let’s see the setup process.

Firstly, when symlinking all of the top-level files and directories in the tenant to the core, we may see a white screen. When we look at the error logs, we can see WordPress was not able to include

wp-config.phpCopy Code
. It uses the
__FILE__Copy Code
constant from PHP to carry out directory traversal. It turns out that before setting
__FILE__Copy Code
, PHP resolves the path by converting all symbolic links. WordPress then searched the core directory for
wp-config.phpCopy Code
. WordPress must search the tenant directory for
wp-config.phpCopy Code
. Here, symlinks worked. But PHP resolved paths preventing the core from including the tenant code.

The

wp-config.phpCopy Code
file’s function is to configure WordPress. So it seemed appropriate to use it. The code that was utilized
__FILE__Copy Code
only failed when referencing
wp-config.phpCopy Code
. Since
__FILE__Copy Code
refers to other core files, other core code using it needs to work as intended. In the end, the
wp-contentCopy Code
directory and the
wp-config.phpCopy Code
are the main references to the tenancy.

Since WordPress already includes a constant, configuring the

wp-contentCopy Code
directory is simple. We can change the
wp-contentCopy Code
directory’s location using the
WP_CONTENT_DIRCopy Code
function. The ability to modify this inside 
wp-config.phpCopy Code
gave this method much more credibility.

How to fix it?

The secret is to link the tenant’s

wp-config.phpCopy Code
file. All we have to do is make a
wp-config.phpCopy Code
file in the core to act as a stand-in. The tenant
wp-config.phpCopy Code
can then be included in the main
wp-config.phpCopy Code
file.

We use PHP’s super global

$_SERVER['DOCUMENT_ROOT']Copy Code
to find the tenant’s location. The server’s setup has already set this specific value. WordPress also makes use of this value. Therefore, there is no security risk. We may now symlink the other core files and folders after resolving the inclusion of the tenant
wp-config.phpCopy Code
issue.

The wp-config.php file in the core
<?php

/**

* The base configurations of the WordPress.

*

* ...

*

* @package WordPress

*/

// NOTE: this WordPress install is configured for multitenancy

require_once dirname($_SERVER['DOCUMENT_ROOT']) . '/wp-config.php';

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */

if(!defined('ABSPATH'))

define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */

require_once(ABSPATH . 'wp-settings.php');
The wp-config.php file in the Tenant
<?php
/**

* The base configurations of the WordPress.

*

* ...

*
* @package WordPress
*/
// NOTE: file lives outside webroot for additional security

// modify the config file based on environment

if (strpos($_SERVER['HTTP_HOST'], 'local') !== false) {

$config_file = 'config/wp-config.dev.php';

} else {
$config_file = 'config/wp-config.prod.php';

}

$path = dirname(__FILE__) . '/';

require_once $path . $config_file;

/** Database Charset to use in creating database tables. */

define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */

define('DB_COLLATE', '');

// ...

[Looking for a solution to another query? We are just a click away.]

Conclusion

The article explains WordPress Multitenancy in detail. We also include the setup guide from our Tech Support for Multitenancy.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

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

Speed issues driving customers away?
We’ve got your back!