Using WordPress ‘mu_plugin_loaded’ PHP action

The mu_plugin_loaded WordPress PHP action is triggered once a single must-use (MU) plugin has loaded.

Usage

add_action('mu_plugin_loaded', 'my_mu_plugin_loaded_callback', 10, 1);

function my_mu_plugin_loaded_callback($mu_plugin) {
    // your custom code here
}

Parameters

  • $mu_plugin (string): Full path to the plugin’s main file.

More information

See WordPress Developer Resources: mu_plugin_loaded

Examples

Logging MU plugin loading

Log the loading of MU plugins in a log file.

add_action('mu_plugin_loaded', 'log_mu_plugin_loaded', 10, 1);

function log_mu_plugin_loaded($mu_plugin) {
    // Log the loaded MU plugin
    error_log("MU plugin loaded: " . $mu_plugin);
}

Display the name of the loaded MU plugin in the admin area.

add_action('mu_plugin_loaded', 'show_mu_plugin_name_in_admin', 10, 1);

function show_mu_plugin_name_in_admin($mu_plugin) {
    if (is_admin()) {
        // Extract the plugin directory name
        $plugin_dir = basename(dirname($mu_plugin));

        // Print the MU plugin name
        echo 'Loaded MU plugin: ' . $plugin_dir . '<br>';
    }
}

Check for a specific MU plugin

Detect if a specific MU plugin has been loaded.

add_action('mu_plugin_loaded', 'check_specific_mu_plugin', 10, 1);

function check_specific_mu_plugin($mu_plugin) {
    $target_plugin = 'my-custom-plugin';

    if (strpos($mu_plugin, $target_plugin) !== false) {
        // Your custom code if the target plugin is loaded
    }
}

Count loaded MU plugins

Count the number of loaded MU plugins.

add_action('mu_plugin_loaded', 'count_mu_plugins', 10, 1);

function count_mu_plugins($mu_plugin) {
    static $plugin_count = 0;

    $plugin_count++;

    // Your custom code using $plugin_count
}

Display a message after a specific MU plugin is loaded

Show a custom message after loading a specific MU plugin.

add_action('mu_plugin_loaded', 'display_message_after_plugin_loaded', 10, 1);

function display_message_after_plugin_loaded($mu_plugin) {
    $target_plugin = 'my-custom-plugin';

    if (strpos($mu_plugin, $target_plugin) !== false) {
        // Display a custom message
        echo 'The ' . $target_plugin . ' MU plugin has been loaded successfully.';
    }
}