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.
Making a Custom MU Plugin
- 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. - Using a basic text editor create a new file and put some code in it.
- Save the file with whatever name you want using only latin characters, numbers, and dashes in your file name.
Example file name: wp-imonweb-custom.php - Upload this file to the mu-plugins folder created in step 1.
- 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 Imonweb, always wrap it in a condition like this:
if ( function_exists( 'some_wp_imonweb_function' ) ) { // do something with some_wp_imonweb_function() here }
- Again, make sure to deactivate and re-activate WP Imonweb 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 Gistinstead 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’ 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_imonweb_loaded action hook. * * Note: wp_imonweb_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_imonweb_loaded' Hook name to hook function into * @param string 'yourprefix__do_something' Function name to be hooked */ add_action( 'wp_imonweb_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; }