Customer asks if a specific carousel plugin he uses can be widgetized. That means I should create a widget inside functions.php which calls the plugin's function. That means that the plugin's code has to be loaded first so that the function be available to WordPress when the functions.php file is loaded, right? Would that work?

up vote 164 down vote accepted

The plugins are loaded right before theme (yes, I've been looking for excuse to use this):

enter image description here

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.

  • 3
    WP core load will never make any sense. – gagarine Aug 27 '15 at 13:45
  • 7
    Any chance we can get an updated chart in 2016? ;-) – jgraup Jan 10 '16 at 15:04
  • 8
    @jgraup you say that like things change in WordPress core... :) – Rarst Jan 10 '16 at 21:19
  • 1
    @Rarst, touché ;-) – jgraup Jan 10 '16 at 21:27
  • Just so I'm sure I understand this chart - The sections that extend across more than one of the 3 main columns are common to all? So I'm following a vertical line downward and crossing the events in chronological order for a given request? – Isaac Lubow Oct 11 at 0:30

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 catch plugins_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.

  • Inside wp-admin plugins are loaded after the theme or in the order of the installation... and in front-end plugins are loaded before the theme... – Vishal Kumar Sahu Apr 17 '17 at 17:17

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.

  • 1
    Correct, plugins are loaded before theme files. If the plugin includes code, but not the widget you want, it is possible to define the widget in your theme's functions.php. – goldenapples Aug 22 '11 at 23:47

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.