Comment obtenir la séquence d'exécution des hooks / actions de WordPress?
-
-
Copiepossible de http://wordpress.stackexchange.com/questions/135857/how-to-list-the-hooks-and-order-of-execution-in-current-loading-page comme lamienne a étépubliée le 25février '14Possible duplicate of http://wordpress.stackexchange.com/questions/135857/how-to-list-the-hooks-and-order-of-execution-in-current-loading-page as mine was posted on Feb 25 '14
- 0
- 2015-11-16
- Babu
-
** BACK-END VS FRONT-END **: https://www.lance.bio/2017/10/11/wordpress-hooks-and-filters-order-of-precedence/**BACK-END VS FRONT-END** : https://www.lance.bio/2017/10/11/wordpress-hooks-and-filters-order-of-precedence/
- 0
- 2020-04-01
- T.Todua
-
6 réponses
- votes
-
- 2014-09-29
"Données! Données! Données!" s'écria-t-il avecimpatience. "Jene peuxpasfaire debriques sans argile. "
Alors rassemblons quelques données réelles àpartir d'uneinstallation sanspluginset lethème TwentyTwelve activé avec un seul widget Texte.
Pour lapage d'accueil,les appels
do_action
suivants sonteffectués dans l'ordre suivant:muplugins_loaded registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy registered_post_type registered_post_type registered_post_type registered_post_type registered_post_type plugins_loaded sanitize_comment_cookies setup_theme unload_textdomain load_textdomain after_setup_theme load_textdomain load_textdomain auth_cookie_malformed auth_cookie_valid set_current_user init registered_post_type registered_post_type registered_post_type registered_post_type registered_post_type registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy widgets_init register_sidebar register_sidebar register_sidebar wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_loaded parse_tax_query parse_tax_query posts_selection template_redirect admin_bar_init add_admin_bar_menus get_header wp_head wp_enqueue_scripts wp_print_styles wp_print_scripts get_template_part_content begin_fetch_post_thumbnail_html end_fetch_post_thumbnail_html get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content begin_fetch_post_thumbnail_html end_fetch_post_thumbnail_html get_sidebar dynamic_sidebar_before dynamic_sidebar dynamic_sidebar_after get_footer twentytwelve_credits wp_footer wp_print_footer_scripts wp_before_admin_bar_render wp_after_admin_bar_render shutdown
Si vous souhaitez vérifier l'ordre des actionset combien defois chacune d'ellesest déclenchée,vouspouvez utiliserparexemple:
add_action( 'shutdown', function(){ print_r( $GLOBALS['wp_actions'] ); });
ou cette version sublimée:
add_action( 'shutdown', function(){ foreach( $GLOBALS['wp_actions'] as $action => $count ) printf( '%s (%d) <br/>' . PHP_EOL, $action, $count ); });
pour obtenir la liste suivante:
muplugins_loaded (1) registered_taxonomy (10) registered_post_type (10) plugins_loaded (1) sanitize_comment_cookies (1) setup_theme (1) unload_textdomain (1) load_textdomain (3) after_setup_theme (1) auth_cookie_malformed (1) auth_cookie_valid (1) set_current_user (1) init (1) widgets_init (1) register_sidebar (3) wp_register_sidebar_widget (12) wp_loaded (1) parse_request (1) send_headers (1) parse_tax_query (2) parse_query (1) pre_get_posts (1) posts_selection (1) wp (1) template_redirect (1) wp_default_scripts (1) wp_default_styles (1) admin_bar_init (1) add_admin_bar_menus (1) get_header (1) wp_head (1) wp_enqueue_scripts (1) wp_print_styles (1) wp_print_scripts (1) loop_start (1) the_post (10) get_template_part_content (10) begin_fetch_post_thumbnail_html (2) end_fetch_post_thumbnail_html (2) loop_end (1) get_sidebar (1) dynamic_sidebar_before (1) dynamic_sidebar (1) dynamic_sidebar_after (1) get_footer (1) twentytwelve_credits (1) wp_footer (1) wp_print_footer_scripts (1) admin_bar_menu (1) wp_before_admin_bar_render (1) wp_after_admin_bar_render (1) shutdown (1)
PS: vous devriez également consulter leformidableplugin Query Monitor de John Blackbourn. (Jene suispas lié à ceplugin)
"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."
So let's gather some real data from an install with no plugins and the TwentyTwelve theme activated with only a single Text widget.
For the home page the following
do_action
calls are made in the following order:muplugins_loaded registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy registered_post_type registered_post_type registered_post_type registered_post_type registered_post_type plugins_loaded sanitize_comment_cookies setup_theme unload_textdomain load_textdomain after_setup_theme load_textdomain load_textdomain auth_cookie_malformed auth_cookie_valid set_current_user init registered_post_type registered_post_type registered_post_type registered_post_type registered_post_type registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy registered_taxonomy widgets_init register_sidebar register_sidebar register_sidebar wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_register_sidebar_widget wp_loaded parse_tax_query parse_tax_query posts_selection template_redirect admin_bar_init add_admin_bar_menus get_header wp_head wp_enqueue_scripts wp_print_styles wp_print_scripts get_template_part_content begin_fetch_post_thumbnail_html end_fetch_post_thumbnail_html get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content get_template_part_content begin_fetch_post_thumbnail_html end_fetch_post_thumbnail_html get_sidebar dynamic_sidebar_before dynamic_sidebar dynamic_sidebar_after get_footer twentytwelve_credits wp_footer wp_print_footer_scripts wp_before_admin_bar_render wp_after_admin_bar_render shutdown
If you want to check the order of actions and how many times each one is fired, then you can use for example:
add_action( 'shutdown', function(){ print_r( $GLOBALS['wp_actions'] ); });
or this prettified version:
add_action( 'shutdown', function(){ foreach( $GLOBALS['wp_actions'] as $action => $count ) printf( '%s (%d) <br/>' . PHP_EOL, $action, $count ); });
to get the following list:
muplugins_loaded (1) registered_taxonomy (10) registered_post_type (10) plugins_loaded (1) sanitize_comment_cookies (1) setup_theme (1) unload_textdomain (1) load_textdomain (3) after_setup_theme (1) auth_cookie_malformed (1) auth_cookie_valid (1) set_current_user (1) init (1) widgets_init (1) register_sidebar (3) wp_register_sidebar_widget (12) wp_loaded (1) parse_request (1) send_headers (1) parse_tax_query (2) parse_query (1) pre_get_posts (1) posts_selection (1) wp (1) template_redirect (1) wp_default_scripts (1) wp_default_styles (1) admin_bar_init (1) add_admin_bar_menus (1) get_header (1) wp_head (1) wp_enqueue_scripts (1) wp_print_styles (1) wp_print_scripts (1) loop_start (1) the_post (10) get_template_part_content (10) begin_fetch_post_thumbnail_html (2) end_fetch_post_thumbnail_html (2) loop_end (1) get_sidebar (1) dynamic_sidebar_before (1) dynamic_sidebar (1) dynamic_sidebar_after (1) get_footer (1) twentytwelve_credits (1) wp_footer (1) wp_print_footer_scripts (1) admin_bar_menu (1) wp_before_admin_bar_render (1) wp_after_admin_bar_render (1) shutdown (1)
PS: You should also check out the great Query Monitor plugin by John Blackbourn. (I'm not related to this plugin)
-
Trèsbeaueffectivement!Very nice indeed!
- 0
- 2015-03-19
- jdm2112
-
Merci d'avoirmentionné Query Monitor.Semble être unplugin utile dans ce cas.Thank you for mentioning Query Monitor. Seems to be a useful plugin on this case.
- 0
- 2015-10-16
- D.A.H
-
@kraftnermercipour lamise àjour,j'aitoujoursprévu (maisje l'ai oublié) de créer un lien direct vers l'histoireelle-mêmeen tant que source appropriée,évidemmentma recherche Sherlock Holmesn'étaitpasgéniale à cemoment-là ;-)@kraftner thanks for the update, I always planned (but forgot about it) to link directly to the story itself as a proper source, obviously my Sherlock Holmes search foo wasn't great at that time ;-)
- 0
- 2017-03-01
- birgire
-
J'ai aimé la citationet je voulais voirplus de contexte.Et commej'avais déjà le lienpourmoi,pourquoine pas simplementmettre àjourici aussi.:)I liked the quote and wanted to see more context. And as I already had the link for myself why not just update here as well. :)
- 1
- 2017-03-01
- kraftner
-
Cela devraitfairepartie du codex,je suis sérieux.This should be a part of codex, I'm serious.
- 0
- 2017-07-13
- Jack Johansson
-
heureux d'entendre que cela aide @JackJohanssonglad to hear it helps @JackJohansson
- 0
- 2017-07-13
- birgire
-
Plus de 4 àpublieret toujours utile.Mercibeaucoup!Over 4 yo post and still helpful. Thanks a lot!
- 1
- 2019-02-05
- Sebastian Kaczmarek
-
- 2014-09-29
Here is the WordPress load chart
-
Ajoutez aumoins la [source] (http://wordpress.stackexchange.com/questions/26537/between-functions-php-widgets-and-plugins-which-is-loaded-first/26622#26622),ouencoremieux:trouver un duplicatapour cette question.Add at least the [source](http://wordpress.stackexchange.com/questions/26537/between-functions-php-widgets-and-plugins-which-is-loaded-first/26622#26622), or even better: find a duplicate for this question.
- 9
- 2014-09-29
- fuxia
-
Enfait,je ne savaispas d'oùje venais.J'aienregistré cetteimage surmon PC.Sinon,j'auraisfait ça.Actually I did not know where I got it from. I had this image saved on my PC. Otherwise I would have done that.
- 2
- 2014-09-29
- Robert hue
-
Ilest égalementpublié sur lapage d'accueil de Tom Mc Farlin: Le cycle de vie despages WordPress -> https://tommcfarlin.com/wordpress-page-lifecycle/It's also published on Tom Mc Farlin homepage: The WordPress Page Lifecycle -> https://tommcfarlin.com/wordpress-page-lifecycle/
- 0
- 2015-10-16
- D.A.H
-
- 2017-12-23
Solutiontrouvée!
Merci @birgire pour cettebelle réponse. J'ajouterai à cela,
muplugins_loaded
n'estparfoispas déclenché,doncj'utiliseraiplugins_loaded
commepremier hook (mais à cemoment -l'autorisationn'estpasencoreterminée. Si vous souhaitez vérifier l'autorisation de l'utilisateur,init
est lepremierpour cela) ...p.s.ilexiste d'excellentsplugins:
1) Query Monitor - Vouspouveztout voir ce qui sepasse lors du chargement de lapage,c'est-à-dire la durée de chaquefonctionexécutéeet bienplusencore (voirtoutes les captures d'écran sur lapage duplugin):
2) WP-DEBUG-BAR + < strong> WP-DEBUG-SLOW-ACTIONS :
a) la liste d'exécution des hooks de débogage ( actions ) sur votre site.
b) Voir la durée de chaque action (pas defonction):Found Solution!
Thanks @birgire for nice answer. I will add to that,
muplugins_loaded
sometimes is not fired, so I will useplugins_loaded
as the most first hook (but at that time, user-authorization is not done yet. If you want to check user's authorization, theninit
is the earliest for that)...p.s. there exist excellent plugins:
1) Query Monitor - You can see everything what happens on the page-load, i.e. duration of each executed function and much more(view all screenshots on plugin page):
2) WP-DEBUG-BAR + WP-DEBUG-SLOW-ACTIONS:
a) debug hooks(actions) run list on your site.
b) See duration of each action (not function): -
- 2017-11-24
Iln'y apas deux demandesexactementidentiques.Unmoyen rapide (maistrèsprécis) de savoir ce qui sepasseest d'ajouter temporairement une ligne au début de lafonction
do_action
danswp-includes/plugin.php
quienregistre la$tag
,parexemple:if (isset($some_get_or_post_trigger_var)) file_put_contents(ABSPATH . 'action.log', "$tag\n", FILE_APPEND);
No two requests are exactly the same. A quick and dirty (but very accurate) way to find out what's going on is to temporarily add a line to the start of the
do_action
function inwp-includes/plugin.php
which logs the$tag
, e.g.:if (isset($some_get_or_post_trigger_var)) file_put_contents(ABSPATH . 'action.log', "$tag\n", FILE_APPEND);
-
- 2018-05-28
La séquence debasepeut également êtretrouvée sur la documentation officielle:
The basic sequence can also be found on the official docs:
-
- 2020-05-05
Ceciest similaire à la réponse de @birgire,mais celafournit unbon rapport quipeut être affichépourn'importe quelle URL dans un site WordPress. Vous devrez être connectéen tant qu'utilisateur deniveau Admin,puis ajouter
?wp-hooks
à lafin d'une URL que vous souhaiteztester./** * WordPress Hooks Reference * * Dump all action and filter hooks at the bottom of any page * by adding ?wp-hooks onto the end of the URL while logged-in * as an Administrator level user. */ function kevinlearynet_hooks_reference() { // Only shown for Administrator level users when ?list-wp-hooks is added to the URL $trigger = isset( $_GET['wp-hooks'] ) && current_user_can( 'manage_options' ); if ( ! $trigger ) return; // Capture and sort filters and hooks $filters = array_keys( $GLOBALS['wp_filter'] ); sort( $filters ); $actions = array_keys( $GLOBALS['wp_actions'] ); // Output rough template ob_start(); ?> <section class="wp-hooks"> <h1 class="wp-hooks__h1">WordPress Hooks Reference</h1> <div class="wp-hooks__lists"> <div class="wp-hooks__col"> <h2 class="wp-hooks__h2">Actions</h2> <?php foreach ( $actions as $hook ) : ?> <p class="wp-hooks__hook"><?php echo $hook; ?></p> <?php endforeach; ?> </div> <div class="wp-hooks__col"> <h2 class="wp-hooks__h2">Filters</h2> <?php foreach ( $filters as $hook ) : ?> <p class="wp-hooks__hook"><?php echo $hook; ?></p> <?php endforeach; ?> </div> </div> </section> <style> .wp-hooks { padding: 30px; margin: 30px; border-radius: 4px; background: white; font-size: 16px; line-height: 1.4; height: 50vh; min-height: 500px; overflow-y: scroll; } .wp-hooks__lists { display: flex; } .wp-hooks__col { flex: 1; width: 50%; } .wp-hooks__h1 { margin: 0 0 20px; } .wp-hooks__h2 { line-height: 1; font-size: 18px; margin: 0 0 10px; } .wp-hooks__hook { padding: 0; margin: 0; } </style> <?php ob_end_flush(); } add_action( 'shutdown', 'kevinlearynet_hooks_reference' );
Le résultat ressemble à ceci:
J'ai écrit à ce sujetmoi-même,alors voici la source originale pour référence. Celainclut unpeuplus de détails sur les décisions derrière letriet lafonctionnalité.
This is similar to the @birgire's answer, but it provides a nice report that can be displayed for any URL in a WordPress site. You'll need to be logged-in as an Admin level user, then add
?wp-hooks
onto the end of a URL you want to test./** * WordPress Hooks Reference * * Dump all action and filter hooks at the bottom of any page * by adding ?wp-hooks onto the end of the URL while logged-in * as an Administrator level user. */ function kevinlearynet_hooks_reference() { // Only shown for Administrator level users when ?list-wp-hooks is added to the URL $trigger = isset( $_GET['wp-hooks'] ) && current_user_can( 'manage_options' ); if ( ! $trigger ) return; // Capture and sort filters and hooks $filters = array_keys( $GLOBALS['wp_filter'] ); sort( $filters ); $actions = array_keys( $GLOBALS['wp_actions'] ); // Output rough template ob_start(); ?> <section class="wp-hooks"> <h1 class="wp-hooks__h1">WordPress Hooks Reference</h1> <div class="wp-hooks__lists"> <div class="wp-hooks__col"> <h2 class="wp-hooks__h2">Actions</h2> <?php foreach ( $actions as $hook ) : ?> <p class="wp-hooks__hook"><?php echo $hook; ?></p> <?php endforeach; ?> </div> <div class="wp-hooks__col"> <h2 class="wp-hooks__h2">Filters</h2> <?php foreach ( $filters as $hook ) : ?> <p class="wp-hooks__hook"><?php echo $hook; ?></p> <?php endforeach; ?> </div> </div> </section> <style> .wp-hooks { padding: 30px; margin: 30px; border-radius: 4px; background: white; font-size: 16px; line-height: 1.4; height: 50vh; min-height: 500px; overflow-y: scroll; } .wp-hooks__lists { display: flex; } .wp-hooks__col { flex: 1; width: 50%; } .wp-hooks__h1 { margin: 0 0 20px; } .wp-hooks__h2 { line-height: 1; font-size: 18px; margin: 0 0 10px; } .wp-hooks__hook { padding: 0; margin: 0; } </style> <?php ob_end_flush(); } add_action( 'shutdown', 'kevinlearynet_hooks_reference' );
The output looks like this:
I wrote about this on my own, so here's the original source for reference. This includes a little more detail on decisions behind sorting and functionality.
Dans quel ordre les hooks
add_action
s'exécutent?c'est-à-dire
MODIFIER:
J'ai égalementpubliéma solution.