How to Create and Install Must-Use Plugins in WordPress: A web developer uses laptop to write code

How to Create and Install Must-Use Plugins in WordPress

Must-use plugins are a type of WordPress plugin that remains permanently enabled and cannot be deactivated from the admin dashboard. Often abbreviated as “mu-plugins,” they’re used to ensure critical site-wide modifications or features are always activated.

In this article, we’ll discuss some of the use cases for mu-plugins and show you how to build one for your WordPress site.

What are mu-plugins? 

Before we go any further, let’s clarify what exactly mu-plugins are.

As noted above, mu-plugins are always active on your WordPress site, but what truly sets them apart is how WordPress loads and manages them. 

Unlike standard plugins, which you manage through the admin dashboard, mu-plugins are automatically included during the WordPress initialization process. They load earlier in the sequence, giving them priority over regular plugins. 

This early execution is essential for enforcing critical functionality, such as maintaining security protocols or ensuring specific features remain active without risk of accidental deactivation. 

Mu-plugins also differ in how they’re set up. Instead of residing in the general plugins folder, they’re stored in a dedicated directory (wp-content/mu-plugins). They don’t require activation, nor do they show up in the WordPress admin interface for updates or deactivation. 

This design makes them a powerful tool for developers who want to lock in essential functionality while minimizing risks from user errors or misconfigurations, ensuring site reliability and security.

Mu-plugin use cases

There are a lot of situations where mu-plugins are useful, typically in cases where you want your WordPress site to include custom code that can’t be easily disabled, at least by accident.

Normal WordPress plugins can usually be deactivated with a single mouse click from within the WordPress admin. Deactivating a mu-plugin requires that you first have access to wp-content/mu-plugins, and then manually deleting the file. 

The specific use case for your mu-plugin will vary depending on your site and what you’re trying to achieve. The list below is by no means exhaustive, but it gives you some idea of the most common use cases: 

1. Security: Mu-plugins can be used to implement security measures that cannot be disabled by users, even those with admin-level access. 

2. Performance: Mu-plugins can be used to optimize performance by loading critical functionality early in the WordPress loading process.

3. Customization: Mu-plugins can be used to implement custom functionality that is specific to a particular WordPress installation. 

4. Portability: Using mu-plugins for custom code enhances portability because the code is not tied to a specific theme. Custom code added to the functions.php file of a theme can be lost if the theme is updated or changed, but mu-plugins remain active regardless of theme changes. This ensures that critical custom functionalities are preserved across different themes and updates.

5. Multisite networks: Mu-plugins are particularly useful in multisite networks, where they can be used to implement network-wide functionality.

6. Host-specific features: Your host may install a mu-plugin on your WordPress site to activate certain features specific to their services. 

How to create a mu-plugin

By default, mu-plugins are located in the wp-content/mu-plugins directory of your WordPress installation. You can change this directory by defining the WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL attributes in your wp-config.php file.

There are some best practices and caveats you should be aware of before you create your mu-plugin. 

Best practices

1. Use a unique filename: Use a unique filename for your mu-plugin to avoid conflicts with other plugins. Remember that mu-plugins are loaded alphabetically by file name. This may be important if you have multiple mu-plugins.

2. Keep it simple: Keep your mu-plugin code simple and focused on a specific task.

3. Test thoroughly: Test your mu-plugin thoroughly to ensure it does not cause any issues with your WordPress installation. We recommend testing in a staging environment or with a tool like Local before rolling it out to your production site.

Caveats

1. No activation hooks: Mu-plugins cannot use activation hooks, which are commonly used by regular plugins to initialize or set up the plugin. This means any setup code that relies on these hooks will not be executed for mu-plugins.

2. Update notifications: Mu-plugins do not appear in the update notifications list in the WordPress admin dashboard, even if you’re using a mu-plugin created by someone else. You will need to make any updates manually. 

3. Security: Since mu-plugins are always active and cannot be deactivated through the WordPress admin, it is crucial to use trusted and secure mu-plugins to avoid introducing malware or errors that could affect the entire site or network.

Creating a mu-plugin for WordPress: A step-by-step tutorial

In this tutorial, we’ll create a simple mu-plugin that adds a custom footer message to the WordPress admin dashboard. We’ll cover the basics of mu-plugin development, including creating the plugin file, adding functionality, and testing the plugin.

Step 1: Create the mu-plugin file

To create a mu-plugin, you’ll need to create a PHP file in the wp-content/mu-plugins directory of your WordPress installation. If the mu-plugins directory doesn’t exist, create it. 

Using your favorite code editor or IDE, create a new file called custom-footer-message.php in the wp-content/mu-plugins directory.

It’s possible to use a text editor to write PHP files by switching the file extension from .txt to .php. This will work, but we strongly recommend against it. There are code editors available for free, such as VSCode, with features like syntax highlighting, auto-completion, and error checking, which can make coding more efficient and less prone to errors.

Step 2: Add the plugin header

In WordPress, plugins typically include a header that provides information about the plugin, such as its name, description, and version. While mu-plugins don’t require a header, it’s a good practice to include one to provide context about the plugin.

Add the following code to the top of the `custom-footer-message.php` file:

<?php
/*
Plugin Name: Custom Footer Message
Description: Adds a custom footer message to the WordPress admin dashboard
Version: 1.0
*/

Step 3: Add the plugin functionality

In this example, we’ll add a custom footer message to the WordPress admin dashboard. We’ll use the admin_footer_text filter to modify the footer text.

Add the following code to the custom-footer-message.php file:

function custom_footer_message() {
  return 'This is a custom footer message!';
}
add_filter( 'admin_footer_text', 'custom_footer_message' );

This code defines a function called custom_footer_message that returns the custom footer message. We then use the add_filter function to hook into the admin_footer_text filter and modify the footer text.

Step 4: Test the plugin

To test the plugin, simply save the custom-footer-message.php file and refresh the WordPress admin dashboard. You should see the custom footer message displayed at the bottom of the page.

Screenshot of part of the WordPress admin, with "This is a custom footer message!" displayed in the footer.

Conclusion

Mu-plugins offer a powerful way to extend and customize your WordPress site. However, it’s essential to use them responsibly and with caution, as they can drastically change the way your WordPress site functions. This is particularly true for multisite networks, as they can be used to implement network-wide functionality, with impacts cascading down to the individual sites. 

Always test your mu-plugins thoroughly, and consider using a version control system such as git to manage your code and track changes.

Find more content like this on WP Engine’s Resource Center and visit wpengine.com to learn more about hosting your sites on the most trusted platform for WordPress. 
 

Get started

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.