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?
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.