How to Add Cron Jobs to WordPress

Posted on January 30, 2017 by in Tips & Tricks | 6 comments

How to Add Cron Jobs to WordPress

Cron jobs are something you use every single day, even though you might not know it. They run behind-the-scenes, powering all of your favorite websites. And they deserve respect!

In this post, I’m going discuss Cron jobs in general, tell you how WP-Cron jobs are unique, and show you a basic method for how to add Cron jobs to WordPress. I’m no battle hardened developer, so this post will primarily come at the topic from the view of a non-professional developer.

What Are Cron Jobs?

In a general sense, Cron is a utility that helps you schedule commands or scripts to run at a specific time. Cron jobs are the resulting tasks. So in human language, a Cron job tells your server something like “run example.php at midnight every day.”

Basically, Cron jobs are an easy way to set up tasks to run automatically, either on a one-time or recurring basis.

If you’re an average user, you benefit from Cron every day, whether you know it or not. For example, Cron jobs are what allow your backup tool to run an automatic backup at the same time every single day.

Without them, you’d be stuck having to manually click the back up button each and every time you wanted to create a new backup of your site.

How Do Cron Jobs Help WordPress?

I already mentioned one use for WP-Cron – backup plugins. But that’s far from the only use.

One of the most publicly visible implementations of WP-Cron is automatic updates. WP-Cron regularly checks for the latest software, theme, and plugin updates. And then it tells you to hit the dang update button so that your site keeps working!

Additionally, WP-Cron is also what handles publishing your scheduled posts, sending pingbacks and trackbacks, and an array of other potential features added by the plugins that you’re using.

WP Crontrol is just one plugin that helps with cron job setup

How is WP-Cron Different Than Regular Cron?

WordPress actually has its own system for running Cron jobs. And while the name might suggest that the two are identical, there are some very important differences between the two.

With normal Cron jobs, you can specify an exact date and time for each job to execute. For example, you can essentially say: “I want this script to execute at exactly 10:15:22 on August 8th, 2016.” And like clockwork, your script will execute at exactly that time.

But WP-Cron Works Differently…

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

But here’s the potential problem with WP-Cron…

If nobody visits your site over a period of time, there’s nothing telling WP-Cron to run and check for tasks which need to be completed.

Obviously, if your site is getting thousands of visits daily, this is unlikely to be an issue. But if your site only gets a few visitors a day but you still need a plugin’s Cron jobs to run every 30 minutes, you’re going to encounter problems.

For example, say a WordPress plugin tries to send out an automated email at 2 am. If no one visits your site until 6 am (a real possibility unless you have a global audience), then the email won’t send until 6 am assuming you’re not using a third-party service.

Additionally, sometimes caching plugins, DNS issues, or plugin conflicts can cause WP-Cron not to run.

There are ways to work around this issue, though. For example, Harish Chouhan detailing how he sets a true Cron job to call wp-cron and thus fire WP-Cron jobs at regular intervals.

How to Add WP-Cron Jobs

I’ll briefly cover how to add both basic recurring jobs as well as one-time scheduled jobs. Then, I’ll link you out to some resources covering how you can add even more complex WP-Cron jobs.

How to Add a Recurring WP-Cron Job

To add a recurring WP-Cron Job, most of the code snippets and arguments I reference will come from . So if you need further clarification on anything, I recommend that you start there.

An important thing to remember is that , not specific times. For example, if you schedule a task to start at 12 pm running on an interval of two hours, it will run first at 12 pm. Then again at 2 pm, and so on.

As an example, to add an hourly recurring task, you would use this framework:

register_activation_hook(__FILE__, 'my_activation');

function my_activation() {
    if (! wp_next_scheduled ( 'my_hourly_event' )) {
	wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
}

add_action('my_hourly_event', 'do_this_hourly');

function do_this_hourly() {
	// do something every hour
}

How to Add a One-Time WP-Cron Job

To add a one-time WP-Cron job, you can use the following code example as a base. Just like with the previous example, if you need more clarification, I recommend that you in the codex.

Again, remember that even if you schedule an event to run at a specific time, it will only run at that time if your WordPress site receives a visit around the same time.

As an example, to run a task one hour from the current time, you would use this framework:

function do_this_in_an_hour() {

    // do something
}
add_action( 'my_new_event','do_this_in_an_hour' );

// put this line inside a function, 
// presumably in response to something the user does
// otherwise it will schedule a new event on every page visit

wp_schedule_single_event( time() + 3600, 'my_new_event' );

// time() + 3600 = one hour from now.

Further Reading on Adding WP-Cron Jobs

If you want to dig deeper into how to add WP-Cron jobs, here’s some great further reading:

  • – Smashing Magazine has a very in-depth guide to adding WP-Cron jobs.
  • – Tom McFarlin explains how to bypass the issue of WP-Cron requiring visits to run.
  • – SitePoint has a great guide on using WP-Cron for scheduling events.

How to Add and View WP-Cron Jobs With a Plugin

Viewing WP-Cron jobs is a helpful way to debug your site/plugin or just get a good look at what’s running on your WordPress site.

To view all the WP-Cron jobs scheduled on your WordPress site, you can use a plugin called . As an added bonus, you can also use the plugin to add new WP-Cron jobs.

Just install and activate it like you would any other plugin. Then, you can view all of the existing WP-Cron events for your WordPress site by going to Tools → Cron Events:

WP Cron jobs

You can also easily add both regular Cron events and PHP Cron events at the bottom of that page:

And one other cool thing the plugin lets you do is define new intervals for Cron jobs. You can do that by going to Settings → Cron Schedules:

Wrapping Things Up

There it is! A quick and dirty introduction WP-Cron jobs. Honestly, unless you’re a developer, you’ll probably never need to know about WP-Cron jobs (though you will benefit from them). But, it’s always cool to get a look under the hood, right?

Have you tried to implement cron jobs in WordPress? Share your experiences here.

Article thumbnail image by Azad Ahmadov / shutterstock.com

6 Comments

  1. There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies.

    • It’s not a new technology. It’s something that WordPress have for a decade now))

  2. Good post, and Cron jobs are something that I’ve been looking into a lot more, particularly as speed and reliability have become more and more important in successful websites.
    It’s always been the case for consumers, but with SEO involving more focus on a fast site, it means even more longterm problems if Cron jobs start causing issues.

    One thing worth noting is that some hosting companies block WP-Cron jobs from operating normally due to the security/resource costs involved, and you may need to set them up via the hosting company (and disable the WP-Cron jobs from running).

    I wrote a fairly detailed guide having seen some of the issues created by not doing this with one internet host – – it had some serious negative effectives on SEO and traffic as it led to duplicate urls being constantly created for every piece of content on each affected website.

  3. Oh, great! I’ve used Windows since 1991 and have purposely avoided learning what “cron” was. I even learned how to back up my computer AND my websites automatically without knowing what “cron” meant. (I always suspected Cron was Conan the Barbarian’s kid brother.) Now that I am committed to learning WordPress I suddenly find out I need to know this stuff. Arghhh.

    But thanks for this article and all the other excellent tutorials you all provide.

  4. I’ve been creating and managing websites since 1997 (Joomla), now mainly on WP, met the word ‘cron’ too in some plugins advanced settings. Only today I understand what it can really do and help. schedule database extraction is sn example I can use with cron jobs. Thanks.

  5. Thanks for the article.
    For the WP cron, I use this useful module to create cron: .

500,591 Customers Are Already Building Amazing Websites With Divi. Join The Most Empowered WordPress Community On The Web

We offer a 30 Day Money Back Guarantee, so joining is Risk-Free!

Sign Up Today
купить футзалки киев

浮気

также читайте

Pin It on Pinterest