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.php. It uses the __FILE__ constant from PHP to carry out directory traversal. It turns out that before setting __FILE__, PHP resolves the path by converting all symbolic links. WordPress then searched the core directory for wp-config.php. WordPress must search the tenant directory for wp-config.php. Here, symlinks worked. But PHP resolved paths preventing the core from including the tenant code.

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

Since WordPress already includes a constant, configuring the wp-content directory is simple. We can change the wp-content directory’s location using the WP_CONTENT_DIR function. The ability to modify this inside  wp-config.php gave this method much more credibility.

How to fix it?

The secret is to link the tenant’s wp-config.php file. All we have to do is make a wp-config.php file in the core to act as a stand-in. The tenant wp-config.php can then be included in the main wp-config.php file.

We use PHP’s super global $_SERVER['DOCUMENT_ROOT'] 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.php 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 *

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