easter logo

Enjoy Up To
70% OFF

Use Code:

How to Build a Multisite Compatible WordPress Plugin

How to Build a Multisite Compatible WordPress Plugin

WordPress Multisite is a topic that hasn’t gotten a lot of attention. If you’re a regular WordPress user, you’re probably aware that just a few blogs go into great length about this topic. Even in publications discussing plugin creation, it’s unusual to stumble across one that discusses multisite compatibility. I believe this is due to the misconception that working with WP multisite is difficult. Is this, however, the case?

In a nutshell, the answer is ‘No.’ ‘If you understand how WordPress Multisite works, developing a plugin multisite compatible should NOT require a significant lot of effort,’ says the author.

However, this does not guarantee that a plugin you create will work in a multisite environment. You may need to make a few minor adjustments, especially if your plugin writes data to a database. This is due to the fact that information in a multisite is formatted differently.

What is WordPress Multisite and How Does It Work?

A multisite network is a collection of sites for a single WordPress installation, as opposed to a single site deployment. In a multisite setup, multiple sites share the same database and plugins and themes. They do, however, have different pathways for storing uploads and separate tables for storing unique site-related data.

WordPress Multisite Tables

The tables produced for single-site WordPress have the same prefix, “wp_” (by default). As a result, tables like ‘wp_posts,’ ‘wp_options,’ and ‘wp_postmeta’ are frequently produced. As a result, if you wanted to get the total number of comments on your site, you could use the query below.

$comments_count = $wpdb->get_var(“SELECT COUNT(*) FROM wp_comments”);

In a multisite, however, this query might not work as planned or result in an error. This is due to the fact that the table names for WordPress multisite are different.

WordPress provides a set of tables for each site in a multisite deployment, each with its own prefix. If you have three sites in your network, the comments tables for each would be wp_1_comments, wp_2_comments, and wp_3_comments, respectively. Keep in mind that this is dependent on the prefix WordPress assigns. As a result, notable names can be assumed.

However, a golden rule to remember when making database queries is to never utilize hardcoded table names, regardless of whether your plugin was designed for a single site or multisite. To refer to a specific table, you should instead utilize the $wpdb object. As a result, our previous query should be rebuilt as,

$comments_count = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->comments”);

Here, $wpdb->comments, will return the current site’s comments table, in a multisite.

Creating Tables

If your plugin creates tables, then a table has to be created for every site in your multisite network. We will cover this in the following topic.

Changing Data Specific to a Site

WordPress uses the $wpdb object to handle all database operations internally. This indicates that the relevant table will be changed based on the prefix held by $wpdb. If $wpdb->prefix is ‘wp 2_,’ for example, tables from the second site in your network will be updated.

We need to know which blog is presently being indexed, or we need to update the index to the chosen blog, to ensure that the intended values are modified. The switch_to_blog function in WordPress comes in handy for this.

Switch to Blog Function

The switch_to_blog is a vital function, for developers of multisite compatible plugins. You will need it when doing most operations.


where $new_blog is the id of the new blog (site) you want to use. Any WordPress functions you use after using this method and switching to a specific site will operate on that site.

When you look at how the switch_to_blog function works, you’ll notice that it changes the current blog id and the $wpbd object to correspond to the selected blog.

$wpdb->set_blog_id( $new_blog );

This means you won’t have to utilize many functions to execute tasks like update_post_meta, generate tables for all blogs, or update plugin parameters. Simply remember to run the switch_to_bog  method before putting the code in place.

To create tables for each site from your plugin, for example, your plugin code would need to look like this:

function themedev_activate(){

global $wpdb;

// check if it is a multisite network

if (is_multisite()) {

// check if the plugin has been activated on the network or on a single site

if (is_plugin_active_for_network(__FILE__)) {

// get ids of all sites

$blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");

foreach ($blogids as $blog_id) {


// create tables for each site







// activated on a single site, in a multi-site






// activated on a single site




register_activation_hook( __FILE__, 'themedev_activate' );

The table-creation code will have to go into the themedev_perform_activation  function. Creating a Custom Table with Plugins is a good place to start learning how to make a table.

You must then use $wpdb->prefix.’table_name’ to query a custom table. Remember that when you delete a site, you should also delete the table produced by your plugin.

Restore the functionality of the current blog

We utilized the restore current blog method in the above code, as you can see. When using switch_to_blog, this function should always be utilized. Both of these functions put and pop data onto the global stack ‘_wp switched stack’. The wp_upload_dir function is affected by the switch blog function. The wp_upload_dir function creates your site’s URLs. As a result, if you don’t switch back, you can end up with the wrong URLs.

Using Multisite Specific Functions

Every function meant to be used in a multisite, should be called only after verifying a multisite deployment, using is_multisite function.

For example;

if( is_multisite()) {

echo network_home_url();


Actions can be added to a particular site, using the add_blog_action function. Similarly, the get_blog_option and the update_blog_option functions can be used to change the options for a particular site.


Our basic plugin functionality remained the same, as you may have seen. All we had to do was add a few checks and make sure the procedures were being carried out on the correct site. Thus, creating a plugin for a WordPress Multisite installation isn’t all that different from creating one for a single site; all you have to do is follow the steps outlined above.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Golam Mostafa
Written by

Golam Mostafa

Golam Mostafa is a WordPress aficionado with a good sense of cinema. He enjoys reading books and playing football in his spare time.

Table of Content

Table of Contents