Must-use plugins (mu-plugins) in WordPress
Besides normal plugins there is a second folder WordPress loads code from: wp-content/mu-plugins/. Files in it are “must-use” plugins: they are loaded on every request, before the normal plugins and the theme, and cannot be activated or deactivated in the admin.
Problem: Code that must always run (custom post types, security headers, environment settings) lives in functions.php or in a normal plugin, and disappears when somebody switches the theme or deactivates the wrong plugin.
Solution: Put such code into a PHP file in wp-content/mu-plugins/. WordPress loads every PHP file in the root of that folder automatically; files in subfolders need a small loader.
When an mu-plugin is the right place
They are useful for small things that belong to the site, not to the design: registering custom post types and taxonomies (so the content does not vanish with a new theme), environment constants, security headers, disabling features the site does not use. On agency sites they also protect important code from the client clicking around the Plugins screen.
The trade-offs: they have no activation hook, they get no updates from the plugin directory, and the only way to disable one is to remove or rename the file. So keep them small and in version control.
A first mu-plugin
Create the folder if it does not exist and a file wp-content/mu-plugins/site-setup.php:
<?php
/**
* Plugin Name: Site setup
* Description: Post types and settings this site needs regardless of the theme.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'init', function () {
register_post_type( 'event', array(
'label' => 'Events',
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
) );
} );
add_filter( 'xmlrpc_enabled', '__return_false' );
add_action( 'send_headers', function () {
header( 'X-Frame-Options: SAMEORIGIN' );
header( 'X-Content-Type-Options: nosniff' );
header( 'Referrer-Policy: strict-origin-when-cross-origin' );
} );
The header comment is optional; with it the plugin is listed nicely under Plugins → Must-Use.
Subfolders need a loader
Only PHP files directly in mu-plugins/ are loaded. A plugin copied there with its folder (mu-plugins/my-plugin/my-plugin.php) is ignored. Load it from a file in the root:
<?php /** * Plugin Name: MU loader */ require_once WPMU_PLUGIN_DIR . '/security-headers/security-headers.php'; require_once WPMU_PLUGIN_DIR . '/my-plugin/my-plugin.php';
A normal plugin moved into mu-plugins this way loses its activation hook: tables or options it creates on activation are never created. Test such a plugin carefully, or keep it a normal plugin.
Load order
The mu-plugins are loaded in alphabetical order of their file names, then the normal plugins, then the theme. Code in an mu-plugin can therefore not call functions of normal plugins directly when the file is loaded; wrap such calls in a hook such as plugins_loaded or init.
NOTE: Because mu-plugins are not updated from the admin, nobody gets a notification when one has a bug or a security problem. Deploy them like the rest of your code (Git, deployment script) and never edit them on the live server.