What is WordPress Cron Job and How to Create One

Cron is a technology popularly used to run scheduled tasks on web servers. WordPress has a built-in cron function that it uses to run automatic updates, publish scheduled posts, etc. 

When you are running a blog, sometimes you have to publish posts on special days (for example, Christmas and other holidays). With WordPress Cron it is possible to schedule those posts so that it will be published automatically on your designated time. But how do you customize your Cron tasks?

In this article, we’ll discuss what is WordPress cron and how effective it is in 2022. We will also learn how to customize your own cron tasks and publish scheduled posts. So, let’s get started.

What is WordPress Cron? How does it work?

Many of us don’t understand what is WordPress cron, but it is used very often. It is used to run scheduled server maintenance tasks. WordPress also has some default Cron tasks built into it. It uses Cron to perform tasks like checking for updates, deleting old comments from the trash, etc.
You probably might not have noticed this before, but some of your plugins might be using your WordPress cron for various tasks. For example, some WordPress backup plugins use cron technology to make scheduled backups of your WordPress website.

Can Cron slow you down?

By now, you might think that cron is an amazing technology with a lot of uses. Yes, it’s true that cron itself is very useful, but if some plugins use your cron irresponsibly, then it might slow your website down, especially if you’re on shared hosting.

If your WordPress website is running slow, then it could be that some plugins are running resource intensive tasks. If that is the case, then you need to quickly identify this issue and fix it.

Don’t worry, there’s a way you can look into your cron activities and also control those without coding. Let’s see how you can do that.

Get Latest Post Notifications!

Subscribe to our newsletter

View and control WordPress Cron activities

Now that we discussed what is WordPress Cron, let’s learn how to view and control its activities. The simplest way to view and control your WordPress Cron activities is by using a plugin called “WP Crontrol”. You can install it from the WordPress dashboard via wordpress.org. Once the plugin is installed and activated go to Tools > Cron Events from your WordPress admin dashboard.

accessing and understanding what is wordpress cron events

Once inside the Cron Events menu, you will see a list of all the cron tasks that are currently on your WordPress website. On the left side, you will see the hooks that are running your cron events. These hook names will give you an idea of what a particular cron task does. 

Generally, a native hook name starts with the ‘wp’ prefix such as wp_update_plugins, wp_version_check, wp_update_themes etc.

list of cron tasks

Some plugins might use the wp prefix while some plugins use their own custom prefix. For example, the All In One SEO plugin uses “aioseo” as its prefix.

The other columns are Arguments, Next Run, Action, and Recurrence. All of these are self-explanatory. By looking at the next run, you’ll understand when that particular task will happen and by looking at the recurrence data, you can find out how often the task is carried out.

Important Notice: Before proceeding, don’t change the default WordPress cron tasks as they could leave your website severely vulnerable or might even cause malfunctions.

Now, we want to find a cron event that takes more resources to run and is slowing your website down. Start with checking the plugins’ in-built options and see if there’s any way of controlling it from there. If not, then click on the ‘Edit’ button on cron events.

editing a cron event

Clicking on the Edit button will open up a new tab called ‘Edit Cron Event’. Here you can configure the settings of a particular hook.

edit cron event

If the recurrence takes place quite often, you can change it so that fewer resources will be used.

How to setup cron job on WordPress – Adding your own cron task

Now that we know how to view and edit the cron tasks on your WordPress website, let’s see how to set up a cron job on WordPress. If you want to add your own cron tasks, you can use this same plugin. WP Crontrol allows you to easily set your own cron tasks. Like before, go to Tools > Cron Events from your WordPress dashboard. On the top, you’ll find a button ‘Add New’. Click on it and it will open a new tab where you can create your own cron event.

adding a new cron event

Once the new tab is opened, you can configure your new cron task. First, select your cron event type. First, let’s setup the standard crone event and then move on to the PHP cron event.

Then give your event a hook name and if your function requires arguments, you can also include it. Then you need to set a time when the cron will execute this task. You can select Now, Tomorrow, or your custom date. Then set the recurrence of the cron event. Now you can click on ‘Add Event’ to create your new event but it will not do anything since we haven’t specified any function.

adding a custom cron event

This is the part where a bit of coding is required. But don’t worry, it’s simple. For our custom cron task, we are going to send an email. So, select the PHP cron event from the event type. Then type in the following code to attach your hook with a function:

add_action( wp_custom_event’, ‘my_function’ );

Now our custom hook is connected to a function called ‘my_function’. To define this function to send an email, type in the following code:

function my_function() {
    wp_mail( '[email protected]', 'WP Crontrol', 'This is an email from WP Cron' );
}
adding php code for cron event

Here, the part ‘[email protected]’ is used for an example only. You can replace it with an actual email address. To test out cron, you can use your own email address.

And that is how you setup cron job in WordPress. However, we used a function that sends an email but in order to perform other cron tasks, you need some programming skills. If you want to learn more about what is WordPress cron, then you can read this handbook from WordPress.org.

Conclusion

With the cron function, you can do a lot. You can schedule your posts, you can schedule emails, etc. By default, WordPress takes care of all the updates and other important tasks using the cron function. You can also use WordPress cron through the cPanel/control panel provided by your hosting service.

We hope you found this article helpful and now you understand completely what is WordPress cron, if you liked this article or have any questions leave those in the comments below!


Disclaimer: This post may contain affiliate links and we may receive a small commission if you purchase something by following them. However, we recommend services/products that we believe good to serve your purpose.

Leave a Reply

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