WP Cron and Events Manager
What is Cron and WP_Cron?
Cron is a Linux scheduling utility which enables a server to run tasks automatically at preset times, without user intervention. Common tasks that use Cron include things like automated backups, which might be scheduled to run daily in the middle of the night. Each scheduled task is called a cron job and they can be run as frequently as every minute or as far apart as once a year.
Many core WordPress features – like checking for theme and plugin updates – are run as scheduled tasks. The trouble is, because Cron is a server command (and not all WordPress sites are run on Linux), WordPress can’t add tasks directly to Cron. Instead, it uses its own version called WP_Cron.
How WordPress Crons Work
Every time a page is loaded in admin or on the front-end of your site, WordPress calls WP_Cron. This function checks for scheduled tasks and runs any that are found. This takes place as a background process so that page loading isn’t held up while tasks are running.
Events Manager uses WP_Cron to send event email reminders. When you set up reminder emails under Events > Settings > Emails > Event Email Reminders, you’re effectively creating a cron job to run every day to check for event reminders. In fact, Events Manager uses two crons – one to create the mail queue daily and another to process the queue every minute and send out emails in batches (if there are any to send).
Potential problems with WordPress Cron
The problem with using WP_Cron that if your site traffic is relatively low, you may not get a visitor until some time after a cron job was due to run – leading to delayed event email reminders.
Sites with lots of traffic can experience problems too, with simultaneous page loads causing WP_Cron to run more than once. In some cases, this can lead to more than one reminder being sent to a user as a second call to WP_Cron re-processes the mail queue before the first WP_Cron has had time to delete it.
This means automatic triggering of WP_Cron isn’t always the most reliable solution – especially for time-sensitive content like events. The answer is to set up a “real” cron job on the server to call WP_Cron at the exact time, only once, without needing to be triggered by visitors on your site.
Setting up ‘real’ cron jobs
The first step is to prevent WordPress from running WP_Cron automatically. To do that, open up your wp-config.php file (found in the top level folder of your WordPress install) in a text editor and add this line:
Now you’re ready to add a server side cron job. We’re recommending two ways, the ‘right’ way and the ‘easy’ way, both should have the same result.
The ‘right’ way: Set up a server cron job
The exact steps / screens will vary between web hosts, but the general process is similar no matter which company you use. The steps and screenshot below show how to do it using cPanel which is used by Bluehost, Hostgator and many other hosting companies.
After logging in to your cPanel / admin area, go to the Advanced area and click the Cron jobs icon.
On the Add new cron job page you’ll need to configure both a time for the cron and the script you want to run. For optimum running of all WordPress and Events Manager scheduled tasks, schedule a cron to run once every minute.
Many admin panels – including cPanel – have a preset selector for “Every minute”. If not, add an asterisk (*) under the minute option to denote “every minute”. Make sure other options like Day and Month also have an asterisk so that the cron runs every day and every month.
In the Command field, paste the line below making sure you change yoursite.com to your own domain:
wget http://www.yoursite.com/wp-cron.php?doing_wp_cron=1 > /dev/null 2>&1
On some systems, you may need to use a curl function instead:
curl -s http://www.yoursite.com/wp-cron.php?doing_wp_cron=1 > /dev/null
The ‘easy way’: use a monitoring service
If setting up server cron jobs isn’t your cup of tea, there’s an easy alternative. There’s loads of monitoring services out there that ping your site to make sure it’s working. You can use these to do the same thing you’d do via a cron job, which is simply visiting the wp-cron.php file on your site every minute. The downside is that most services will charge for minute-by-minute checks which is the recommended frequency.
EasyCron is a service dedicated to doing cron jobs. They even have a free account offering crons every 20 minutes and have a tutorial specifically for Events Manager.
Pingdom is a robust service mainly aimed at checking whether your site is up, but is also capable acting as a cron job, simply set up an HTTP request every minute to http://www.yoursite.com/wp-cron.php?doing_wp_cron=1 (remember to change www.yoursite.com to your domain).
Checking cron is running
Some admin panels allow you to specify a notification email that’s sent each time the cron script is run. That can be handy, but make sure you remove the email once you know it’s working, unless you like getting lots of email :)
Alternatively, try the WP Crontrol plugin which lets you view scheduled tasks to make sure they’re being run as they should.
Cron on WordPress Multisite
If you’re running a WordPress network, remember the method above will only run WP_Cron for one site only – you’ll need to add an additional cron for each site in the network.
Another plugin that may be useful is WP-Cron Control by Automattic, which lets you specify a secret key that must be used when calling WP_Cron. This is another way to prevent multiple calls to WP_Cron.
Photo credit: Βethan