This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
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.
Example 1: Basic Usage
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);
Example 2: Using a Class
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; } }
Example 3: Removing a Hook
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 );
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.