$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - Delay an action until current action is completed|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - Delay an action until current action is completed

matteradmin8PV0评论

I have two actions that run when a post is updated. The issue is, the second function runs before the first has completed.

add_action('save_post', 'postUpdate', 10);

add_action('post_updated', ‘deleteCacheOnCloudfront ’, 1000, 3);

What is the best method to allow the first function to finish before the second action starts? Is there another hook that starts much later? In the meantime, I have set a one time cron to activate 5 seconds later, which I don't think is an elegant solution.

Thanks!

I have two actions that run when a post is updated. The issue is, the second function runs before the first has completed.

add_action('save_post', 'postUpdate', 10);

add_action('post_updated', ‘deleteCacheOnCloudfront ’, 1000, 3);

What is the best method to allow the first function to finish before the second action starts? Is there another hook that starts much later? In the meantime, I have set a one time cron to activate 5 seconds later, which I don't think is an elegant solution.

Thanks!

Share Improve this question asked Feb 8, 2019 at 21:22 HarvHarv 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Note that the third input argument for the save_post callback, tells us whether it's an update or not. That could be helpful here, I think.

Taking your example as is, one suggestion could be:

add_action( 'save_post', function(  $post_ID, $post, $update ) {

   postUpdate( $post_ID );

   if ( $update ) {
       deleteCacheOnCloudfront( $post_ID, $post, $update );
   }

}, 10, 3 );

to run the deleteCacheOnCloudfront() function after the postUpdate() function on post updates only.

The save_post is a very general hook and there exists more specific ones, like save_post_{post_type} or other based on post status transitions, if you want to avoid running your code when .it doesn't need to.

The postUpdate() function name is very general, so you should consider using a namespace or a function prefix, to avoid possible name collisions.

Hopefully the deleteCacheOnCloudfront() runs quickly (short timeout) and will not let the user wait too long during each post save.

Hope it helps!

Update:

If deleteCacheOnCloudfront() depends on $post_before and $post_after we can consider:

/**
 * Run postUpdate() on save_post with priority 10.
 */
add_action( 'save_post', function(  $post_ID, $post, $update ) {
   postUpdate( $post_ID );
}, 10, 3 );

/**
 * Run deleteCacheOnCloudfront() on save_post with priority 11 when post is updated.
 */
add_action( 'post_updated', function(  $post_ID, $post_before, $post_after ) {
    add_action( 'save_post', 
        function(  $post_ID, $post, $update ) use ( $post_before, $post_after ) {
            deleteCacheOnCloudfront( $post_ID, $post_before, $post_after );
        }, 11, 3 );
}, 10, 3 );

where we pass data from the parent scope with the use keyword and use different priority to make sure deleteCacheOnCloudfront() (priority 11) runs after postUpdate() (priority 10), within the save_post action.

We could also write this as a class instead, to move data across different hooks.

Post a comment

comment list (0)

  1. No comments so far