Using WordPress ‘plugin_loaded’ PHP action

The plugin_loaded WordPress PHP action fires once a single activated plugin has loaded.

Usage

add_action('plugin_loaded', 'my_custom_plugin_loaded');
function my_custom_plugin_loaded($plugin) {
    // Your custom code here
    return $plugin;
}

Parameters

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

More information

See WordPress Developer Resources: plugin_loaded

Examples

Change plugin text domain

Load a different text domain for a plugin.

add_action('plugin_loaded', 'change_plugin_text_domain');
function change_plugin_text_domain($plugin) {
    if (strpos($plugin, 'example-plugin/example-plugin.php') !== false) {
        load_plugin_textdomain('new-text-domain', false, dirname($plugin) . '/languages/');
    }
}

Add a custom admin notice

Add a custom admin notice after a specific plugin is loaded.

add_action('plugin_loaded', 'my_custom_admin_notice');
function my_custom_admin_notice($plugin) {
    if (strpos($plugin, 'example-plugin/example-plugin.php') !== false) {
        add_action('admin_notices', 'my_admin_notice_function');
    }
}

function my_admin_notice_function() {
    echo '<div class="notice notice-info is-dismissible"><p>' . __('My custom notice!', 'text-domain') . '</p></div>';
}

Load additional files for a plugin

Load extra files after a specific plugin is loaded.

add_action('plugin_loaded', 'load_additional_files');
function load_additional_files($plugin) {
    if (strpos($plugin, 'example-plugin/example-plugin.php') !== false) {
        require_once plugin_dir_path($plugin) . 'includes/extra-functions.php';
    }
}

Set a custom default option for a plugin

Set a custom default option value when a specific plugin is loaded.

add_action('plugin_loaded', 'set_custom_default_option');
function set_custom_default_option($plugin) {
    if (strpos($plugin, 'example-plugin/example-plugin.php') !== false) {
        if (get_option('custom_option_name') === false) {
            update_option('custom_option_name', 'default_value');
        }
    }
}

Add custom CSS for a plugin

Enqueue custom CSS styles for a specific plugin.

add_action('plugin_loaded', 'enqueue_custom_plugin_styles');
function enqueue_custom_plugin_styles($plugin) {
    if (strpos($plugin, 'example-plugin/example-plugin.php') !== false) {
        add_action('wp_enqueue_scripts', 'enqueue_my_custom_styles');
    }
}

function enqueue_my_custom_styles() {
    wp_enqueue_style('custom-plugin-styles', plugins_url('/css/custom-plugin-styles.css', __FILE__));
}