11.3. WordPress Multisite Activation

In the previous blog, we installed and configured WordPress Multisite. In this blog, we explain the plugin activation in Multisite Installations.

Multisite Super Admin and Site Admins

WordPress Multisite Installation will have a Super Admin (SA) to manage the network and multiple admins to manage the sites in the network.

In multisite, only SA can install (add) and update the plugins and site admins are not allowed to do so.

SA can activate or deactivate the plugins network wide. When SA activates a plugin network wide, it will be activated in all the sites. Network wide activated plugins are not listed in the site admin Plugins menu.

WordPress Multisite Activation

However, when a plugin is installed but not activated by SA then it is listed in site admin Plugins menu and they can activate/deactivate the plugin for their site.

SA can also control whether Plugins menu is enabled for site admins. To enable Plugins menu for sites, open Network Admin Dashboard and go to SettingsNetwork Settings and scroll down to Menu Settings section at the end and check the Enable administration menus. When enabled, site admin menu panel will have the Plugins menu else it is not displayed.

 
 

WordPress Multisite Plugin Activation

The plugin activation method uses WordPress function is_multisite() and parameter $networkwide to handle the multisite activation.

share-on-social/admin/class-activator.php

class Sos_Activator {

    public function setup () {
        register_activation_hook( SOS_PLUGIN_FILE, 
                array( $this,'activate' ));
    }

    public function activate ( $networkwide ) {
        if ( function_exists( 'is_multisite' ) && is_multisite() ) {
            if ( $networkwide ) {
                if ( false == is_super_admin() ) {
                    return;
                }
                $blogs = wp_get_sites();
                foreach ( $blogs as $blog ) {
                    switch_to_blog( $blog[ 'blog_id' ] );
                    $this->activate_for_blog();
                    restore_current_blog();
                }
            } else {
                if ( false == current_user_can( 'activate_plugins' ) ) {
                    return;
                }
                $this->activate_for_blog();
            }
        } else {
            // single site stuff
            ....            
        }
    }

The WordPress function is _multisite() doesn’t exist in normal WordPress installation. Hence, we check whether the function exists and then call it to check whether the installation is multisite. If installation is not multisite enabled, then we activate the plugin for the single site as explained in Plugin Activation.

If is_multisite() is true, then we check, with the help of $networkwide parameter, whether it is a network wide activation by SA or site level activation by site admin.

In network wide activation by SA, we first check whether the user is really a SA using WordPress function is_super_admin().

Next, we fetch the list of all sites in the network using WordPress function wp_get_sites(). Sites in multisite are also called as blogs. We loop through the list of blogs and for each blog (site), we fetch its id using $blog['blog_id'] and then call WordPress function switch_to_blog() to switch to that site and the activate the plugin for the blog by calling Sos_Activator::activate_for_blog().

 
 

After activating the plugin for a blog, we need to call WordPress function restore_current_blog() so that internal pointers are reverted back to the site before switching the site.

We continue the process for all sites of the network so that plugin gets activated in all sites.

Network Wide

$blogs = wp_get_sites();
foreach ( $blogs as $blog ) {
    switch_to_blog( $blog[ 'blog_id' ] );      
      .... do something
    restore_current_blog();
}

We see this pattern in activation, deactivation and uninstall code which as follows.

  • get the list of blogs (sites) in the network.

  • for each site, call switch_to_blog() to switch to the site

  • do something by calling appropriate method.

  • revert back to the existing site using restore_current_blog().

Remember to call restore_current_blog() before switching to the next blog within the loop and failure to do so may lead to inconsistency.

In the next section, we go through the plugin deactivation code.