96

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?

3
  • Hola consulta para llamar una api externa en que parte de wordpress lo tendria que hacer? gracias Commented Nov 24, 2020 at 23:51
  • Hi - you've posted this as an answer to an existing question, whereas it's really a new question. Could you ask this as a new question please? This site is in English but there's also Stack Overflow en español.
    – Rup
    Commented Nov 24, 2020 at 23:59
  • But the broad answer I think is that you can call out to an external API from anywhere in a plugin or theme. You should generally cache the response though and avoid long-running requests if you can. If you do reask this question please provide more details about the API you're calling out to and what you want to do. Thanks!
    – Rup
    Commented Nov 25, 2020 at 0:01

3 Answers 3

224

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.

6
  • 4
    WP core load will never make any sense.
    – gagarine
    Commented Aug 27, 2015 at 13:45
  • 11
    Any chance we can get an updated chart in 2016? ;-)
    – jgraup
    Commented Jan 10, 2016 at 15:04
  • 13
    @jgraup you say that like things change in WordPress core... :)
    – Rarst
    Commented Jan 10, 2016 at 21:19
  • 5
    @Rarst, touché ;-)
    – jgraup
    Commented Jan 10, 2016 at 21:27
  • 2
    @IsaacLubow yes, the things in more than one column run for those respective contexts, as you can see the same significant chunk of core load is always involved for any request.
    – Rarst
    Commented Oct 15, 2018 at 11:13
19

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.

1
  • 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... Commented Apr 17, 2017 at 17:17
4

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
  • 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. Commented Aug 22, 2011 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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