In a plugin, why is add_action(‘init’) not executed before the plugin is activated?

WordPress is an application and everything that is run within that application is called by hooks.

Then 2 main hook functions are add_action and add_filter, but as you saw, register_activation_hook and register_deactivation_hook are other functions that are used in the context of activation and deactivation.

There exist more hook defining functions, like functions that will hook time sensitive actions (pseudo cron) like wp_schedule_event for instance.

The codex offers a good list of available action hooks (ordered by their generaly called sequence) to be used with add_actions and filters to be used with add_filter.

I am not aware of such a list for all hooks defining functions.

Like I said in my comment, most of these functions are simply intelligent wrappers for PHP’s call_user_func functions that give context to these hooks.

So when a plugin or theme is installed, it won’t automatically run even though their files would technically be read (at least the main files are).

In the main file of a plugin or theme, you will find a comment section defining the parameter of the plugin/theme, such as theme name, author, version, etc.

In themes, the style.css file would be read and would contain something similar to this

/*
 Theme Name: Twenty Thirteen
 Theme URI: http://wordpress.org/themes/twentythirteen
 Author: the WordPress team
 Author URI: http://wordpress.org/
 Description: The 2013 theme for WordPress takes us back to the blog, featuring a full range of post formats, each displayed beautifully in their own unique way. Design details abound, starting with a vibrant color scheme and matching header images, beautiful typography and icons, and a flexible layout that looks great on any device, big or small.
 Version: 1.0
 License: GNU General Public License v2 or later
 License URI: http://www.gnu.org/licenses/gpl-2.0.html
 Tags: black, brown, orange, tan, white, yellow, light, one-column, two-columns, right-sidebar, flexible-width, custom-header, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-language-support, sticky-post, translation-ready
 Text Domain: twentythirteen

 This theme, like WordPress, is licensed under the GPL.
 Use it to make something cool, have fun, and share what you've learned with others.
*/

Similarly, in a plugin’s main PHP file, you would find a comment section containing something similar to this

/*
 Plugin Name: My Toolset
 Plugin URI:  http://URI_Of_Page_Describing_Plugin_and_Updates
 Description: This describes my plugin in a short sentence
 Version:     1.5
 Author:      John Smith
 Author URI:  http://URI_Of_The_Plugin_Author
 License:     GPL2
 License URI: https://www.gnu.org/licenses/gpl-2.0.html
 Domain Path: /languages
 Text Domain: my-toolset
*/

Finally, once a theme/plugin is activated, then it will go on loading the rest of it’s files following WP’s hook loading sequence and load everything that the developers hooked to these action hooks or filters.

And in those developer’s created functions, all the magic happens for that particular plugin or theme.

A pretty comprehensive and worth reading explanation of WP load sequence was made by userabuser here.

Leave a Comment