Skip to Main Content

How to add a cron job on WordPress

Divi: the easiest WordPress theme to use

Divi: The best WordPress theme of all time!

With over 901.000 downloads, Divi is the most popular WordPress theme in the world. It is complete, easy to use and comes with more than 62 free templates. [Recommended]

"Cron" tasks are something you use every day, even if you can not know it. They run in the background, and are used on most websites.

In this tutorial, I'll show you how to use Cron tasks, tell you how Cron tasks are unique, and show you a basic way to add Cron tasks to WordPress.

But before, if you have never installed WordPress discover How to Install a WordPress Blog in 7 Steps et How to search, install and activate a WordPress theme on your blog 

Then back to why we are here.

What is a Cron job?

In a general sense, a "Cron" job is a utility that allows you to schedule commands or scripts to be executed at a specific time. Cron jobs are the resulting jobs. So in human parlance a Cron job gives the following message to your server: " run example.php at midnight every day. " How to add wordpress cron job

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

Also discover our guide on How to open a Youtube video in a popup on WordPress

If you are an average user, you benefit from "Cron" jobs, sometimes without knowing it. For example, Using Cron Jobs is a tool that will allow you to make backups of your blog at the same time every day.

Without them, you would have to manually click the backup button each time you wanted to create a new backup of your website.

How Cron Jobs Work in WordPress?

I have already mentioned the use of Cron jobs is perfect for backing up a blog. But that's far from the only use case.

One of the most publicly visible implementations of Cron jobs is the automatic update. "WP-Cron" regularly checks for the latest updates of software, themes and plugins. And then it notifies you of updates!

Also read our guide on How to add a drop-down contact form to your global header

In addition, it is also WP-Cron which also handles the scheduling of your articles, sending pingbacks and trackbacks, and a range of other potential features added by the plugins you use.

How is WordPress Cron different from a classic Cron?

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

With classic Cron jobs, you can specify an exact date and time for each job to run. For example, you could basically say, “I want this script to run at exactly 10:15:22 on August 8, 2019.” And like clockwork, your script will run at exactly that time.How is cron wordpress different from classic cron?

But WP-Cron works differently ...

WP-Cron doesn't work that way. Instead, WP-Cron only works when someone visits your website. Every time someone visits it, WP-Cron checks if there are any scheduled tasks that need to be executed. If it finds that a task's scheduled time has been reached, it will run that task.

But here is the potential problem with WP-Cron.

If no one visits your website for a period of time, WP-Cron will not be able to verify and perform the tasks that need to be completed.

Easily create your website with Elementor

Elementor allows you to easily create any website design with a professional look. Stop paying expensive for what you can do yourself. [Free]

See also our article on 6 WordPress plugins to breathe new life into your articles

Obviously, if your website has thousands of hits a day, it's unlikely to be a problem. But if your website receives only a few visitors a day, and you want to run tasks every 30 minutes for example, you will encounter a lot of problems.

For example, telling WordPress plugin to attempt to send an automatic email at 2 a.m. If no one visits your website until 6 a.m. (a real possibility unless you have a global audience), the email will not be sent until 6 a.m. assuming you are not using a third-party service.

In addition, sometimes caching plugins, DNS problems, or plugin conflicts can prevent Cron jobs from running.

Discover also our 10 WordPress plugins to optimize your blog

But, there are ways to get around this problem.

How to add Cron jobs?

I would briefly cover how to add the two basic recurring jobs as well as the one-time scheduled tasks. So, I'm going to send you to resources covering how you can add even more complex tasks with WP-Cron. How to add cron jobs

How to add a task recurrent WP-Cron

To add a WP-Cron recurring task, we will use code snippets. I hope you remember how to create a WordPress plugin. So if you need clarification on anything, I recommend you start there.

An important thing to remember is that using WP-Cron works at intervals, and not at specific times. For example, if you schedule a task that should start at 12 o'clock with an interval of two hours, it will run first at 12 o'clock, then again at 14 o'clock, and so on.

As an example, to add an hourly recurring task, you must use this code:

register_activation_hook (__ FILE__, 'my_activation');
 
function my_activation () {if (! wp_next_scheduled ('my_events')) {wp_schedule_event (time (), 'hourly', 'my_event'); }} add_action ('my_events', 'do_this_each_time_time'); function do_this_each_time () {// your code}

How to add a single WP-Cron task

To add a WP-Cron job, you can use the following sample code as a basis. Just like with the previous example, if you need more clarification, I recommend you check out the WordPress codex.

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

As an example, to run a task at a current time, you can use this code:

function do_this_in_a_time () {// Your code} add_action ('my_events', 'do_this_in_a_time'); wp_schedule_single_event (time () + 3600, 'my_events'); // time () + 3600 = One hour from now.

How to add and view Cron tasks with a plugin

Viewing WP-Cron Jobs is a useful way to debug your website / plugin or just to get an overview of what is running on your WordPress website.Wp crontrol plugin

To see all the WP-Cron tasks programmed on your WordPress website, you can use a plugin called " WP-Crontrol ". As an added bonus, you can also use this plugin to add new WP-Cron tasks.

It is enough install and activate it as you would any other plugin. Then you can see all existing WP-Cron events for your WordPress website by going to " Tools → Cron Events "

Are you looking for the best WordPress themes and plugins?

Download the best plugins and WordPress themes on Envato and easily create your website. Already more than 49.720.000 downloads. [EXCLUSIVE]

Wp cron jobs plugin tool

You can also add regular events at the bottom of the page:

Add cron events

And another thing that this plugin does well. It allows you to define new intervals for WP-Cron tasks. You can do it by going into Settings → Cron Shedules "

How to configure cron jobs

Finally

So that's a quick introduction to using WP-Cron. Honestly, unless you are a developer, you probably will never have to deal with the tasks of WP-Cron. But, it's always cool to have a peek under the hood, isn't it?

Discover also some premium WordPress plugins  

You can use other WordPress plugins to give a modern look and optimize the grip of your blog or website.

We offer you here some premium WordPress plugins that will help you do that.

1. W8 Contact Form

W8 Contact Form is one of the most popular contact form plugins on WordPress. It offers plenty of features such as submission to multiple recipients, animation support, unlimited form creation, different styles, and a wide variety of additional customization options.

W8 contact form wordpress contact form plugin

In addition, it gives your website or blog a professional system to make it easier to get in touch with your customers. You will be able to customize all of your forms according to your needs and your tastes.

Download | Demo | Web hosting

2. WooCommerce Currency Switcher

The WooCommerce Currency Switcher plugin allows you to change currencies and get the converted rates in real time (prior WooCommerce installation required). You can use currency aggregators to get converted rates either by Yahoo, Google, AppSpot aggregators, either manually.Woocommerce currency switcher

WooCommerce Currency Switcher is available as a widget and works in any space provided (shortcode available -> [woocs]). On the public interface, "Currency Switcher" can be presented in 5 different ways to discover.

Download | Demo | Web hosting

3. Ultimate Membership Pro

Ultimate Membership Pro is an excellent WordPress subscription and content restriction plugin. It can manage users according to their package (free or paid) by creating exclusive access levels.

Ultimate membership pro wordpress membership plugin

It will therefore be possible to protect all the content of your website, or just part of it. It will be for example a course, a lesson, a page, a product, a category, an image, etc ...

See also our guide on How to create PowerPoint slideshows on WordPress

Its main features include: content protection, multiple levels of access, support for multiple payment gateways -PayPal, Authorize.net, Stripe, 2CheckOut, Bank Transfer-, partial content protection, and much more again…

Easily create your Online Store

Download free WooCommerce, the best e-commerce plugins to sell your physical and digital products on WordPress. [Recommended]

Download | Demo | Web hosting 

Other recommended resources

We also invite you to consult the resources below to go further in the grip and control of your website and blog.

Conclusion

Here ! That's all. We hope this tutorial helps you create WP-Cron jobs on your WordPress blog or website. Hoping that this tutorial was useful to you, we invite you to the share on your favorite social network.

However, you will also be able to consult our resources, if you need more elements to carry out your projects of creation of Internet sites, by consulting our guide on the WordPress blog creation.

Tell us about your Comments and suggestions in the dedicated section.

... 

This article features 1 comment

  1. Hello !
    I work on a dropshipping site between Woo Commerce and Bigbuy, For a while I can't move forward because of the cron. I found your article interesting can you help me?

Leave a comment

Your email address will not be published. Required fields are marked with *

This site uses Akismet to reduce unwanted. Learn more about how your comments data is used.

Back To Top
6 shares
Share4
tweet1
Save1