Kishan Jasani

Code, Creativity & Everything I Learn

How to create custom MU plugin?

MU Plugin in WordPress

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:

  1. Navigate to your WordPress installation.
  2. Open the wp-content directory.
  3. Inside wp-content, create a new directory named mu-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:

  1. Inside the mu-plugins folder, create a new PHP file. Let’s name it xyz.php.
  2. 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:

  1. Log in to your WordPress dashboard.
  2. Navigate to Plugins > Must-Use.
  3. 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

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