Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this function where I call a custom function through the add_action hook:

add_action('publish_post', 'custom_function');

... now it works perfectly, but the I want the custom_function to be delayed so it runs AFTER the post has been published.

BUT, if I add sleep(20) inside the custom_function it delays the post itself. What I want is the post to be published, and THEN run this function after x seconds.

Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
ah, visiting the site is a strange requirement. :( any other workarounds? –  Nimbuz Jun 23 '12 at 7:34
    
i do not see any easy workaround! all the alternatives i've in my mind are more complicated :s –  HungryCoder Jun 24 '12 at 4:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.