delay function on publish?

publish_post is called after post is published! So, you already got covered. but if you want to run an action after a certain time of the post is published, it’s better to write a cron job.

For example, if you need to run the function after 5 minutes of the post is published, you need to register a single cron event that will be triggered after 5 minutes from now (post publish)

add_action('publish_post', 'register_single_cron');

function register_single_cron($id){
  wp_schedule_single_event(tim() + 300, 'custom_function');
}

function custom_function(){
  //your logic goes here
}

Please check the api details here.

But this system has one problem, it will not be triggered until site is loaded/visited on or after the scheduled time.

Leave a Comment