plugins_loaded
Fires immediately after all active plugins have loaded.
It fires early, before the following events: setup_theme, after_setup_theme, init, and wp_loaded.
It is normally used to initialize a plugin's main code. This allows the plugin code to run after WordPress pluggable functions and other plugins have loaded. Those plugins may in turn modify some WordPress functions.
New hooks in WP 5.1
WordPress has the plugins_loaded, network_plugins_loaded, and mu_plugins_loaded hooks, but they fire only after all plugins have loaded. There was no easy way to run arbitrary code between individual plugin loads or only after plugin X loaded, which made monitoring and debugging the loading of a particular plugin difficult.
WordPress 5.1 introduced hooks that fire after each plugin loads. Each hook passes the path to the plugin's main file.
IMPORTANT: The plugins_loaded hook cannot be replaced with plugin_loaded; they serve different purposes.
Usage
add_action( 'plugins_loaded', 'wp_kama_plugins_loaded_action' );
/**
* Function for `plugins_loaded` action-hook.
*
* @return void
*/
function wp_kama_plugins_loaded_action(){
// action...
}
Examples
#1 Initialize a plugin
Suppose a plugin needs to retrieve the current user and perform some work with it. The wp_get_current_user() function provides the user. However, calling it directly in the plugin file without a hook does not work because the function is loaded only after all active plugins, including this one. Calling it directly would therefore occur before the function is defined and cause a fatal PHP "undefined function" error.
The plugins_loaded event is intended for this and similar cases. A short example:
<?php
/*
Plugin Name: Plugin Name
Plugin URI: http://plugin_description_and_updates_page
Description: A short plugin description.
Version: Plugin version number, for example: 1.0
Author: Plugin author name
Author URI: http://plugin_author_page
*/
// Actions unrelated to the plugin's main operation.
// For example, set the plugin's table names in the global $wpdb object.
global $wpdb;
$wpdb->mytable = $wpdb->prefix .'mytable';
// Or include files.
require_once plugin_dir_path( __FILE__ ) .'/upgrade.php';
// Now begin the plugin's main work by loading its code during plugins_loaded.
add_action( 'plugins_loaded', 'my_plugin_init' );
function my_plugin_init() {
$cuser = wp_get_current_user();
// If the user has the 'newrole' role.
if( in_array( 'newrole', (array) $cuser->roles ) ){
// Do something for this role.
}
else{
// Do something for the other roles.
}
}Changelog
| Since 1.5.0 | Introduced. |
Where the hook is called
do_action( 'plugins_loaded' );
Where the hook is used in WordPress
add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 );
add_action( 'plugins_loaded', 'wp_maybe_load_embeds', 0 );
add_action( 'plugins_loaded', '_wp_customize_include' );
add_action( 'plugins_loaded', 'wp_initialize_theme_preview_hooks', 1 );
add_action( 'plugins_loaded', '_wp_add_additional_image_sizes', 0 );