How to make a WordPress mu-plugin

Creating a “mu-plugin” (must-use plugin) for WordPress effectively ensures critical functionalities are always active on your website. This plugin is automatically activated and cannot be disabled through the WordPress dashboard, making it ideal for essential features or customizations.

Another use case for a mu-plugin is to add extra functions to a plugin or the WP core. For instance, WP Staging has many filters and hooks that can provide more features to the WP Staging staging or backup feature. All of these documented filters can be added to a custom mu-plugin.

Introduction to Must-Use Plugins

Mu-plugins, or “must-use” plugins, are a special type of WordPress plugin. They are stored in a separate directory (wp-content/mu-plugins) and are automatically loaded by WordPress. These plugins are ideal for code that must always be executed, like custom functions for a website, and they are immune to accidental deactivation.

Advantages of Mu-Plugins

  1. Automatic Activation: Mu-plugins are automatically activated and cannot be accidentally deactivated.
  2. Enhanced Security: Since they can’t be deactivated from the admin panel, they offer a more secure way to implement critical functionalities.
  3. Ease of Management: Ideal for managing multiple WordPress sites, as it simplifies the maintenance of essential plugins.

Make a Mu-Plugin

1. Access Your Site’s Files

  • Access your website’s files through FTP or your web host’s file manager.

2. Locate the Mu-Plugin Directory

  • Navigate to the wp-content directory.
  • Check for the mu-plugins folder. If it doesn’t exist, create it.

3. Create Your Mu-Plugin File

  • Create a new PHP file for your mu-plugin. For example, my-mu-plugin.php.
  • Use a simple text editor to write your PHP code.

4. Add Code to Your Mu-Plugin

  • Your mu-plugin should start with a PHP opening tag <?php.
  • Below is the basic structure of a mu-plugin file:
PHP
<?php
/*
Plugin Name: My Custom Mu-Plugin
Description: A custom must-use plugin to enhance my WordPress site.
Version: 1.0
Author: Mickey Mouse
*/

// Your custom code goes here

Step 5: Upload Your Mu-Plugin

  • Upload the .php file to the mu-plugins directory on your server.

Step 6: Verify Activation

  • Since mu-plugins are automatically activated, check your site to ensure your custom code works.

Sample Mu-Plugin: Custom Admin Footer Text

For example, let’s create a simple mu-plugin that changes the footer text in the WordPress admin area.

PHP
<?php
/*
Plugin Name: Custom Admin Footer
Description: Changes the footer text in the WordPress admin area.
Version: 1.0
Author: Your Name
*/

add_filter('admin_footer_text', function () {
    echo 'Customized by Tony Stark - Powered by WordPress';
});

Conclusion

Creating a mu-plugin is a straightforward process that can greatly enhance the functionality and security of your WordPress site. By following the above steps, you can implement always active customizations, providing a consistent and reliable experience for your site.

Remember to test your mu-plugins thoroughly on a staging environment before deploying them to a live site to ensure they work as expected.