Speed Lights, by Jonel Hanopol

A mu-plugin is a special kind of WordPress plugin, originally created to facilitate the administration of multi-user (that’s why the ‘mu’) sites but later renamed as Must-Use and made available for all kinds of sites in order to help everybody to benefit from their special features.

The most interesting feature of mu-plugins is that they are always executed before loading the “normal” plugins which makes them very useful to control how (and when) those other plugins are loaded. In our case, the mu-plugin we deploy as part of our A/B Testing for WordPress service, increases the page speed of split testing experiments by stopping other plugins to interfere with our AJAX calls (part of our page loading process) and delay them. Thanks to the mu-plugin, we can stop them during some “routine AJAX calls” that our plugin needs to perform, until we are done.

However, when we first included this plugin as part of the Nelio release we realized that practically nobody was using it. The reason is simple, mu-plugins cannot be installed from your WP Dashboard, they need to be directly uploaded to a special folder (wp-content/mu-plugins by default). Same for uninstalling them. And that’s just too complicated for many users.

Therefore, with a little help from our friends on the Advanced WordPres group on Facebook (not on Facebook? read about the group here ), we decided to automate the (un)installation of the mu-plugin by adding a new option in our settings page (available starting at Nelio v. 3.0.7).


Click a button and get your mu-plugin installed for you. As easy as it gets for the user and code-wise, it´s not that much work either once you know the constants storing the location of the mu-plugins directory and the WP functions to create directories and files. We show it here for your reference (hoping you may find it useful, if you know a better way to do it please let us know!).

public static function is_performance_muplugin_installed() {
			$mu_dir = ( defined( 'WPMU_PLUGIN_DIR' ) && defined( 'WPMU_PLUGIN_URL' ) ) ? WPMU_PLUGIN_DIR : trailingslashit( WP_CONTENT_DIR ) . 'mu-plugins';
			$mu_dir = untrailingslashit( $mu_dir );
			$mu_plugin = $mu_dir . '/nelioab-performance.php';
			return file_exists( $mu_plugin );
		public static function toggle_performance_muplugin_installation() {
			$mu_dir = ( defined( 'WPMU_PLUGIN_DIR' ) && defined( 'WPMU_PLUGIN_URL' ) ) ? WPMU_PLUGIN_DIR : trailingslashit( WP_CONTENT_DIR ) . 'mu-plugins';
			$mu_dir = untrailingslashit( $mu_dir );
			$source = NELIOAB_ROOT_DIR . '/mu-plugins/nelioab-performance.php';
			$dest = $mu_dir . '/nelioab-performance.php';
			$result = array( 'status' => 'OK', 'error' => '' );
			if ( !self::is_performance_muplugin_installed() ) {
				// INSTALL
				if ( !wp_mkdir_p( $mu_dir ) ) {
					$result['error'] = sprintf(
						__( 'Error! The following directory could not be created: %s.', 'nelioab' ), $mu_dir ); $result['status'] = 'ERROR'; } if ( $result['status'] !== 'ERROR' && !copy( $source, $dest ) ) { $result['error'] = sprintf( __( 'Error! Could not copy Nelio's performance MU-Plugin from %1$s to %2$s.', 'nelioab' ), $source, $dest ); $result['status'] = 'ERROR'; } } else { if ( file_exists( $dest ) && !unlink( $dest ) ) { $result['error'] = sprintf( __( 'Error! Could not remove the Nelio's performance MU-Plugin from %s.', 'nelioab' ), $dest ); $result['status'] = 'ERROR'; } } header( 'Content-Type: application/json' ); echo json_encode( $result ); die(); }

Featured image by Jonel Hanopol

One response to “Installing a WordPress mu-plugin automatically”

  1. TimbreDesign Avatar

    6 years later, thank you for this, a big help!

Leave a Reply

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

I have read and agree to the Nelio Software Privacy Policy

Your personal data will be located on SiteGround and will be treated by Nelio Software with the sole purpose of publishing this comment here. The legitimation is carried out through your express consent. Contact us to access, rectify, limit, or delete your data.