Wypo Logo Header@2x
Blog / Developer / Using WordPress crons

Using WordPress crons

Developer
Time to read : 11 minutes

Publication date: 18 July 2024

What is a Cron Job?

Imagine a dedicated digital personal assistant that performs tasks for you at specific times, without you having to intervene. That’s essentially what a cron job (a contraction of “chronograph job”) is. It’s an automated way of scheduling and running tasks at regular intervals, whether that’s every minute, every hour, every day, or at other specific intervals.

In the context of WordPress, cron jobs are used to automate various actions essential to the smooth running of a website. Here are some common examples of how cron jobs are used in WordPress:

  • Scheduled article publishing: When you schedule the publication of an article for a specific date and time, WordPress uses a cron job to ensure that the article is published exactly at the scheduled time, even if no user is present to manually trigger this action.
  • Plugin and theme updates: cron jobs are also responsible for regularly checking for available updates for plugins, themes and even the WordPress core. This ensures that your site remains secure and up-to-date without requiring constant human intervention.
  • Periodic emailing: If your site sends automatic emails at regular intervals, such as periodic newsletters or scheduled notifications, a cron job is used to trigger the sending of these emails at the desired frequency.
  • Database clean-up: Cron jobs can also be used to automate maintenance tasks such as database clean-up, deleting temporary or obsolete data, to optimize performance and avoid the accumulation of unnecessary data.
  • Scheduled backups: If you perform regular backups of your WordPress site, cron jobs can be configured to automate this process at selected intervals, ensuring that your critical data is secure and accessible when needed.

How do I use Cron Jobs in WordPress?

Understanding the three types of Cron Jobs :

  • WordPress Cron (default Cron): This is a traffic-based system. When a user visits your site, WordPress checks to see if there are any scheduled tasks to run.
  • Server Cron (Actual Cron): Tasks are triggered by the server at specific intervals, independent of site traffic.
  • External Cron: Configuration to use an external service to manage cron tasks instead of the server.

Configuring Cron Jobs in WordPress

WordPress uses the traffic-based cron system by default. This means that scheduled tasks run when someone visits your site.

To trigger WordPress cron jobs more precisely, you can add the following line to your wp-config.php file:

PHP

Next, set up a cron job on your server to call wp-cron.php at regular intervals. Here’s an example command line to add a cron job:

Shell

This will run wp-cron.php every 5 minutes.

Using Cron Jobs for specific tasks

  • Plugins can add their own cron jobs for specific actions such as backing up the database, sending notifications, etc.
  • Some plugins can provide an interface to manage cron jobs directly from the WordPress interface.

Steps to add a WordPress cron in php

WordPress uses a cron system based on HTTP requests to execute scheduled tasks. Here’s how to use it effectively:

Write the function to be executed

Create a PHP function in your theme’s functions.php file or in a plugin you’re developing. This function represents the task you wish to automate.

PHP

Schedule cron job

Use the wp_schedule_event function to schedule your function to run at specific intervals.

PHP

In this example :

  • wp_schedule_event is used to set the schedule for your cron job.
  • 'every_15_minutes' is the predefined interval you can use. WordPress supports multiple intervals like 'hourly', 'daily', 'twicedaily', etc. You can also define a custom interval using wp_get_schedules.
Last 30 days : 1
Total : 217