Multisite Optimizations and Jetpack Wrangling

Performance issues on a WordPress multisite installation can be a challenge to solve. Over the last few weeks I’ve been making various performance improvements on a rather large multisite network. The following is a recap of those optimizations using SSH and WP-CLI.

Remove expired transients across network

On a regular site a simple WP-CLI command like wp transient delete --expired will remove any expired transients. However on a multisite network that will only clear expired transients on the main site not the rest of the multisite network. To loop through all subsites you’ll want to use the following WP-CLI command.

wp transient delete --expired --network && wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --expired

If that one-liner is too confusing then see the shell script equivalent. The following script will produce the same results as the above command.

for site in $( wp site list --field=url )
do
   wp transient delete --expired --network --url=$site
done

Deactivate various Jetpack modules

Jetpack is generally fairly good at offloading performance intensive tasks to WordPress.com’s servers. That said it doesn’t mean you should just run Jetpack blindly on multisite. The same one-liner approach can be useful for deactivating various Jetpack modules across the entire multisite. Here are a few examples.

# Deactivate Jetpack monitor module
wp jetpack module deactivate monitor && wp site list --field=url | xargs -n1 -I % wp --url=% jetpack module deactivate monitor

# Deactivate Jetpack protect module
wp jetpack module deactivate protect && wp site list --field=url | xargs -n1 -I % wp --url=% jetpack module deactivate protect

# Deactivate Jetpack image CDN
wp jetpack module deactivate photon && wp site list --field=url | xargs -n1 -I % wp --url=% jetpack module deactivate photon

# Deactivate Jetpack asset CDN
wp jetpack module deactivate photon-cdn && wp site list --field=url | xargs -n1 -I % wp --url=% jetpack module deactivate photon-cdn

# Deactivate Jetpack stats
wp jetpack module deactivate stats && wp site list --field=url | xargs -n1 -I % wp --url=% jetpack module deactivate stats

After performing bulk deactivation of Jetpack modules I highly recommend running wp jetpack sync start. This will make sure these module changes are pushed up to WordPress.com’s servers.

Hunt for rogue Jetpack monitor activity.

For multisite network which have a high turnover of subsites, cleaning up old sites can be tricky. The following command on Kinsta’s SSH will reveal any Jetpack monitor activity.

tail -f ~/logs/access.log | grep "jetmon"

On my multisite this uncovered a surprising amount of traffic. A good portion of this traffic wasn’t valid and going to an old subsite which had been archived or removed. I reached out to Jetpack’s Support team for help in order to deactivate Jetpack monitor on sites which no longer exist.

Prevent certain Jetpack modules from being used.

If you really don’t want a particular Jetpack module used then blacklist it. Make a must-use plugin called anchor-jetpack-control.php stored under wp-content/mu-plugins. The following will remove four modules from being listed as options within Jetpack. Feel free to change for your own needs. The array $mods_to_disable specifies which Jetpack modules should be removed. Refer to Github for a full list of Jetpack module names.

<?php
/*
Plugin Name: Anchor Multisite Jetpack Control
Description: Disable select Jetpack modules
Version: 1.0
Author: austinginder
Author URI: https://austinginder.com
Network: true
License: GPL2
*/

// Blacklist the following Jetpack modules
function anchorhost_blacklist_jetpack_modules( $modules ) {

        $mods_to_disable = array( 'photon', 'photon-cdn', 'stats', 'monitor' );

        foreach ( $mods_to_disable as $mod ) {
                if ( isset( $modules[ $mod ] ) ) {
                        unset( $modules[ $mod ] );
                }
        }
        return $modules;
}
add_filter( 'jetpack_get_available_modules', 'anchorhost_blacklist_jetpack_modules' );