Description

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().

Paramètres

$tag

(string) (Requis) Le nom de l'action a exécuter.

$arg

(mixed) (Optionnel) Arguments additionnels qui peuvent être passés à la fonction attachée à l'action.

Valeur par défaut : ''

Structure de la fonction do_action()

Définie dans le fichier wp-includes/plugin.php à la ligne 444 :

function do_action( $tag, ...$arg ) {
    global $wp_filter, $wp_actions, $wp_current_filter;

    if ( ! isset( $wp_actions[ $tag ] ) ) {
        $wp_actions[ $tag ] = 1;
    } else {
        ++$wp_actions[ $tag ];
    }

    // Do 'all' actions first.
    if ( isset( $wp_filter['all'] ) ) {
        $wp_current_filter[] = $tag;
        $all_args            = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
        _wp_call_all_hook( $all_args );
    }

    if ( ! isset( $wp_filter[ $tag ] ) ) {
        if ( isset( $wp_filter['all'] ) ) {
            array_pop( $wp_current_filter );
        }
        return;
    }

    if ( ! isset( $wp_filter['all'] ) ) {
        $wp_current_filter[] = $tag;
    }

    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[ $tag ]->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ù trouver 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

Retour