Sélectionner une page

How to Add Cron Jobs to WordPress

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 has a nice post 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 this part of the WordPress codex. So if you need further clarification on anything, I recommend that you start there.

An important thing to remember is that WP-Cron jobs run on intervals, 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 check out this page 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:

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 WP-Crontrol. 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

Source link

Poster le commentaire

Votre adresse e-mail ne sera pas publiée.