Building Multisite-Compatible WordPress Themes

Posted on 16th June 2023

As a WordPress theme developer, you may be asked to build a theme that can be used on a WordPress Multisite network. In this article, we will cover what you need to know to build a WordPress theme that is compatible with Multisite.

What is Multisite?

WordPress Multisite is a feature of WordPress that allows you to create a network of sites using a single WordPress installation. Multisite is available as a feature in WordPress 3.0 and above.

If you are not familiar with Multisite, we recommend reading the following articles:

How is Multisite Different from a Standard WordPress Installation?

There are a few key differences that you need to be aware of when building a WordPress theme for Multisite:

  • In a Multisite network, each site in the network has its own directory in the WordPress installation. For example, if your WordPress installation is in the /wordpress directory, each site in the network will have its own directory within /wordpress. This is important to know when building your theme, as you will need to take this into account when referencing files in your theme.
  • In a Multisite network, each site in the network has its own set of users. This means that a user who is logged in to one site in the network will not be logged in to the other sites in the network. This is important to know when building your theme, as you will need to account for this when adding features that require a user to be logged in.
  • In a Multisite network, each site in the network has its own set of plugins. This means that a plugin that is activated on one site in the network will not be activated on the other sites in the network. This is important to know when building your theme, as you will need to account for this when adding features that require a plugin to be activated.

What You Need to Know to Build a Multisite-Compatible WordPress Theme

Now that you know what Multisite is and how it is different from a standard WordPress installation, let’s cover what you need to know to build a WordPress theme that is compatible with Multisite.

1. Referencing Files in Your Theme

As we mentioned earlier, in a Multisite network each site has its own directory. This means that you cannot reference files in your theme using a static path. For example, if your theme is in the /wordpress/wp-content/themes/my-theme directory, you cannot reference a file in your theme using the path /wordpress/wp-content/themes/my-theme/file.php. Instead, you need to use the get_template_directory() function.

The get_template_directory() function returns the path to the directory of the active theme. This means that the path returned by get_template_directory() will be different for each site in a Multisite network, as each site has its own active theme. Using get_template_directory(), you can reference a file in your theme using the following code:

<?php
$path = get_template_directory() . '/file.php';
?>

2. Adding Features that Require a User to be Logged In

As we mentioned earlier, in a Multisite network each site has its own set of users. This means that a user who is logged in to one site in the network will not be logged in to the other sites in the network. This is important to know when adding features to your theme that require a user to be logged in.

If you want to add a feature to your theme that requires a user to be logged in, you need to use the is_user_logged_in() function. The is_user_logged_in() function returns true if a user is logged in, and false if a user is not logged in. Using is_user_logged_in(), you can check if a user is logged in before adding a feature that requires a user to be logged in:

<?php
if ( is_user_logged_in() ) {
	// Add feature that requires a user to be logged in.
}
?>

3. Adding Features that Require a Plugin to be Activated

As we mentioned earlier, in a Multisite network each site has its own set of plugins. This means that a plugin that is activated on one site in the network will not be activated on the other sites in the network. This is important to know when adding features to your theme that require a plugin to be activated.

If you want to add a feature to your theme that requires a plugin to be activated, you need to use the is_plugin_active() function. The is_plugin_active() function returns true if a plugin is activated, and false if a plugin is not activated. Using is_plugin_active(), you can check if a plugin is activated before adding a feature that requires the plugin to be activated:

<?php
if ( is_plugin_active( 'plugin-name/plugin-name.php' ) ) {
	// Add feature that requires the plugin to be activated.
}
?>

4. Adding Features that Require a Specific Multisite Network Configuration

As we mentioned earlier, in a Multisite network each site has its own set of users and plugins. This means that a feature that requires a specific Multisite network configuration (e.g. a user to be logged in and a plugin to be activated) will not work on all sites in the network. This is important to know when adding features to your theme that require a specific Multisite network configuration.

If you want to add a feature to your theme that requires a specific Multisite network configuration, you need to use the is_multisite() function. The is_multisite() function returns true if the site is part of a Multisite network, and false if the site is not part of a Multisite network. Using is_multisite(), you can check if the site is part of a Multisite network before adding a feature that requires a specific Multisite network configuration:

<?php
if ( is_multisite() ) {
	// Add feature that requires a specific Multisite network configuration.
}
?>

Conclusion

In this article, we have covered what you need to know to build a WordPress theme that is compatible with Multisite. We have covered the basics of Multisite, how it is different from a standard WordPress installation, and what you need to know to build a WordPress theme that is compatible with Multisite.