Recently I had to export data from a mailing list for one of my customer. So I searched the web for WordPress Cron. Thinngs are not that hard but not really clear either and easy to understand what is what. Let’s clarify all of this right now.
What is a WordPress Cron ?
A WordPress Cron is a system that automates scheduled tasks within a WordPress site, such as publishing scheduled posts, checking for updates, or sending email notifications. Unlike traditional server-based cron jobs, WordPress Cron runs tasks when a user visits the site, ensuring functionality even without direct server access. This makes it a convenient tool for managing repetitive processes seamlessly.
For more information, you can have a look on the WordPress Developer Resources.
How to implement ?
First of all, we need to define the periodicity of our WordPress Cron. We can have to run our task every day at a particular time, every Monday, or even every first day of the month.
In WordPress, it’s much easier than in Linux, you just have to define a function and add it to the cron_schedules filter.
Add the following pieces of code in your functions.php theme file.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php function myNewSchedule( $schedules ) { $schedules['every_month'] = array( 'interval' => 60, // in seconds 'display' => "Every minute" ); return $schedules; } add_filter( 'cron_schedules', 'myNewSchedule' ); ?> |
Now we have defined our periodicity, we need to add it to the Cron table to be run.
1 2 3 4 5 6 7 |
<?php if ( ! wp_next_scheduled( 'myNewSchedule' ) ) { wp_schedule_event( time(), 'every_month', 'myNewSchedule' ); } ?> |
Now is the trickiest part., we’re about to define what our new Cron will do !
1 2 3 4 5 6 7 8 9 10 |
<?php // Parameters are the name of our new schedule, and then the name of the function to link. add_action( 'myNewSchedule', 'myNewSchedule_func' ); function myNewSchedule_func() { // Doing some stuffs. } ?> |
In another post, I wil explain how to query the WordPress Database, don’t worry, it’s not that complicated.
Potential Performance Issues
While WP-Cron is convenient, it can lead to performance issues, especially on high-traffic sites. Since WP-Cron is triggered on every page load, sites with significant traffic may experience increased server load, potentially slowing down performance. Conversely, sites with minimal traffic might miss scheduled tasks due to the lack of page loads.
To check if performance issues are due to WordPress Cron, you can temporarily disable them.
Find the following line in your wp-config.php file and add / update like that.
1 2 3 4 5 |
<?php define('DISABLE_WP_CRON', true); ?> |
Great post! I had no idea that WP-Cron could be a potential performance issue for high-traffic sites. I followed the advice to disable WP-Cron and set up a server cron job, and it’s made a noticeable difference in my site’s speed. Very useful tips!
Really insightful article! I was struggling with scheduled posts not running on my site, but after reading this, I realized WP-Cron was the culprit. The advice on setting up custom cron jobs is exactly what I needed. Thanks for making it so easy to understand!