Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use plugin_loaded action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 5, 2023
5 minutes read

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.

Access Premium WordPress Plugins

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 );
    
Contact UsIf you’re facing any challenges with this hook or need customization, feel free to Contact Us. Our team at WePlugins is here to assist you!

Sandeep Kumar Mishra

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.

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.