Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.
plugin_loaded action is a super handy WordPress hook that fires once a single activated plugin has loaded. You’ll want to register it using add_action, and this can be done either in your theme’s functions.php file or a custom WordPress plugin. WePlugins always suggests using a custom plugin to avoid any issues when updating your theme.
For instance, we can define a function, execute_on_plugin_loaded_event, which we register with add_action. The parameters are straightforward: the hook name, the function to call, the priority, and the number of arguments.
And if you ever need to remove a hook, simply use remove_action.
Here’s an example of how you can implement this hook in your WordPress setup.
function weplugins_execute_on_plugin_loaded_event($plugin){
// Implement your custom functionality here.
}
add_action( "plugin_loaded", "weplugins_execute_on_plugin_loaded_event" , 10, 1);
Sometimes, you might want to organize your code using a class. Here’s how you can do it:
add_action( 'plugin_loaded', array( 'weplugins_wpdocs_class_name', 'instance' ) );
class weplugins_wpdocs_class_name {
private function __construct() {
self::init();
}
public static function instance() {
static $instance = null;
}
}
To remove a hook callback, ensure you provide the same callback function name, priority, and number of arguments.
remove_action( "plugin_loaded", "weplugins_execute_on_plugin_loaded_event", 10, 1 );
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.