Wordpress - Between functions.php (theme), widgets, and plugins, which is loaded first?
The plugins are loaded right before theme (yes, I've been looking for excuse to use this):
However it is wrong to think about either as point of code execution. For most cases everything should be hooked and executed no earlier than init
hook. According to Codex widget registration with register_widget()
should be hooked to widget_init
.
Because of that order of load doesn't matter for this case, you will have everything loaded by the time widget needs it in any case.
One interesting approach would be to list all hooks to a file in a sequence of execution.
add_action( 'all', '_20161224_printer' );
function _20161224_printer( $r ){
$line = microtime(true)*10000 . ' ' . $r . "\n";
$fp = fopen( ABSPATH . 'hooks.txt', 'a+');
fwrite($fp, $line);
fclose($fp);
}
And you will get the output like this:
14825992300742 pre_option_blog_charset
14825992300743 option_blog_charset
14825992300743 plugins_loaded
14825992300744 load_default_widgets
14825992300745 load_default_embeds
14825992300745 wp_audio_extensions
14825992300745 wp_audio_embed_handler
14825992300746 wp_video_extensions
14825992300746 wp_video_embed_handler
14825992300746 sanitize_comment_cookies
14825992300747 pre_option_permalink_structure
14825992300747 option_permalink_structure
14825992300748 pre_option_wp_user_roles
14825992300748 option_wp_user_roles
14825992300749 wp_roles_init
14825992300749 setup_theme
14825992300749 pre_option_template
14825992300750 option_template
14825992300750 template
14825992300750 theme_root
14825992300751 template_directory
14825992300751 pre_option_stylesheet
14825992300751 option_stylesheet
14825992300751 stylesheet
14825992300752 theme_root
14825992300752 stylesheet_directory
14825992300752 pre_option_WPLANG
14825992300753 query
14825992300754 default_option_WPLANG
14825992300755 locale
14825992300755 override_unload_textdomain
14825992300755 unload_textdomain
14825992300755 override_load_textdomain
14825992300756 load_textdomain
14825992300756 load_textdomain_mofile
14825992300756 locale
...
many many more action hooks
...
14825992302886 wp_parse_str
14825992302886 nonce_life
14825992302886 salt
14825992302886 wp_parse_str
14825992302887 esc_html
14825992302887 logout_url
14825992302887 clean_url
14825992302887 gettext
14825992302887 wp_after_admin_bar_render
14825992302888 pre_option_template
14825992302888 option_template
14825992302888 template
14825992302888 theme_root
14825992302888 template_directory
14825992302889 parent_theme_file_path
14825992302889 shutdown
Note the full list simply could not fit the 30.000 characters limitation per WPSO post, so I removed many action hooks.
Put the above code inside a plugin. If you do that from the themes
functions.php
you will not catchplugins_loaded
. One another proof the plugins are loaded before the theme.
The possible goodies of this check are many, but please note the output will be different for different page templates you will call, or if you are in a dashboard.
I simply called this from /?p=1
or Hello World page.
If you don't have a single plugin activated, you may put this code into mu-plugins
folder.
It may be better to use WP FS API, but this way is realy concise.
You can have a widget inside the plugin's .php file (and have a shared global variable which they can both use), if that's what you're asking. Here's a tutorial with sample code I stumbled upon a while back.
Also, here's a list of actions run during a typical request, in order of execution. #2 and #10 suggest that plugins load first; not sure about functions.php.