How to manage cron jobs at Pressable
What is a cron job?
A cron job is used to schedule tasks that are executed periodically; for example, to send out an email notice every morning, or to clear the cache every so often. Automation of such repetitive tasks using cron jobs saves time and ensures consistency.
WordPress cron jobs
In WordPress, the wp-cron function is used to simulate a system cron. The wp-cron() runs a hooked function at a set interval. A backup plugin (like Jetpack Security Daily) runs on a predefined schedule; this is an instance of a WordPress cron job.
An important factor to keep in mind when working with wp-cron: Cron jobs rely entirely on intervals and only work when a page is loaded. In order for wp-cron.php to fire, the website has to be loaded by a visitor; in cases where a website has low-traffic, wp-cron.php may not fire often or on time. In this document, we will address how to successfully work with wp-cron.php. To learn more about WP Cron, we recommend checking out the official WordPress Plugin Handbook.
WP Cron Jobs at Pressable
We currently do not offer server-side cron jobs at Pressable. However, you can create and manage cron jobs on your WordPress site programmatically, or by using a plugin such as Advanced Cron Manager (or a similar plugin like WP Crontrol).
Using Advanced Cron Manager
Once you install and activate the plugin, you should see the Cron Manager under the Tools (or Settings) tab on your WordPress Dashboard.
The Cron Manager enables you to view, edit, execute, add, pause, or delete cron jobs. The option to “execute now” does come in handy when troubleshooting, where you may need to run the event multiple times. The Implementation tab under each of the events also shows the action hook.
There are default schedules that WordPress has in place like hourly, daily, twice daily, and weekly – you can also add new schedules based on your requirement from the Schedules section.
Steps to create a new cron job in WordPress
- Click on the “Add new event” button on the Cron Manager.
Here you will be able to add the Event Hook and also the schedule. - Create a custom hook and give that hook the name of a function to execute. In the example below, wp_test_email is the hook name and email_cron_function is the function the hook will execute.
- Click on the “Add event” button to save the cron.
- Add a corresponding action hook somewhere in your code, such as the functions.php file of your theme (or child theme).Example: If your hook is wp_test_email, you’ll need to add this in PHP:
add_action( ‘wp_test_email’, 'email_cron_function' ); function email_cron_function() { // This is the task that the cron job will perform when triggered wp_mail( 'youremail@example.com', 'Automatic email', Scheduled email from cron'); }
Once you become accustomed to their creation, you can create crons to perform tasks for you. Although not essential, having intermediate level programming and WordPress development skills will make setting up custom crons quick and easy.
Using monitoring tools to trigger wp-cron.php
In order to trigger wp-cron.php without requiring a visit, you can create a monitor in Uptime Robot (or Pingdom) for yoursite.com/wp-cron.php. Setting up such a monitoring service to visit your site every 5 minutes (or the duration you need) will trigger the wp-cron, and as an added bonus the service will also alert you if the site ever goes down.
The Monitor also gives information on the status of your site.
You now have your cron job set up and also a way to ensure it is triggered.
That’s all there is to it!
Online Cron Services
Alternatively, you may want to look into some of the online cron services to make the process easier. A few such services are listed here:
Cron Event Information on Pressable Control Panel
You can now check cron jobs that are running on a site via the Pressable Control Panel. Under the “Advanced” tab, the Cron Events are listed. This provides a quick overview of the cron jobs and makes it easy to monitor.
This also includes details like the Hook Name, Next Run Time(GMT and Relative), and Recurrence.