Using WordPress ‘plugins_loaded’ PHP action

The mu_plugin_loaded WordPress PHP action fires once a single must-use plugin has loaded.

Usage

add_action('mu_plugin_loaded', 'my_custom_function');
function my_custom_function($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

Log when a must-use plugin is loaded

Logs a message when a must-use plugin is loaded.

add_action('mu_plugin_loaded', 'log_mu_plugin_loaded');
function log_mu_plugin_loaded($mu_plugin) {
    error_log("Must-use plugin loaded: " . $mu_plugin);
}

Display a message when a specific must-use plugin is loaded

Shows a message in the admin dashboard when a specific must-use plugin is loaded.

add_action('mu_plugin_loaded', 'display_message_for_specific_plugin');
function display_message_for_specific_plugin($mu_plugin) {
    if (strpos($mu_plugin, 'my-specific-plugin.php') !== false) {
        add_action('admin_notices', 'my_specific_plugin_notice');
    }
}

function my_specific_plugin_notice() {
    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>**My Specific Plugin** is loaded and working!</p>';
    echo '</div>';
}

Modify the behavior of a must-use plugin

Runs custom code only when a specific must-use plugin is loaded.

add_action('mu_plugin_loaded', 'modify_mu_plugin_behavior');
function modify_mu_plugin_behavior($mu_plugin) {
    if (strpos($mu_plugin, 'mu-plugin-to-modify.php') !== false) {
        // your custom code here
    }
}

Deactivate a must-use plugin if a condition is met

Deactivates a must-use plugin if a certain condition is met.

add_action('mu_plugin_loaded', 'conditionally_deactivate_mu_plugin');
function conditionally_deactivate_mu_plugin($mu_plugin) {
    if (strpos($mu_plugin, 'mu-plugin-to-deactivate.php') !== false && some_condition()) {
        remove_action('init', 'mu_plugin_to_deactivate_init_function');
    }
}

function some_condition() {
    // return true or false based on your condition
    return true;
}

Perform a task when all must-use plugins are loaded

Performs a task when all must-use plugins are loaded using the plugins_loaded action.

add_action('plugins_loaded', 'task_after_all_mu_plugins_loaded');
function task_after_all_mu_plugins_loaded() {
    // your custom code here
}