How to run a Cron Job with WordPress

Cron is a Linux server service that is typically used to schedule commands or scripts on a web server that runs in the background. A cron job is the task itself, which is used to schedule tasks at periodic fixed dates, times, or intervals. Typically these involve repetitive maintenance tasks that are automated to save time.

In WordPress, we have our own cron called WP-Cron, which is used to simulate a system cron.

Understanding WordPress Cron Job

WP-Cron, by default, only runs when someone visits your site. Whenever someone visits your site, WP-Cron checks if there are any scheduled tasks to be run. If it finds that a task’s scheduled time has been met, it will go ahead and run that task.

If you are looking for performance, it’s a good idea to disable the WP-Cron internal process dispatcher and call it from an external service.

At wetopi, by default, we call the wp-cron from an external cron service every 20 seconds. You can learn how to disable your WordPress cron in this post.

How to run a WordPress Cron Job

To run a WordPress cron with WP-Cron events you have to create custom hooks.

If you are an advanced user we recommend the official WordPress Plugin Handbook guide on scheduling WP Cron events.

For those who perhaps aren’t as advanced, we will describe the process using the free WP Crontrol plugin

WP Crontrol lets you view and control what’s happening in the WP-Cron system. From the admin screens you can: View all cron events, add new ones, edit, delete, etc …

WP-Cron Schedules

Once WP Crontrol plugin is installed and activated you can change the WordPress Cron job schedules under “Cron Schedules”. This option is within the settings of your WordPress admin.

You can add additional schedules in seconds, such as 600 seconds for every 10 minutes.

Adding an Event to run our Job

Click into “Cron Events.” under Tools:

Cron Events

Many of the action name’s of the events are easily recognizable as they should match part of the task name, such as “wp_update_plugins” or “wp_update_themes.”

In this how-to, we are going to To add a WordPress Cron job to make the simple task of sending an email.

This is a new action, so we will have to add the action and place the function code in our functions.php file:

add_action( 'send_mail', 'send_mail_func' );

function send_mail_func() {
    wp_mail( '[email protected]', 'Sending an automated email', 'Hello World' );

Then we click on the “Add Cron Event.”
The Action Name, must be the same we have added to functions “send_mail”, the we set the desired start Time in “Next Run” and the recurrence based in our Schedules.

Our new send_email Event hook ready to run

You may also enjoy Disabling Wp-Cron

At wetopi, we want to minimize the friction that every professional face when working and hosting WordPress projects.

In short, we are just techies passionate about WordPress. With wetopi, a specialized WordPress hosting, we want to go further to offer professionals and agencies a platform to work efficiently and effectively.

Not a wetopi user?

Try now – you’ll find an efficient way to work with WordPress

Free full performance servers for your development and test.
No credit card required.

1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 4.55 out of 5)
How to run a Cron Job with WordPress
Share this post