Différence entre do_action et add_action
-
-
Que sepasse-t-il lorsque vous appelez `do_action ('custom');` ** après ** vous avezenregistré le rappel?Et assurez-vous quejQueryest chargé.What happens when you call `do_action('custom');` **after** you registered the callback? And make sure jQuery is loaded.
- 0
- 2013-10-28
- fuxia
-
jen'aipas reçu lemessage d'alerteni sur leback-endni sur lefront-endi did not get the alert message either on back end or front end
- 0
- 2013-10-28
- sun
-
jQuery sont chargés.Quandj'accroche lamêmefonction avec `wp_head`,celafonctionnebien maispas avec le`personnalisé`jQuery are loaded. When i hook the same function with `wp_head` it is working fine but not with the `custom`
- 0
- 2013-10-28
- sun
-
Sun,je crois que votre questioninitiale a été réponduepartoschoet vous devriez lamarquer comme résolue.Lesmodificationsn ° 3et n ° 4 que vous avezeffectuées appartiennent à une [nouvelle question] (http://wordpress.stackexchange.com/questions/ask).Sun, I believe your original Question was answered by toscho and you should mark this as resolved. The edits #3 an #4 that you've done belong on a [new Question](http://wordpress.stackexchange.com/questions/ask).
- 0
- 2013-10-30
- brasofilo
-
4 réponses
- votes
-
- 2013-10-28
Utilisez
do_action( 'unique_name' )
pour créer vospropres actions.Vouspouvez l'utiliserpourproposer une APIpour votreplugin ,afin que d'autrespluginspuissentenregistrer des rappelspour votreaction.Exemple: Dois-je appeler do_action dansmonplugin?
Mais vouspouvez également utiliser des actionspersonnalisées (ou desfiltres) dans unthème.Exemple: Meilleurepratiquepourimplémenter des sectionspersonnalisées dans unthème WordPress
Et vouspouvez combiner les deuxpour créer unpluginet unthèmefonctionnantensemble.Exemple: Comment rendre laméthode duplugin disponible dans lethème?
Résumé:
add_action( 'foo' )
enregistre un rappel,do_action( 'foo' )
exécute ce rappelenregistré.Use
do_action( 'unique_name' )
to create your own actions.You can use that to offer an API for your plugin, so other plugins can register callbacks for your custom action. Example: Do I need to call do_action in my plugin?
But you can use custom actions (or filters) in a theme too. Example: Best practice way to implement custom sections into a WordPress theme
And you can combine both to make a plugin and a theme working together. Example: How to make method from plugin available in theme?
Summary:
add_action( 'foo' )
registers a callback,do_action( 'foo' )
executes that registered callback.-
Mercipour l'aide quej'ai obtenue,c'estprincipalement à desfins d'API.J'aiessayé un échantillon,mais celan'apasfonctionné. Pouvez-voustrouver unproblème?Vérifiezmon EDIT @toschoThanks for the help i got this is mainly for API purpose. I tried a sample but it didn't work out.Can you find any problem there? Check my EDIT @toscho
- 0
- 2013-10-28
- sun
-
Pouvons-nous dire cela comme un autre cas comme l'héritage?au lieu d'étendreet de remplacer la classe,nous allons dans cette direction.Est-ce correct?Can we say this as another case like inheritance? intead of extending and overriding the class we are going this way. Is this correct?
- 0
- 2013-10-28
- sun
-
@sun Hm,oui,mais vouspouvez [combiner les deux approches] (http://wpkrauts.com/2013/initialize-a-plugin-with-a-configuration-object/),ellesne s'excluentpasmutuellement.@sun Hm, yes, but you can [combine both approaches](http://wpkrauts.com/2013/initialize-a-plugin-with-a-configuration-object/), they are not mutually exclusive.
- 0
- 2013-10-28
- fuxia
-
- 2016-02-14
C'estma supposition,donc si vous savezmieux,veuillezfaire un commentaire afin queje puissemettre àjourma supposition.
Le code de votrepluginestexécuté avant
wp_head ()
(qui,nouspouvons supposer,appellera les actions qui lui sont ajoutées). Lorsque vousadd_action ('wp_head','custom_register')
,vous dites à PHP que lorsque (dans lefutur)do_action ('wp_head')
est appelé,pour appeler < code> custom_register () également. Ilen va demêmepour votre appel àadd_action ('custom','custom_register')
mais comme vous le voyez dans votre code,l'appel àdo_action ('custom')
a déjà étéfait,et quandil a été appelé,iln'y avait (encore) aucune action ajoutée. C'estpourquoi Toscho a demandé ce qui sepasse lorsque vous appelezdo_action ('custom')
après avoirenregistré le rappel. Votre réponse sur leback-endet lefront-endest ambiguë. Si vouspermutez les deux dernières lignes du code suivant,je pense que celafonctionnera:fonction custom_register () { echo '& lt; script >jQuery (document) .ready (function () {alert ("Learning Hooks");}); & lt;/script >'; } do_action ('personnalisé');//Ceciest appelé avant d'avoir uneffet. add_action ('personnalisé','enregistrement_personnalisé');//Troptard - do_action a déjà été appelé.
This is my guess, so if you know better, please make a comment so I can update my guess.
Your plugin code is executed before
wp_head()
(which we can assume will invoke the actions added to it). When youadd_action('wp_head','custom_register')
, you are telling PHP that when (in the future)do_action('wp_head')
is called, to callcustom_register()
as well. The same is true of your call toadd_action('custom','custom_register')
but as you see in your code, the call todo_action('custom')
has already been made, and when it was called, there was not (yet) any action added to it. This is why Toscho asked what happens when you calldo_action('custom')
after you registered the callback. Your answer about back end and front end is ambiguous. If you swap the last two lines in the following code, I think it will work:function custom_register() { echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>'; } do_action('custom'); // This is called before it will have an effect. add_action('custom','custom_register'); // Too late - do_action was already called.
-
- 2018-04-19
do_action
:enregistre un crochet d'action pendantadd_action
: ajoute unefonction de rappel au hookenregistré.Considérez que vous vouliezimprimer quelque chose avant labarre latérale dans votremodèle.
- Vous allez ajouter un crochet d'action dans votrefichiermodèle
index.php
via<?php add_action('bp_sidebar_left'); ?>
. - Maintenant,dans votrefichier
functions.php
,vouspouvez ajouter unefonction de rappel à ce hookpourimprimer ce que vous voulez.
add_action('bp_sidebar_left', 'bp_sidebar_left_cb'); function bp_sidebar_left_cb() { echo 'Hello World !'; }
do_action
: Registers an action hook whileadd_action
: adds a callback function to the registered hook.Example
Consider you wanted to print something before sidebar in you template.
- You will add an action hook in your template file
index.php
via<?php add_action('bp_sidebar_left'); ?>
. - Now in your
functions.php
file you can add a callback function to that hook to print something you want.
add_action('bp_sidebar_left', 'bp_sidebar_left_cb'); function bp_sidebar_left_cb() { echo 'Hello World !'; }
-
- 2017-02-01
Vous devez utiliser comme ci-dessous:
function custom_register() { echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>'; } . add_action('custom','custom_register'); // Too late - do_action was already called. do_action('custom'); // This is called before it will have an effect
You have to use like below:
function custom_register() { echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>'; } . add_action('custom','custom_register'); // Too late - do_action was already called. do_action('custom'); // This is called before it will have an effect
-
ÇamarchepourmoiIt is working for me
- 0
- 2017-02-01
- user112289
Cette questionestpeut-êtreerronée,je n'en suispas sûr. Parce queje ne suispas clair à ce sujet.
Je sais que
add_action
est utilisépour accrochernotrefonction à lafonction spécifiée. Parexemple,add_action('wp_head'.'myfunc');
maintenant quel que soit le code dansmyfunc
seraexécuté dans lewp_head()
. C'est clair,maisj'ai des doutes surdo_action
ce qu'ilfait?Jepense qu'ilest utilisépour créernotrepropre hook comme des hooks déjà disponibles (wp_head,wp_footer,..etc.) Sij'ai raison,quelqu'unpeut-ilme montrer une réponse simpleet compréhensible avec unexemple simple.
J'aiessayé la différence sur Internet,maistousindiquent la différenceentre add_actionet add_filter. Jene veuxpas y allerparce queje veux d'abord clarifier cela,puisje vais y aller.
Quelqu'unpeut-ilm'aider?
MODIFIER après le POST de la question
J'aiessayé ceci dans lepluginmaisje n'aipas reçu lemessage d'alerte.
Mais quandj'accroche lamêmefonction avec
wp_head
,celafonctionnebien