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 want to do a cron(executes function at specific time, like email,db backup etc) job. I searched in wordpress, i got this wp_schedule_event Here they say it will execute job when someone visits wordpress site if the specified time is passed.

I have a doubt, it is doing this only when someone visits wordpress website. Is cron job same for normal php site without any framework.

If it runs only when user visits the site, then why not can i do it in init hook itself with a time if condition so i can achieve what wp_schedule_event is doing without it.Like below

function test(){
if($time_condition > 20 && $time_condition < 23){
//do stuff
}
}
add_action('init','test');

As init runs wordpress loaded, so every time user visits site this will be checked and executed.

can someone please give the difference and Which is useful for this kind of job?

I see both

wp_schedule_event and init are same except we need to add the condition in init because other than that wp_schedule_event also runs only when someone visits.

share|improve this question

1 Answer 1

The point of the wp-cron is to spawn separate process to run events in.

It isn't quite perfect and sometimes spawning process slows down page view noticeably, however it is vastly better than executing some heavy operation at the start of random visitor's page load.

share|improve this answer
    
THanks for the answer. So if we use wp-cron there won't be page load problem. But if we use init hook then there will be page load. Is this the difference between them? –  rram Mar 2 at 5:54

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.