• I have two “chains” of actions in WordPress (add_action‘s and do_action‘s linked together) and I need all of the first chain to complete before starting the second chain.

    Using priority order I can trigger the start of each chain one after the other, but I am worried that a large loop in the middle of the first chain will still be running when the second chain starts.

    EXAMPLE:

    Since the first_chain_first_loop action has a priority of 10, and second_chain_first_loop action has a priority of 11, will the entire first chain of actions (first_chain_first_loop + first_chain_second_loop) be processed before second_chain_first_loop starts?

    Or does second_chain_first_loop start immediately after first_chain_first_loop is complete? And if this is the case, how can I ensure the second chain will wait for the first chain to complete (including the large loop) before triggering the second chain.

    //First chain of actions - Has early priority but a large loop in the middle
    add_action( 'init', 'first_chain_first_loop', 10 );
    function first_chain_first_loop() {
        for ( $i=0; $i < 10; ++$i ) {
            do_action( 'first_chain_second_loop_action', $i );
        }
    }
    add_action( 'first_chain_second_loop_action', 'first_chain_second_loop', 10, 1 );
    function first_chain_second_loop( $i ) {
        for ( $i=0; $i < 1000000000; ++$i ) {
            do_action( 'first_chain_logic_function_action', $i );
        }
    }
    add_action( 'first_chain_logic_function_action', 'first_chain_logic_function', 10, 1 );
    function first_chain_logic_function( $i ) {
        //Do stuff here
    }
    
    //Second chain of actions  - Has later priority
    add_action( 'init', 'second_chain_first_loop', 11 );
    function second_chain_first_loop() {
        for ( $i=0; $i < 10; ++$i ) {
            do_action( 'second_chain_second_loop_action',  $i );
        }
    }
    add_action( 'second_chain_second_loop_action', 'second_chain_second_loop', 10, 1 );
    function second_chain_second_loop( $i ) {
        for ( $i=0; $i < 10; ++$i ) {
            do_action( 'second_chain_logic_function_action', $i );
        }
    }
    add_action( 'second_chain_logic_function_action', 'second_chain_logic_function', 10, 1 );
    function second_chain_logic_function( $i ) {
        //Do stuff here
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Don’t worry about one function processing before a different one running before it has finished. PHP doesn’t work like that. It’s a pretty simple one-step-at-a-time system that goes in order. As long as you set the prioroties in the right order when you set up the functions with add_action() they will run in order, no matter how long one of them takes.

    Moderator bcworkz

    (@bcworkz)

    Catacaustic is basically correct, but you can encounter situations where a race condition can develop. For example, saving something to the DB, then immediately getting it back out again. You may find you’ve gotten the old value from the DB, not the one just saved. But as far as doing things entirely within PHP, it’s all going to run in one thread in the order called, no overlapping.

    Action priorities only apply to callbacks added to the same action. There is no relationship between priorities assigned to different actions.

    For the sake of discussion, why all the chained actions? There’s no reason you couldn’t do this, but it seems overly complicated. Why not simply call each function in turn instead of firing off various actions?

    If there were any concern at all of a race condition developing, you could add an action from within an earlier running callback. Then it would be impossible for the second added callback to run should the action it is added to somehow fire too early. (It couldn’t fire early the way you have it set up, but for some situation where it could let’s say…) You would be doubly assured the second could not execute until the first has executed. It does mean the second may not execute if its related action had fired earlier.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How does priority order effect a chain of add_action’s and do_action’s in WordPr’ is closed to new replies.