WordPress offers a powerful way to extend functionality using plugins. While standard plugins require activation, Must-Use (MU) plugins are automatically enabled once placed in the designated folder. MU plugins are ideal for implementing essential features that should always be active, such as security enhancements, performance tweaks, or custom functionalities.
In this guide, I’ll walk you through the steps to create your own MU plugin in WordPress.
Step 1: Create the MU Plugins Directory
By default, WordPress does not include a directory for MU plugins. To set it up, follow these steps:
- Navigate to your WordPress installation.
- Open the
wp-content
directory. - Inside
wp-content
, create a new directory namedmu-plugins
.
Step 2: Add a PHP File for Your Plugin
Now that you have the mu-plugins
directory, it’s time to add your custom plugin file:
- Inside the
mu-plugins
folder, create a new PHP file. Let’s name itxyz.php
. - Open
xyz.php
in a code editor and add the following comment at the top to define it as an MU plugin:
<?php
/**
* Plugin Name: XYZ MU Plugin
* Description: A custom must-use plugin for WordPress.
* Author: Kishan Jasani
* Version: 1.0
*/
// Your custom code goes here
Step 3: Add Your Custom Functionality
You can now add any functionality you need inside the xyz.php
file. Since MU plugins load before standard plugins, they are useful for defining global settings, loading custom features, or overriding default behaviors.
Example: Disable Admin Bar for Non-Admins
Here’s an example of a simple function that hides the WordPress admin bar for non-admin users:
add_action( 'after_setup_theme', function() {
if ( ! current_user_can( 'administrator' ) ) {
show_admin_bar( false );
}
} );
Step 4: Verify Your MU Plugin
Unlike regular plugins, MU plugins do not appear in the standard Plugins menu in the WordPress admin panel. Instead, they can be found under Plugins > Must-Use.
To confirm that your plugin is active:
- Log in to your WordPress dashboard.
- Navigate to Plugins > Must-Use.
- Your
XYZ MU Plugin
should be listed there and automatically activated.
Creating a Must-Use plugin in WordPress is a great way to implement critical functionality that remains active without user intervention. By following these simple steps, you can develop custom MU plugins tailored to your needs.
Leave a Reply