PressTigers

WordPress Plugin Development — Single-site vs Multi-site

Are you looking for an introduction to WordPress plugin development that will describe all of the important steps and hold your hand along the way? Then you are at the right place! Read along to know more.

WordPress multi-site is a WordPress feature that allows us to create and manage multiple sites from the same WordPress installation.

WordPress Plugins

WordPress plugins are an add-on approach to extend WordPress functionality and better WordPress plugin development. This approach allows developers to add their code into an existing WordPress application. Apart from adding code, plugin vendors can add updates and upgrade features as well.

Single-Site, Network-Only, and Network-Wide scopes

Single site plugins are developed for a single-site WordPress installation. While it can be activated as network-wide but it will not work like a multi-site plugin.

The scope of plugins in a network site can be specified by the developer as a network only or network-wide. Network-Only plugins are supposed to be activated network-wide from the plugins section of the network. It provides global configuration and settings interfaces at the network. Along with Network, the only plugin can allow child site-level configuration as well.

Following is an example for defining a plugin to be activated as Network-Only through plugin header.

/*

Plugin Name: My Plugin

Plugin URI: https://presstigers.com/

Description: This is my WordPress plugin

Version: 1.0

Author: PressTigers

Author URI: http://www.presstiges.com

License: GPLv2

Network: True

/

Note the bold parameter of Network and its value as True, making it Network-Only.

User Roles and Capabilities

While a single-site have Administrator as a top-level role with all the possible capabilities, multi-site have super-admin roles that have privileges to manage network including all the admins of child sites.

Admin in single-site can manage users while Admins in-network can manage users of the site they are associated with. An admin deleting a user from his site will delete it from the child site but it would not be deleted from the network. Instead, super-admin can only delete the user from the network.

Database Structure

In a multi-site, WordPress creates a whole set of tables for each site with a unique prefix. For WordPress plugin development, it adds a site number to the prefix. If there are three sites in your network, the comments table for each site would be, wp_1_comments, wp_2_comments, and wp_3_comments. Remember, this would depend on the prefix WordPress assigns.

Whether your plugin was meant for a single site or multi-site, a golden rule is to always remember when making database queries, is to never use hard-coded table names. Instead, you should use the $wpdb object, to refer to a particular table.

If your plugin creates tables, then a table has to be created for every site in your multi-site network.

Updating Specific Site Data

WordPress has a global $wpdb object to manipulate the database. Using this object for getting table prefix will automatically generate the prefix that is local to the current child site where plugin code is being executed. If we are in child site number 3, $wpdb->prefix will generate “wp_3_’’.

If we want to update the database of blog id 3 while staying at blog id 2 we can use the following example of code.

it is important to note that after performing operations to other blogs, it switches back to the blog where the current code is being executed.

Using Multi-site Specific Functions

Every function meant to be used in a multi-site fro WordPress plugin development should be called only after verifying it as one.

The get_blog_option and the update_blog_option functions can be used to change the options for a particular site. Similarly, To add Actions to a particular site, you can use add_blog_action function.

Plugin activation and deactivation

If we want to perform an operation on activating multi-site plugins with network wide scope, we need to make sure all the blogs are updated for the required database changes. Following is an example of updating all the blogs if a plugin is being activated network-wide.

Similar steps should be followed when deactivating the plugin network-wide that all blogs should be updated for plugin deactivation.

Conclusion

In regards to creating a plugin that is compatible with multi-site, Core plugin functions will remain the same. What we needed to do is just add a few verification checks, and ensure that the operations are being performed on the relevant site for improved WordPress plugin development.

Are you looking for more options for WordPress plugin development and are not sure where to begin? We at PressTigers are best at performing anything related to WordPress and WordPress plugin development. Feel free to reach out to us and get connected with our Tigers and leave the rest to us.

Written By: Hijab e Fatima

User Comments

Leave a Reply

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

Get in Touch