I have many plugins that I wrote for WordPress, and now I want to adapt them to MU.
What are the considerations / best practices / workflow / functions / pitfalls that I have to follow / avoid / adapt in order to ‘upgrade’ my plugins to support also Multisite installations?
For example, but not limited to:
- Enqueue scripts/register
- Incuding files (php, images)
- Paths for custom files uploads
$wpdb
- Activation, uninstall, deactivation
- Handling of admin specific pages
In the Codex, there are sometimes remarks about Multisite in single function description, but I did not find any one-stop page that address this subject.
As for enqueuing and including, things go as normal. Plugin path and URL are the same.
I never dealt with anything related to upload paths in Multisite and I guess normally WP takes care of this.
$wpdb
There is a commonly used snippet to iterate through all blogs:
You may find examples where
restore_current_blog()
is used instead ofswitch_to_blog( $original_blog_id )
. But here’s whyswitch
is more reliable: restore_current_blog() vs switch_to_blog().$blog_id
Execute some function or hook according to the blog ID:
Or maybe:
Install – Network Activation only
Using the plugin header
Network: true
(see: Sample Plugin) will only display the plugin in the page/wp-admin/network/plugins.php
. With this header in place, we can use the following to block certain actions meant to happen if the plugin is Network only.Uninstall
For (De)Activation, it depends on each plugin. But, for Uninstalling, this is the code I use in the file
uninstall.php
:Admin Pages
1) Adding an admin page
To add an administration menu we check if
is_multisite()
and modify the hook accordingly:2) Check for Multisite dashboard and modify the admin URL:
3) Workaround to show interface elements only in main site
Without creating a Network Admin Menu (action hook
network_admin_menu
), it is possible to show some part of the plugin only in the main site.I started to include some Multisite functionality in my biggest plugin and did the following to restrict one part of the plugin options to the main site. Meaning, if the plugin is activated in a sub site, the option won’t show up.
Looking at this again, maybe it can be simply:
is_multisite() && is_super_admin() && is_main_site()
. Note that the last two returntrue
in single sites.And then:
4) Collection of useful hooks and functions.
Hooks:
network_admin_menu
,wpmu_new_blog
,signup_blogform
,wpmu_blogs_columns
,manage_sites_custom_column
,manage_blogs_custom_column
,wp_dashboard_setup
,network_admin_notices
,site_option_active_sitewide_plugins
,{$hook}admin_menu
Functions:
is_multisite
,is_super_admin
is_main_site
,get_blogs_of_user
,update_blog_option
,is_network_admin
,network_admin_url
,is_network_only_plugin
PS: I rather link to WordPress Answers than to the Codex, as there’ll be more examples of working code.
Sample plugin
I’ve just rolled a Multisite plugin, Network Deactivated but Active Elsewhere, and made a non-working resumed annotated version bellow (see GitHub for the finished full working version). The finished plugin is purely functional, there’s no settings interface.
Note that the plugin header has
Network: true
. It prevents the plugin from showing in child sites.Other resources – e-books
Not directly related to plugin development, but kind of essential to Multisite management.
The e-books are written by no less than two giants of Multisite: Mika Epstein (aka Ipstenu) and Andrea Rennick.