Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does wp-cron.php run in Wordpress?

I need details about wp-cron. I used wp_schedule_event for automatically sending emails.

But wp_schedule_event is only triggered when a user visits our site. How to make the cron run automatically?

like image 477
softsdev Avatar asked Oct 15 '12 12:10

softsdev


3 Answers

It will run at first visit right after the scheduled time.
1.- yes, run automatically, but triggered by a visitor.
2.- it's enough to sending email in a scheduled basis.


For a real cron behavior you can run a script using crontab

crontab -e
wget -q http://domain.com/wp-content/some-path/your-script.php

-q: quiet (no output). Just run the script. Otherwise you will have a lot of files in your server (wget pull files)

Tip: http://www.thegeekstuff.com/2011/07/php-cron-job/


Using cpanel: http://wiki.hostbillapp.com/index.php?title=Settings:_cPanel:_Cron_job

enter image description here

like image 197
Igor Parra Avatar answered Oct 15 '22 20:10

Igor Parra


wp_cron does not run all the time in the background the way the name might suggest. That kind of scheduling isn't possible for a web application like WordPress, since the WordPress script only runs when someone is viewing the site and it does not run when no one is looking. What happens instead is that when the WordPress script boots, the cron values are checked and if one is (over)due it gets executed. It may be a few minutes late or a few hours late, but that is the way it works. The wp_cron jobs run at the first opportunity, basically.

If you want to run a script when someone visits the site, you don't really want wp_cron at all. wp_cron doesn't trigger when someone visits. It is a fuzzy timer. To run when someone visits you are going to have to think it through some. You could put a function in your theme's functions.php but it would run on every page load, not just on the first load of the visit. You could hook to wp_login and run your function when someone logs in. You are going to have to decide what counts as a 'visit' first.

like image 38
s_ha_dum Avatar answered Oct 15 '22 20:10

s_ha_dum


It seems that every one has just answered the way a *nix cron should be setup. Where as the real answer should be wordpress specific. There is a setting that can be done in the wp-config.php

define('DISABLE_WP_CRON', 'true');

Now if you can configure the *nix cron like Igor Parra answered, the background and offset cron jobs will not run any more, where as the time set in the control panel or crontab will be in effect.

like image 25
Jiju Thomas Mathew Avatar answered Oct 15 '22 21:10

Jiju Thomas Mathew