I wasn’t sure how to ask this. In WP, when the program runs, is it all “sequential”? By this I mean does WP hit the hook you registered and call it, wait, then proceed, synchronous? There’s no Dependency Injection of IoC that I could find and no async.

I looked a the core, and I couldn’t tell. I saw references to a few global variables and an array of hooks iterated over, but I did not understand the execution. I tried to with xdebug, but I have not grasped it yet.

2 Answers
2

The do_action() and apply_filters() are both wrappers of the

WP_Hook::apply_filters()

method, that invokes the registered callbacks in sequential order (src).

Here’s a simple test:

Let’s define a callback wrapper:

$callback_gen = function( $seconds ) { 
    return function() use ( $seconds ) {
        sleep( $seconds ); 
        printf( '%s finished' . PHP_EOL, $seconds ); 
    };
};

Next we register the callbacks to the mytest action:

add_action( 'mytest', $callback_gen( 3 ) );
add_action( 'mytest', $callback_gen( 5 ) );
add_action( 'mytest', $callback_gen( 1 ) );

Then we invoke the callbacks and measure the time it takes:

$start = microtime( true );
do_action( 'mytest' );
$stop = microtime( true );

and display it with:

echo number_format( $stop - $start, 5 );

Here are outputs from 5 test runs:

3 finished 
5 finished 
1 finished 
9.00087

3 finished 
5 finished 
1 finished 
9.00076

3 finished 
5 finished 
1 finished 
9.00101

3 finished 
5 finished 
1 finished 
9.00072

3 finished 
5 finished 
1 finished 
9.00080

where the order is the same for each run, with total of c.a. 9 seconds, as expected for a sequential run order.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *