How to run a Cron Job with WordPress

Last update:

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.

Table of Contents

Understanding WordPress Cron Job

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

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.

Cron Job with WordPress increases Page Load Time
WP Cron increases Page Load Time

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.

You can learn how to disable your WordPress cron in this post or get a FREE migration to a Wetopi WordPress specialized server. With wetopi your servers are tuned for performance and by default, call the wp-cron from an external service every 20 seconds.

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 has over 100,000 active installs with an impressive 4.9 out of 5 rating.

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 …


Changing Cron Job schedules with WP-Crontrol plugin

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:

Changing Cron Job schedules is on Menu Settings

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

Adding different Cron Job WordPress schedule intervals with WP Control plugin
New Cron Schedule form to add additional intervals

Adding an Event to run our Job

Click into “Cron Events.” under Tools:

WordPress Cron Job Events Menu item

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( 'hello@world.com', 'Sending an automated email', 'Hello World' );
}

Then we click on the “Add Cron Event.”

Add WordPress Cron Event Form

The Hook 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 can run a Cron event hook immediately by simply clicking on the “Run Now” you’ll find in the last column. This can be helpful when troubleshooting things as you might need to run a Cron event multiple times.

You can also edit existing Cron events by clicking on “Edit” next to the action name.

You may also enjoy Disabling Wp-Cron.

We are techies passionate about WordPress. With wetopi, a Managed WordPress Hosting, we want to minimize the friction that every professional faces when working and hosting WordPress projects.

Not a wetopi user?

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

See how Wetopi stacks up against your current hosting

Try before you buy.

With no obligation on your part, we’ll migrate a copy of your website:

No hidden small text.
No commitments.
No credit card.