do_action( string $tag, mixed $arg = '' )
Appel les fonctions qui ont été attachées à une action spécifique.
Paramètres
$tag(string)requisLe nom de l'action a exécuter.
$arg(mixed)optionnelArguments additionnels qui peuvent être passés à la fonction attachée à l'action.
Valeur par défaut : ''
Description / Informations supplémentaires
Cette fonction appelle toutes les fonctions attachées au hook $tag. Il est possible de créer une nouvelle action tout simplement en appelant cette fonction et en spécifiant le nom du nouveau hook $tag.
On peut passer plusieurs arguments $arg comme avec apply_filters().
Retourne
(string) Cette fonction ne retourne rien.
Déclaration et structure de la fonction do_action()
do_action() est déclarée dans le fichier wp-includes/plugin.php à la ligne 482 :
function do_action( $hook_name, ...$arg ) {
global $wp_filter, $wp_actions, $wp_current_filter;
if ( ! isset( $wp_actions[ $hook_name ] ) ) {
$wp_actions[ $hook_name ] = 1;
} else {
++$wp_actions[ $hook_name ];
}
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
_wp_call_all_hook( $all_args );
}
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
}
if ( empty( $arg ) ) {
$arg[] = '';
} elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) {
// Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`.
$arg[0] = $arg[0][0];
}
$wp_filter[ $hook_name ]->do_action( $arg );
array_pop( $wp_current_filter );
}
Fonction utilisée par do_action()
_wp_call_all_hook()Appelle le hook 'all' qui fera défiler les fonctions attachées à celui-ci.
Où est utilisée la fonction do_action()
dans le CMS WordPress
Exemple
function my_callback( $value ){
echo $value;
}
add_action( 'my_action', 'my_callback' );
do_action( 'my_action', 'Hello gentlemen !' );
/* Cette action affichera 'Hello gentlemen !' */
Sources
Codex WordPress : do_action()
Autres fonctions dans le même fichier : wp-includes/plugin.php