As per my understanding, functions attached to plugin_loaded and admin_init hooks are called whenever any admin page is loaded. What's the difference between them?
1 Answer
plugins_loaded
fires once activated plugins have loaded. This fires on both admin and public screens.
admin_init
fires as an admin screen or script is being initialized. This fires only on admin screens.
The typical order for firing of hooks on the admin screen is:
- muplugins_loaded - this is the first hook available to must-use plugins
- registered_taxonomy
- registered_post_type
- plugins_loaded - this is the first hook available to regular plugins
- auth_cookie_valid
- set_current_user
- load_textdomain
- sanitize_comment_cookies
- setup_theme
- unload_textdomain
- after_setup_theme - this is the first hook available to themes
- init
- widgets_init
- register_sidebar
- wp_register_sidebar_widget
- wp_default_styles
- wp_default_scripts
- debug_bar_enqueue_scripts
- wp_loaded - This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
- auth_redirect
- admin_menu
- pre_get_users
- pre_user_query
- admin_init
- ... lots of other stuff
As you can see, a lot happens between plugins_loaded
and admin_init
.
Hooks on the public side are similar to those above, with the notable absence of admin_init
.