Cron Wordpress : All about cron tasks

Learn how to manage, optimize and troubleshoot WordPress cron jobs to keep your site running smoothly.
Vector image representing different devices running on Wordpress using cron jobs
Table of contents

Cron jobs are automated actions, programmed to run at regular intervals on a server. In the context of WordPress, they allow to perform various maintenance, update or backup operations without manual intervention of the user. They are essential to ensure the proper functioning and performance of a WordPress site.

The goal of this article is to provide you with an in-depth understanding of WordPress cron actions, how they work internally, how to schedule and manage them, and best practices for optimizing their use. We will also discuss WordPress plugins plugins for managing actions and troubleshooting solutions for common problems.

Deepening of cron tasks

Internal operation

WP-Cron.php

WP-Cron.php is at the heart of the WordPress tasks. It is responsible for automated executions and is triggered automatically when a page is loaded on your site. WP-Cron. php checks for pending actions and executes them accordingly. This process is different from the traditional process, which relies on the web host to trigger actions.

example of a wordpress wp-cron.php file
The wp-cron.php file is at the origin of all the internal functioning of the automations

Difference between system cron tasks and WP-Cron

System cron jobs are managed by the server on which your site is hosted and run independently of WP. They run on a fixed schedule defined by the administrator of the web infrastructure administrator. In contrast, WP-Cron is triggered by visitors to your site and therefore depends on traffic to run. This difference can lead to inconsistencies in the application and performance problems in some cases.

Importance of cron jobs for WordPress

Regular maintenance

Tasks are crucial to ensure regular maintenance of your site. They allow you to perform actions such as updating tools and themes, backing up the database, removing unwanted comments and checking for broken links. These operations help to maintain the security, performance and reliability of your site.

Automation of tasks

Recurring processes automate various actions that would otherwise have to be performed manually by the site administrator. This simplifies site management, frees up time to focus on other important aspects and reduces the risk of human error. They are also essential for features that require regular action, such as sending newsletters or updating site statistics.

Scheduling of cron jobs

Task management with the functions.php file

Creating a custom cron job

To create a custom cron action, you first need to define a function that contains the code to execute the event. Then, add it to the functions.php of your theme. Here is an example of how to create a custom action:

function mon_action_cron() {
    // Votre code à lancer ici
}

add_action('mon_event_cron', 'mon_action_cron');

Finally, schedule the event using the wp_schedule_event() function:

if (!wp_next_scheduled('mon_event_cron')) {
    wp_schedule_event(time(), 'hourly', 'mon_event_cron');
}

In this example, the action will be executed every hour.

Deleting an existing cron job

To delete an existing task, use the wp_unschedule_event() function specifying the name of the event and the associated hook. For example:

$timestamp = wp_next_scheduled('mon_event_cron');
wp_unschedule_event($timestamp, 'mon_event_cron');

Using hooks and filters

wp_schedule_event()

The wp_schedule_event() function allows you to schedule an event to run at regular intervals. It takes three arguments: the timestamp of the first execution, the recurrence frequency (hourly, twicedaily, daily or a custom value) and the name of the hook action to which the function is attached.

wp_unschedule_event()

The wp_unschedule_event() function allows you to delete a scheduled action by providing the timestamp of the event and the name of the associated hook action. It cancels the future launch of the cron task.

wp_next_scheduled()

The wp_next_scheduled() function returns the timestamp of the next scheduled execution of an event, based on the provided hook action name. It is useful to check if an event is already scheduled and to avoid scheduling duplicate events.

Alternative methods to manage cron jobs

Using WP-CLI to manage WP-Cron

WP-CLI is a command line tool for managing your site. It offers an alternative method of managing periodic operations by allowing you to enter commands directly from the terminal. Here are some useful commands to manage WP-Cron with WP-CLI:

Show the list of scheduled cron tasks :

wp cron event list

Run a specific cron job immediately:

wp cron event run <hook>

Delete a scheduled cron job :

wp cron event delete <hook>

Adding a cron job using cPanel

If your site is hosted on an infrastructure using cPanel, you can add a task directly from the cPanel interface. Here is how to do it:

  1. Log in to your cPanel account.
  2. Look for the " Cron Jobs " section and click on it.
  3. In the " Cron Jobs" page page, set the frequency using the drop-down menus.
  4. In the "Command" field, enter the command to run WP-Cron (adjust the path if necessary):
    wget -q -O - https://www.example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
  5. Click on " Add New Cron Job " to save the job.
Screenshot of the cron job feature of a Cpanel
Cpanel offers integrated cron job management

Configuring a cron job with NGINX

If your site is hosted on an infrastructure using NGINX, you will have to configure the task using the cron process of your server. Here's how to do it:

  1. Connect to your server via SSH.
  2. Open the cron configuration file using the crontab -e command.
  3. Add a line to run WP-Cron at the desired frequency (adjust the path and frequency as needed):
    */15 * * * * wget -q -O - https://www.example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
  4. Save and close the file to apply the new cron job.

Plugins for cron job management

WP Crontrol

WP Crontrol is a popular tool that allows you to easily manage your WordPress site tasks directly from the administration interface. It offers the following features:

  • Display of the list of planned actions with their frequency and hooks.
  • Add, modify and delete custom actions.
  • Manual execution of cron tasks.
  • Add, modify and delete custom recurrence frequencies.

Advanced Cron Manager

Advanced Cron Manager is another task management tool that offers a user-friendly interface and advanced features. Among its main features are:

  • Display scheduled cron tasks with detailed information about their progress and duration.
  • Add, modify and delete custom actions.
  • Manual launch and temporary suspension of cron tasks.
  • Customized recurrence frequency management.
  • Error and performance logging for easy troubleshooting.

Easycron

Easycron is an online scheduling manager that simplifies the configuration and maintenance of scheduled actions on your website. 

Screenshot of the Easycron website
Easycron offers online management of your tasks
  • User-friendly interface: Easycron offers a clear and easy-to-use interface.
  • Flexible scheduling: You can define custom recurrence intervals for your scheduled events, including intervals based on minutes, hours, days or weeks.
  • Manual execution: Easycron allows you to manually execute scheduled actions at any time, which is useful for testing and troubleshooting.
  • Email Notifications: Receive email notifications of failed or successful actions to stay informed and quickly resolve potential issues.
  • Logs and history: Easycron keeps a detailed history of the runs, including information on duration, possible errors and results. 

Comparison of the main features

Both WP Crontrol and Advanced Cron Manager offer similar basic features for management, such as viewing, adding, editing and deleting tasks, as well as managing recurrence frequencies. However, Advanced Cron Manager goes a step further by offering advanced features such as error and performance logging, temporary suspension of actions and more detailed information overall.

The choice between these two solutions will depend on your needs and your level of expertise. If you are looking for a simple and easy-to-use solution, WP Crontrol may be a good choice. On the other hand, if you need advanced troubleshooting and performance monitoring options, Advanced Cron Manager will be more suitable.

Optimization and troubleshooting of cron jobs

Switch to a real cron system

Deactivation of WP-Cron

To improve the reliability and accuracy of scheduled task execution, it may be best to disable WP-Cron and switch to a true cron system. To do this, add the following line to your wp-config.php:

define('DISABLE_WP_CRON', true);

System cron configuration

After disabling WP-Cron, configure the system cron for your web environment to perform the desired tasks. Use the crontab -e command to open the configuration file and add a line like this (adjust the path and frequency as needed):

*/15 * * * * wget -q -O - https://www.example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

This configuration will run WP-Cron every 15 minutes.

Troubleshooting common problems

Blocked or overdue tasks

If some tasks are not running as expected, first check that they are properly scheduled using a cron job management tool. Then, identify conflicting actions or code errors by reviewing WP logs and error reports.

Performance problems

Running multiple tasks simultaneously can cause performance problems on your site. To solve them, stagger actions to avoid overlapping, reduce the frequency of non-essential ones, or consider switching to a higher-performance hosting.

Good development practices

Testing cron jobs

It is crucial to test cron jobs to ensure that they work properly and do not negatively impact the performance of your site. Use a local development environment or a separate test environment to validate their operation before deploying them to your production site.

Use logs for tracking

It is a good practice to set up a logging system to track the progress of tasks and detect errors. Logs will allow you to quickly identify problems and take corrective action. Some task management tools offer built-in logging features to make this task easier.

In conclusion, are cron jobs important or not?

A developer who thinks about the importance of wordpress cron jobs
In light of this article, do you think cron jobs are important for your website?

In conclusion, cron jobs play an essential role in the proper functioning and maintenance of a site. They allow the automation of recurring processes and contribute to the overall performance of the site by performing important tasks at specific times. Their optimal use is crucial to ensure a high-performance, secure and always up-to-date website.

The importance of automations cannot be underestimated, as they are an important part of WordPress management operations. They are involved in actions such as scheduled content publishing, automatic plugin and theme updates, regular data backups, and many others. A site without well-managed automations can quickly become outdated, slow, and vulnerable to attack.

In a world where sites are becoming more and more complex and user expectations for performance and security are increasing, it is crucial to take a stand on the importance of cron actions and to adopt best practices to manage them efficiently. It is important to choose the right methods and tools to plan, execute and optimize actions according to the specific needs of the site and the constraints of the web environment.

Ingenius, the quality expert

AsWordpress expertsexperts, we fully understand the importance of these cron tasks and, as experts in the field, we have the experience and skills to help you optimize your site management and implement best practices to ensure optimal performance of your WordPress.

Our team of professionals is ready to thoroughly analyze your site's important tasks, identify potential problems and develop strategies to optimize their management. We will work closely with you to determine your specific needs and provide you with customized solutions that fit your site and web environment.

The Wordpress agency that designs and builds high quality and sustainable websites.