How to make a custom (MU) plugin

The difference between a regular plugin and a MU (“ must use”) plugin in WordPress is: MU plugins are activated automatically once uploaded to a specific folder in WordPress.

In this Article

Making a custom MU plugin

1
Create a folder named mu-plugins in your wp-content folder.
In case a mu-plugins folder already exists, don’t create a new one, just use the existing one.
2
Using a basic text editor create a new file and put some code in it.
For a head start you can use the simple boilerplate at the end of this article.
3
Save the file with whatever name you want using only latin characters, numbers, and dashes in your file name.
Example file name: wp-rocket-custom.php
4
Upload this file to the mu-plugins folder created in step 1.
5
Finally, make sure to deactivate and re-activate WP Rocket.

Note: You can make a regular plugin instead of a MU plugin just by dropping the same file in wp-content/plugins instead of mu-plugins. Remember you’ll have to activate your regular plugin on your WP Admin plugins page.

Things to consider

  • Always test your plugin before uploading it to your public website!
    Remember that a missing semicolon in PHP can break your site. When anything breaks, remove the plugin file.
  • Make sure your text editor is set to plain text mode when editing code!
    Rich text would compromise your code and result in PHP errors, or even a broken site.
  • MU plugins are loaded automatically and in alphabetical order of their file names.
  • If for some reason you use an existing function from WP Rocket, always wrap it in a condition like this:
    if ( function_exists( 'some_wp_rocket_function' ) ) { 
    	// do something with some_wp_rocket_function() here
    }
    	
  • Again, make sure to deactivate and re-activate WP Rocket once you have uploaded your custom plugin.

Example MU plugin

In case you happen to run into issues with code copy-pasted from here, try downloading the Gist instead and make sure, you’re text editor is set to UTF-8.

<?php
/**
 * Plugin Name: Your Plugin Name Here
 * Description: Short description of your plugin here.
 * Author:      your name here
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

// Basic security, prevents file from being loaded directly.
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );

/* Prefix your custom functions!
 *
 * Function names must be unique in PHP.
 * In order to make sure the name of your function does not
 * exist anywhere else in WordPress, or in other plugins,
 * give your function a unique custom prefix.
 * Example prefix: wpr20151231
 * Example function name: wpr20151231__do_something
 *
 * For the rest of your function name after the prefix,
 * make sure it is as brief and descriptive as possible.
 * When in doubt, do not fear a longer function name if it
 * is going to remind you at once of what the function does.
 * Imagine you’ll be reading your own code in some years, so
 * treat your future self with descriptive naming. ;)
 */

/**
 * Pass your custom function to the wp_rocket_loaded action hook.
 *
 * Note: wp_rocket_loaded itself is hooked into WordPress’ own
 * plugins_loaded hook.
 * Depending what kind of functionality your custom plugin
 * should implement, you can/should hook your function(s) into
 * different action hooks, such as for example
 * init, after_setup_theme, or template_redirect.
 * 
 * Learn more about WordPress actions and filters here:
 * https://developer.wordpress.org/plugins/hooks/
 *
 * @param string 'wp_rocket_loaded'         Hook name to hook function into
 * @param string 'yourprefix__do_something' Function name to be hooked
 */
add_action( 'wp_rocket_loaded', 'yourprefix__do_something' );

/**
 * Define your custom function here.
 * This example just returns true.
 * 
 * @return bool Boolean value TRUE
 */
function yourprefix__do_something() {
	return true;
}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.