Knowledge Base

How to manage cron jobs at Pressable

Category: Tutorials | Last modified: February 18, 2021

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

Cron Jobs | Cron Manager | Pressable

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.

Cron Jobs | Custom Cron Functions | Pressable

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.

Cron Jobs | Cron Manager Dashboard | Pressable

Steps to create a new cron job in WordPress

  1. 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.
  2. 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.
  3. Click on the “Add event” button to save the cron.
  4. 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');

Add new cron jobs event | Pressable

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.

Cron job Monitor | Pressable

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!

An Automattic Invention A family of WordPress solutions.