keepcalculator

06 Numbers and Powers

How to Create & Install Must-Use Plugins in WordPress

Card No. 06·41436 · Updated · Standard formula, shown below

Must-use (MU) plugins are WordPress plugins that are automatically enabled and cannot be deactivated from the admin dashboard. They are ideal for site-wide functionality that should never be accidentally turned off, such as custom security rules, performance tweaks, or custom post types. This guide shows you how to create an MU plugin and install it correctly, ensuring your code runs on every page and is not subject to user error.

What You’ll Need

  • A WordPress site (self-hosted or managed) with file access via FTP, cPanel, or a hosting file manager.
  • A code editor (like Notepad++, VS Code, or Sublime Text) to write the PHP file.
  • Basic understanding of PHP and WordPress hooks (actions and filters).

Step-by-Step

  1. Create the MU plugin directory. Navigate to your WordPress installation’s wp-content folder. Inside it, check if there is a folder named mu-plugins. If not, create it. This folder is where all must-use plugins reside. The path should be /wp-content/mu-plugins/.
  2. Create a PHP file for your plugin. Inside the mu-plugins folder, create a new file. Name it descriptively, e.g., custom-functions.php. Avoid generic names like functions.php because that conflicts with theme files. Use lowercase letters, numbers, and hyphens.
  3. Add the plugin header. Open the file in your code editor. At the very top, add the standard WordPress plugin header comment. This is required for WordPress to recognize it as a plugin and to display its name in the Must-Use Plugins list. The minimum is the Plugin Name, but it’s good practice to include Description, Version, and Author. Example:
    <?php
    /**
     * Plugin Name: Custom MU Plugin
     * Description: Adds custom functionality to the site.
     * Version: 1.0
     * Author: Your Name
     */
    
  4. Write your custom code. Below the header, add your PHP code. This can be anything from a simple function to a full class. Remember to use WordPress hooks and filters. For example, to add a custom excerpt length:
    function custom_excerpt_length( $length ) {
        return 30;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length' );
    

    Always wrap your code in an if ( ! defined( 'ABSPATH' ) ) exit; // Prevent direct access guard to avoid direct file access.

  5. Save the file. Save the file with UTF-8 encoding (no BOM) to avoid PHP errors. Use .php extension.
  6. Upload the file to your server. Using FTP (like FileZilla) or your hosting file manager, upload the file to the /wp-content/mu-plugins/ directory. Do not upload it to the regular plugins folder.
  7. Verify activation. Go to your WordPress admin dashboard. Navigate to Plugins > Installed Plugins. At the top, you will see a tab or section called Must-Use Plugins. Click on it. Your plugin should be listed there and marked as Active. There is no activation toggle – it is always active.
  8. Test your functionality. Visit your site’s front end and confirm that the custom functionality works as expected. For the excerpt example, check a blog listing page.

Common Mistakes

  • Putting the plugin in the wrong folder. Placing the file in wp-content/plugins makes it a regular plugin, not an MU plugin. It will be deactivatable and might not load if a user deactivates it.
  • Missing the plugin header. Without the Plugin Name header, WordPress will not list the plugin in the Must-Use Plugins list, and it may not load correctly.
  • Using a file name that conflicts with a theme or plugin. For example, naming your file functions.php can cause conflicts. Use a unique, descriptive name.
  • Not using ABSPATH guard. Leaving the file accessible via direct URL can expose your code to security risks. Always add the exit check.
  • Writing code that runs on every page when it should only run on admin pages. MU plugins load on all requests. Use conditional tags like is_admin() or is_front_page() to limit execution if needed.

Tips

  • Use a single file for small tweaks. For simple functions, one file is fine. For larger projects, consider creating a subfolder with includes, but WordPress only loads the main file directly in the mu-plugins folder.
  • Keep a backup. Since MU plugins cannot be deactivated, a syntax error will break your site. Always back up the file before editing, and test changes on a staging site if possible.
  • Use a prefix for function names. To avoid collisions with other plugins or themes, prefix your functions with a unique identifier, e.g., mytheme_custom_function().
  • Consider using a drop-in plugin. If you need to override core WordPress functionality (like wp-cron.php), you can use a drop-in in wp-content, but for most custom code, MU plugins are sufficient.
  • Document your code. Add comments so that future you or other developers understand what each function does.

Troubleshooting

  • Plugin not showing in the list. Check that the file is in the correct directory (wp-content/mu-plugins/) and that the file name ends with .php. Also, verify that the plugin header is correct. If you are using a child theme, the mu-plugins folder is still at the root of wp-content, not inside the theme.
  • Site shows a white screen (PHP fatal error). This usually means a syntax error in your code. Immediately rename the file via FTP (e.g., custom-functions.php.bak) to disable it, then fix the error. If you cannot access FTP, use your hosting file manager. After fixing, rename it back.
  • Function not working. Check that the hook you are using exists and is called at the right time. For example, init runs after the theme is loaded. Also, check for typos in function names or hook names. Use a debugging plugin or enable WP_DEBUG in wp-config.php to see errors.
  • Conflict with another plugin. If you suspect a conflict, temporarily disable other plugins (via the Plugins screen) to see if the issue resolves. Since MU plugins cannot be deactivated, you must rename the file to disable it.
  • Changes not taking effect. Clear any caching plugins (like W3 Total Cache or WP Super Cache) and browser cache. Also, if you are using a CDN, purge the cache there.

Frequently Asked Questions

Can I deactivate a must-use plugin?

No, MU plugins cannot be deactivated from the WordPress admin. To disable one, you must delete the file or rename it (e.g., change the extension to .bak).

Can I use a regular plugin as a must-use plugin?

Yes, you can copy the plugin’s main file into the mu-plugins folder, but it is not recommended because many plugins expect to be in the plugins directory and may have activation/deactivation hooks that will not run. It is better to create a custom MU plugin for your own code.

Do MU plugins load on every page?

Yes, they load on all requests, including admin, front-end, and AJAX calls. This is why they are perfect for site-wide functionality.

Can I put a subfolder with multiple files in mu-plugins?

WordPress only loads PHP files directly in the mu-plugins folder, not in subfolders. However, you can include files from a subfolder using include or require in your main MU plugin file.

Will MU plugins affect site performance?

They can, if they contain heavy code. Since they load on every request, it’s important to keep them lightweight and use WordPress transients for caching expensive operations.

How do I update an MU plugin?

You update it manually by replacing the file on the server. There is no automatic update mechanism for MU plugins, so you must keep track of changes yourself.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →