How to Enable WordPress Multisite for your Plugin
So you have written a WordPress plugin, and need to enable it to work with WordPress Multisite. Multisite allows the Network admins to install plugins on all their network blogs at once.
When it comes to plugin development, adding this feature is really not too difficult, but there are not many tutorials available to follow. And while working on this feature, I came across one prominent tutorial with code that just didn’t work. So here’s the code that actually works! This is written for version 4.3 and should work for WordPress versions as early as 3.0.0.
The code shown below is an excerpt from my Check Amazon Links WordPress Plugin. Of course, you will have to change the function and class names to the names used in your plugin.
Code that actually works!
First, the Hook. My hook looks like this because my activate function is inside of the class AmazonLinkCheckerCore:
register_activation_hook( __FILE__, array( 'AmazonLinkCheckerCore', 'activate' ) );
This hook will run on both single sites and multisite WordPress installations. There is no need to specify a different activation hook for multisite, if you write the function as follows.
The function that’s called:
public static function activate($network_wide) {
if(is_multisite() && $network_wide) {
// running on multi site with network install
global $wpdb;
$activated = array();
$sql = "SELECT blog_id FROM $wpdb->blogs";
$blog_ids = $wpdb->get_col($sql);
foreach($blog_ids as $blog_id) {
switch_to_blog($blog_id);
AmazonLinkCheckerCore::implement_activation();
$activated[] = $blog_id;
}
restore_current_blog();
update_site_option('azlc_multisite_activated', $activated);
} else { // running on a single blog
AmazonLinkCheckerCore::implement_activation();
}
// this sets a transient and should only be done once
// put any code that should only happen once, network-wide, here:
self::activate_about_page();
}
Note that my activation code uses a transient to display the settings page after activation. Setting this transient on each blog caused a big bug, so I moved that code outside of the foreach loop. If you don’t use a transient, just delete that line, but I left it here for demonstration purposes. If you do use a transient, then change the code to point to the function that you wrote that generates the transient.
The function that’s called by the above function:
public static function implement_activation() {
// put your activation code here
// for example, install databases
// initialize options
// whatever your plugin already does on activation, move here
}
You will also need to change your deactivation code so that it works in a similar manner to the above activation code. Depending on your plugin, you may need to loop through all of the blogs and perform your deactivate code on each one.
Add this to your deactivation code:
delete_site_option('azlc_multisite_activated');
Also, add code to check if any new blogs were installed on the network
This code will check if there are any new blogs installed and it will activate the plugin on those too. This ensures that your plugin always runs on every single blog on the network.
add_action('admin_init', array('AmazonLinkCheckerCore', 'multisite_check_activated'));
public static function multisite_check_activated() {
global $wpdb;
$activated = get_site_option('azlc_multisite_activated');
if($activated == 'false') {
return false;
} else {
$sql = "SELECT blog_id FROM $wpdb->blogs";
$blog_ids = $wpdb->get_col($sql);
foreach($blog_ids as $blog_id) {
if(!in_array($blog_id, $activated)) {
switch_to_blog($blog_id);
AmazonLinkCheckerCore::implement_activation();
$activated[] = $blog_id;
}
}
restore_current_blog();
update_site_option('azlc_multisite_activated', $activated);
}
}
Other Considerations
- If you have a settings page, it will appear on each blog. If you want to make the settings universal across all network blogs, you will have to implement a way to copy the settings from one blog to all. Read this tutorial: How to make your plugin have universal settings on Multisite WordPress.
- You may find settings that should always be the same on all blogs. In that case, you want the setting to be a SITE OPTION instead of a regular option. Instead of “update_option” use “update_site_option.”
Your feedback is important
I write this blog with sincere hope that I can help other WordPress developers. Please let me know if this helped you, or if I made a bad error. Thanks!
[…] Don’t miss this related article: How to Enable WordPress Multisite for your Plugin […]