Enjoy Up To
80% OFF

How to build a multisite compatible WordPress plugin

How to build a multisite compatible WordPress plugin

With the help of WordPress Multisite, a network of websites may be built using a single WordPress installation. This function can help maintain network-wide consistency in user experience when managing several sites with shared users, themes, and plugins.

But, particularly for beginners, understanding how WordPress Multisite functions can be difficult.

In this post, we’ll go into the specifics of WordPress Multisite’s operation and examine its advantages. We will also talk about how the WordPress Multisite Plugin helps the network run better.

Whether you are an experienced WordPress user or just getting started, this blog will provide you with the knowledge and insights you need to understand how WordPress Multisite Plugin works and how it can benefit your website or online business.

What is WordPress multisite plugin?

WordPress has a feature called WordPress Multisite that enables you to run a network of websites on a single WordPress installation. This implies that you can operate different WordPress sites on the same server, each with their own database of users, themes, and content.

WordPress Multisite Plugin’s plugin improves the network’s functionality by providing new features or functions. All the sites in the network can utilize these plugins because they are installed at the network level.

The WordPress Multisite Plugin may be a great method to administer several websites from a single dashboard and provide a unified user experience for visitors to all of your network’s websites.

How does WordPress multisite plugin work?

A built-in feature of WordPress called WordPress Multisite enables you to set up a network of websites using just one WordPress installation. When Multisite is enabled, you can set up multiple sites with their content, users, and themes on a single WordPress installation.

You must change the WordPress configuration files, notably the .htaccess and wp-config.php files, to allow Multisite. From the WordPress dashboard, select Tools > Network Configuration to activate Multisite.

Upon the activation of Multisite, you may add new sites to the network by going to My Sites > Network Admin > Sites and selecting the “Add New” button. Through the Network Admin panel, you can also manage users and themes for all of the network’s sites.

WordPress Multisite Plugin works by bringing additional features to the Multisite network. All the sites in the network can utilize these plugins because they are installed at the network level. Some plugins might offer more options or capabilities to the network dashboard, while others might provide individual sites in the network more functionality.

Ultimately, WordPress Multisite Plugin allows you to administer several sites from a single dashboard while also delivering your users a uniform experience across all sites within your network.

The role of WordPress multisite plugin

You can build a network of many websites that are all administered from a single WordPress installation with the WordPress Multisite Plugin. With the help of this plugin, you may build a network of websites that share the same WordPress files, database, and login procedure.

For businesses that need to manage many websites, such as universities with separate sites for various departments or businesses with various brands, the Multisite Plugin is excellent. It makes it simpler to maintain consistency and up-to-date content across your websites by allowing you to manage them from a single dashboard.

With Multisite, you can effortlessly build new websites and control the network of sites from one location. Because of this, updating WordPress’s core files, themes, and plugins is simple across all websites. Also, managing user accounts and permissions for all sites can be done anywhere, saving you time and effort.

The WordPress Multisite Plugin is an important element for anyone in charge of a network of WordPress websites, since it makes it easier to manage several websites and lets you organize your workflow.

How to manage WordPress multisite tables?

The prefix “wp_” is used for all tables created for WordPress single-site installations (by default). Tables like “wp_posts,” “wp_options,” and “wp_postmeta” are therefore generally produced. As a result, you might use the following query to determine the total number of comments made on your website.

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

However, in a multisite, this query might not function as expected or produce an error. This is a result of the varied table names for WordPress multisite.

In a multisite deployment, WordPress offers a series of tables, each with its own prefix. The comments tables for each of the three sites in your network would be wp_1_comments, wp_2_comments, and wp_3_comments, respectively. You should be aware that this depends on the prefix that WordPress assigns. Hence, famous names can be presumed.

But, whether your plugin was created for a single site or multisite, a golden rule to follow while creating database queries is to never use hard coded table names. The $wpdb object is what you should use if you want to refer to a specific table. As a result, we need to rewrite our previous query as,

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

In a multisite environment, $wpdb->comments will return the comments table for the current site.

Creating Tables

A table must be established for each site in your multisite network if your plugin generates tables. This will be discussed in the section that follows.

Changing Data Specific to a Site

Internally, WordPress manages all database activities via the $wpdb object. This means that based on the prefix stored in $wpdb, the relevant table will be modified. Tables from the second site in your network will be updated, for instance, if $wpdb->prefix is set to ‘wp 2_,’

To make sure that the desired values are changed, we either need to know which blog is currently being indexed or we need to update the index to the chosen blog. In this situation, WordPress’ switch_to_blog function is useful.

Switch to blog function

For creators of multisite compatible plugins, the switch_to_blog function is essential. You will require it for the majority of procedures.

switch_to_blog($new_blog)

where the id of the new blog (site) you intend to use is $new_blog. After employing this technique and going to a specific site, all WordPress functionality you utilize will work on that site. As you examine how the switch_to_blog function operates, you’ll see that it modifies the $wpbd object and the current blog_id to correspond to the chosen blog.

$wpdb->set_blog_id( $new_blog );

This indicates you won’t have to employ many functions to conduct activities like update_post_meta, produce tables for all blogs, or update plugin settings. Just keep in mind to execute the switch_to_bog method before inserting the code.

For instance, your plugin’s code would need to resemble this in order to generate tables for each site:

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) {

switch_to_blog($blog_id);

// create tables for each site

themedev_perform_activation($blog_id);

restore_current_blog();

}

}

else

{

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

themedev_perform_activation($wpdb->blogid);

}

}

else

{

// activated on a single site

themedev_perform_activation($wpdb->blogid);

}

}

register_activation_hook( __FILE__, 'themedev_activate' );

The themedev_perform_activation method will require the table-creation code. It’s a good idea to start by reading How to Make a Custom Table with Plugins before making your table. To query a custom table, you must use $wpdb->prefix.’table_name’. Keep in mind that you should also remove the table your plugin created when you delete a site.

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.

As you can see from the code above, we used the restore current blog method. This function ought to be used at all times when switch_to_blog is being used. These two functions both push and pop data into the ‘_wp switched stack’ global stack.

The switch blog function has an impact on the wp_upload_dir Created using the wp_upload_dir function, your website’s URLs. As a result, you risk having the incorrect URLs if you don’t switch back.

Using multisite specific functions

Only after executing the multisite function to confirm a multisite deployment should any function intended for use in a multisite be used.

For example:

if( is_multisite()) {

echo network_home_url();

}

With the add_blog_action function, actions can be added to a given website. The functions get_blog_option and update_blog_option can also be used to modify the options for a specific website.

Conclusion

The WordPress Multisite Plugin is an incredibly powerful tool for managing multiple websites from a single dashboard. With this plugin, you can easily create and manage a network of sites, streamline your workflow, and save time and effort in the process.

We’ve explored the key features of WordPress Multisite Plugin, including the ability to manage user roles and permissions, install and manage plugins and themes, and update core files across all sites. We’ve also discussed the benefits of using Multisite for organizations that need to manage multiple websites, such as universities, businesses, and nonprofit organizations.

Overall, the WordPress Multisite Plugin is a versatile and useful tool that can help you manage a network of websites more efficiently. By understanding how it works and leveraging its features, you can simplify your workflow and focus on creating great content and user experiences across all your sites. So, whether you’re managing a handful of sites or dozens, the WordPress Multisite Plugin is a must-have tool in your website management toolkit.

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.

all-plugin
Golam Mostafa
Written by

Golam Mostafa

I'm a senior plugin developer for WordPress. I have four years of experience working as a software engineer for ADB Bank, and in 2018, I started developing plugins and founded ThemeDev.

Table of Content

Table of Contents

LTD Deals: 10+ Pro Plugins

Limited Time Offer
LTD Deals: 10+ Pro Plugins

Offer ends soon! Don't let it get away this time!

(NO RECURRING FEES)

Trusted by 25,099+ Customers

30 days Money Back Guarantee