WordPress Cron Jobs | How To Setup & Control Settings

Technically, you should always make sure your website can run scheduled WordPress Cron Jobs effectively. Your WordPress website needs to perform scheduled tasks like checking for updates. These tasks use a system called cron jobs. Sometimes your hosting environment may block them from running.

Or rather, you may accidentally end up stopping WordPress cron jobs as well. In that case, you’ll need to make sure that WordPress can run these scheduled tasks to improve your site health score. In this article, we’ll have a look at what these WordPress Cron Jobs are and why they matter to SEO webmasters.

Recently, one of our readers asked if it was possible to view and control the WordPress cron job system in the dashboard. And, as such, this guide is a good fit for both the reader and other curious beginners who want to learn more about how a site cron job works, how to view and control WordPress cron jobs, etc.

What Is WordPress Cron Jobs?

WordPress Cron Jobs is a technology to run scheduled tasks on a given website server. WordPress comes with its own built-in cron that allows it to perform scheduled tasks such as checking for updates, publishing scheduled posts, etc. Cron is a technical term used for commands to run on scheduled time or at regular intervals.

Most web servers use it to maintain the server and run scheduled tasks. WordPress comes with its own cron system which allows it to perform scheduled tasks. For example, checking for updates, deleting old comments from trash, etc. Plugins can also use to perform tasks specified by you.

In other words, Cron is a Unix/Linux utility that is typically used to schedule commands or a script on a web server that runs in the background. A cron job is a task itself, which is used to schedule tasks at periodic fixed times, dates, or intervals. Typically, these are repetitive tasks that are automated to save time. In WordPress, this is handled by WP-Cron, by simulating a system cron.

For example, your WordPress backup plugin can use WordPress cron to automatically create backups at a given schedule. Irresponsible use of WordPress cron by plugins can slow down your website. Especially, if you are on shared hosting. If a plugin frequently performs resource-intensive tasks, then you need to identify the issue and fix it.

How To View & Control The WordPress Cron Jobs System

Let’s take a look at how to view and control the WordPress cron system without writing any code. In order to view and control the WordPress Cron Jobs system, the first thing you need to do is install and activate the WP Crontrol Plugin, to begin with. For more details, you can see the step-by-step guide on how to install a WordPress plugin.

Upon activation, you need to visit the Tools » Cron Events page to control cron settings. You will see a list of all cron events scheduled to run on your site using the WordPress cron system. In the first column, you will see the name of the hook that runs the cron. Hook names usually give you a hint at what this particular event does.

Most default WordPress hooks begin with a wp_ prefix, like wp_update_plugins, wp_update_themes, etc. Your WordPress plugins may or may not use their own prefixes for their hooks. For example, All in One SEO uses aioseo_ prefix. You’ll also get to see when a cron will run next, and the time interval between the next run.

Related Topic: WordPress Discussion Settings | Allow Comments or Not?

The last column on the list allows you to edit, delete, or run a cron event. It’s important to note that you must be very careful about what you do with cron events and never delete a default WordPress cron event. Now let’s suppose you see a cron event created by a WordPress plugin that is quite resource-intensive.

First, you should check the plugin’s settings to see if there is an option to control it from there. If there isn’t, then you can click on the ‘Edit’ link next to the cron event to change it. Clicking on the Edit button will open the ‘Modify cron event’ tab down below. Here you can change how often you want the event to run. Once you are done, click on the save changes button to store your settings.

How To Add Your Own Cron Events In WordPress

WP Control plugin makes it easy to add your own cron jobs to WordPress. Simply visit the Tools » Cron Events page and scroll down to the ‘Add Cron Event’ tab. First, you need to provide a hook name for your cron event. Hook names cannot have spaces or special characters. If the function you want to execute requires arguments, then you can provide those arguments.

Next, you need to tell WordPress when to run the cron next time. You can enter ‘now’ which will trigger cron immediately, ‘tomorrow’, ‘+2 days’, or ’25-02-2020 12:34:00′. Lastly, you need to select a schedule. You can select hourly, twice daily, daily, or once a week. You can also make it a non-repeating event. Once you are done, click on the Add Cron Event button to save your changes.

You’ll notice that your cron event will now appear in the events list. However, currently, it does nothing because you haven’t told WordPress what to do when this event is triggered. Instead, you’ll need to add your hook and a function that runs when the cron hook is triggered.

1
2
3
4
5
add_action( 'wpb_custom_cron', 'wpb_custom_cron_func' );
function wpb_custom_cron_func() {
  wp_mail( '[email protected]', 'Automatic email', 'Automatic scheduled email from WordPress to test cron');
}

Don’t forget to use your own email address. This function simply sends a test email to you when the cron runs. You can now scroll up the page and click on the ‘Run Now’ link next to your cron event to test it out. You should also note that using cron requires intermediate-level programming and WordPress development skills.

WP-Cron Schedules

Once activated you can change the WordPress Cron job schedules under “Cron Schedules” from within the settings of your WordPress dashboard. Notice that the plugin adds an additional default schedule of weekly. You can also add additional schedules in seconds, such as 21600 seconds for every 6 hours. These intervals can also be added with code using a filter, such as:

add_filter( 'cron_schedules', 'example_add_cron_interval' );

function example_add_cron_interval( $schedules ) {
 $schedules['five_seconds'] = array(
 'interval' => 5,
 'display' => esc_html__( 'Every Five Seconds' ),
 );

return $schedules;
 }

WP-Cron Events

You can then also use the plugin to see your current WordPress Cron jobs that are already scheduled. Under Tools click on “Cron Events.” Many of the action names are easily recognizable as they should match part of the plugin name. Such as “woocoomerce_cleanup_sessions” or “gravityforms_cron.”

Takeaway,

It’s important to note that WP-Cron is slightly different than a system cron and they each have their own pros and cons. For example, WP-Cron relies entirely on intervals, whereas a system cron relies on specific times. A WP-Cron also only works when the page is loaded (back-end or front-end), which could be less reliable.

The default intervals provided by WordPress are hourly, twice daily, and daily. As I mentioned, some examples of a WordPress cron job might involve scheduling a post to publish or a backup plugin running on a predefined schedule. To schedule WP-Cron events you have to create custom hooks.

Related Topic: Why Are WordPress Post Revisions important? How it works

Finally, I recommend checking out the official WordPress plugin handbook which has an amazing guide on scheduling WP-Cron events. Whilst, for an alternative way to set up a system cron with WordPress I recommend checking out Tom Mcfarlin’s article on defining a WordPress cron job.

That’s it! You are now ready and set to start utilizing this WordPress feature. But, if you’ll need more support, you can Consult Us and let us know how we can sort you out. Likewise, feel free to share your additional opinions, thoughts, contributions, suggestions, recommendations, or even questions in our comments section. Don’t forget to share with other weblog readers.


Trending Content Tags:


Please, help us spread the word!