20i
What are wordpress cron jobs?

How to Use WordPress Cron Jobs

WordPress Cron jobs are a scheduling system that allows you to automate various tasks within WordPress. Although they’re not the true UNIX cron jobs, you still achieve the same outcome.   

Using WP Cron to automate routine and time-consuming tasks and can save a lot of manual work.  

Automated tasks are processed in the background, so it’s essential to ensure that scheduled task system is configured correctly to avoid issues with your website’s functionality.  

What are WordPress cron jobs? 

The key difference between WP Cron and the traditional UNIX Cron is that when scheduled tasks are executed with WP Cron, this takes place within the WordPress website. Whereas traditional UNIX scheduled tasks are executed within the server environment.  

The advantage of traditional Unix Cron is that WP Cron is only executed if a user visits your website.

Unix Cron uses the system’s clock, so you can schedule a time or how often a scheduled task is executed even if there are not any visits to your website.  

Nevertheless, setting up a scheduled task with WP Cron is relatively easy and doesn’t require knowledge of UNIX to configure a command to execute. 

Some use cases of WP Cron include: 

  • Import new products to your website 
  • Automate publishing of posts 
  • Send out email notifications for your subscribers or customers 

Setting up and managing WordPress Cron jobs with a plugin

There are various ways to setup and manage WordPress cron jobs. In the following example, I’ll use a WordPress plugin called WP Control, which will allow you to view and configure WordPress crons.  

With WP Control, you can: 

  • View all cron events, their arguments, when they’re next due to run, the action the event will take and how often the event will run.  
  • Add, Edit, delete, pause, resume, and immediately run cron events. 
  • Add and remove custom cron schedules. 

For this example, ’ll use WP Control to create a custom event using wp_mail. 

  1. Install WP Control: You can use the usual method of installing the plugin via wp-admin and then activate it.


  2. View Cron Events: When the installation has completed, head over to ‘Cron Events’ under the ‘Tools’ section. This is where you can view and manage your current cron events. The event names should be recognisable as they would reference part of your plugin name such as “ai1wm_storage_cleanup”


  3. Head over to Appearance > Theme File Editor > Functions.php This is where you will edit the Functions.php file and add the coding for the custom cron event. The following example will send an email using the wp_mail function when the cron has triggered. 
add_action( '20i_demo_cron', 'demo_function' ); 

function demo_function() { 

wp_mail( 'sales@20i-demo.com, '20i Demo Cron', 'Hosted By 20i!' ); 

}

Here is a breakdown of the above code snippet: 

add_action( '20i_demo_cron', 'demo_function' ); 

This line of code is adding an action hook in WordPress. In this case, we’re creating a custom action hook named ’20i_demo_cron’ and this is what will be used when adding the cron event using WP Control. 

function demo_function()  

This code defines a custom PHP function called demo_function. This will be called when ’20i_demo_cron’ is triggered. 


wp_mail( 'sales@20i-demo.com, '20i Demo Cron', 'Hosted By 20i!' );
} 

Inside demo_function , we’re using the built in WordPress function wp_mail to send an email to sales@20i-demo.com with the subject heading ‘20i Demo Cron’ and a message body of ‘Hosted By 20i!’  

  1. From ‘Cron Events’ > Click Add New: From here, you can create your new cron event. In the following example, I created an event name called ‘20i_demo_cron’, scheduled this to run immediately and to recur daily.  


  2. After clicking ‘Add Event’ this should trigger the event and if you check your inbox, you should have received the email from WordPress with the subject heading ‘20i Demo Cron’ and message ‘Hosted By 20i!’ 

Setting Up and Managing Basic WordPress Cron Jobs using WP-CLI

WP-CLI stands for WordPress Command Line Interface. It provides a way to manage your WordPress installation via command line rather than through the WordPress admin area. This is available on our hosting via SSH.  

  1. Access your hosting via SSH. If you’re a 20i customer, you can access your hosting via SSH by following this guide 
     
  1. When you’ve accessed your hosting over SSH, you will need to go into your WordPress installation using the ‘cd’ (change directory) command. In the following example, the WordPress install is in the public_html directory so I just need to run the following command: 
     
    cd public_html 
     

Now that you’re in the WordPress installation directory, you can now use WP-CLI to manage your cron events.

Here are some examples of commands you can use to manage your cron events…

List Cron Events

You can use the wp cron event list command to list all scheduled cron events on your WordPress site. After running the command, you will see four columns, Hook, next run time, countdown to next run time and recurrence. 

wp cron event list 

Run a Specific Cron Event

You can manually trigger a specific cron event to run using the wp cron event run command. Replace event-name with the name of the event you want to execute. 

wp cron event run event-name 


Schedule/Create a new Cron Event

You can create a new recurring Cron Event using wp-cli. The following command will create a new cron event to run immediately and reoccur daily.  
 
wp cron event schedule cron_event now daily 



By default, WordPress contains the following interval names, and you can use this as part of your command.  

  • hourly: Runs the event once per hour. 
  • twicedaily: Runs the event twice per day (every 12 hours). 
  • daily: Runs the event once per day (every 24 hours).

Delete a specific Cron Event

You can manually delete a cron event by running the following command. (replace event_name with the event you want to remove) 
 
wp cron event delete event_name

Optimising WP-Cron with UNIX scheduled task 

Using a UNIX scheduled task would be more reliable than using WP Cron. Like we mentioned above,  wp-cron.php is only triggered when a user visits your website.

With a UNIX scheduled task, you can schedule the task recur at certain intervals or you specify a time for the cron to run.  

On the other hand, if you have a high traffic website, WP-Cron could be executed multiple times which would put a strain on the server resources and slow down your website.

If you choose to use the UNIX method, rather than using WP Cron, you will need to add the following line of code to your wp-config.php file to disable WP Cron: 
 
define('DISABLE_WP_CRON', true); 

You can set up a cron job or ‘scheduled task’ in the Scheduled Tasks section of your hosting packages control panel. 

Manage Hosting -> Manage -> Scheduled Tasks 

You can add a wget command into the command field to call to wp-cron.php which is the function used to trigger your scheduled cron events. Something like the following will work 

wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron 

With My20i, you can run a scheduled task as often as every minute so you can be assured that your Cron Events are never missed!  

Further Optimisation tips 

  • Avoid Busy Times: If you’ve chosen to schedule a time for when WP-Cron is triggered, avoid busy periods to avoid disruption to your website’s performance.  
  • Streamline smaller scheduled tasks: Rather than having multiple small tasks scattered across different intervals, arrange for the task to run concurrently or at staggered times during off peak hours. 
  • Space out longer running tasks: If you have a longer running tasks and you need to execute this multiple times, ensure that you have allowed time for the process to complete before executing this again.  

Monitoring and Troubleshooting WordPress Cron Jobs 

There are various plugins you can use to monitor WordPress Cron Jobs and to see if they’re running.  An example one ‘WP-Cron Status’ this plugin logs all hooks running with WP-Cron and if they fail or complete.

You’ll be able to see what ran, when it ran, how long it took, and if it completed. If a WP-Cron hook fails, you will be notified of this soon after via email within 24 hours.  

If a WP Cron event fails to run, you can debug this by manually running the event. You can do this using WP Control > Head over to Cron Events > Hover over the Cron Event that you want to test > Click ‘Run Now’ and check the error logs of your website. 

In My20i, the error logs are stored in the logs & statistics section of your hosting control panel. 

Another method would be to use WP-CLI and run the WP Cron event with the –debug flag which will give you an output which would also contain specific error messages relating to the Cron Event which help you investigate.  

You can use the following command and replace ‘event_name’ with the cron event you want to test.  

wp cron event run event_name --debug 

Security and WordPress cron jobs 

Keeping your WordPress cron secure is very important for various reasons. Here are a couple of examples why: 

Data Protection: WordPress cron jobs can be used to handle sensitive data, such as exporting customer information or backup the websites database. It could be that this data is imported to an external storage facility and using SSL will encrypt the data sent between the two servers and cannot be easily intercepted by malicious actors during transmission.  

DDOS Attacks:  The default behaviour of wp-cron.php, if your website receives few visits an hour will be fine but for large websites with more traffic or that are frequently scanned by BOTs, the increase of HTTP/HTTPS requests can resemble a DDOS Attack on your website because of the additional load. To make this more secure, you can disable WP cron and create a UNIX scheduled task as mentioned above.  

Conclusion

In this article, we talked about how you can setup and manage WordPress cron jobs using a plugin and WP CLI.

WordPress crons have their disadvantages and hopefully this article has given you further insight as to why using a true UNIX cron would be better from a performance and security perspective.

How have your experiences been with WP Cron? Would you say that there are advantages using WordPress cron over a true UNIX cron?  

 

Add comment