A Quick Guide to Using WordPress Cron Tasks

There are a lot of tasks involved in properly maintaining your WordPress websites. Staying on top of them all can be time-consuming and tedious. This is particularly true when you’re tackling them manually.

Fortunately, using the WP-Cron system, you can schedule tasks to occur at periodic fixed times, dates, or intervals. By automatically handling these responsibilities, you can free up your time to focus on other pertinent aspects of WordPress site maintenance.

In this post, we’ll explain what WordPress cron tasks are and how they work. Then we’ll walk you through how to create and schedule events in four steps and share some best practices. Let’s jump in!

An Introduction to WordPress Cron Tasks

Generally speaking, a ‘cron’ refers to a job scheduler that enables you to set tasks to occur at specific times. WordPress has its own built-in version called WP-Cron.

There is a wide range of tasks crons can be used for in WordPress. Some of the most popular examples include:

  • Checking for updates
  • Publishing scheduled posts
  • Deleting old comments from the trash
  • Automatically creating backups

As a developer, cron jobs serve to benefit you in a multitude of ways. Primarily, they help you save time by streamlining your workflow.

Also, scheduling recurring events eliminates the need to remember to perform the functions yourself. This can improve the overall security and performance posture of your site, particularly when it comes to critical tasks such as running backups and updates.

How WordPress Cron Operates

WP-Cron jobs work in the background. They send a command to your site’s server to perform a task at a scheduled time or interval.

For the cron event to kick off, someone needs to visit your site. WP-Cron then checks for scheduled tasks. If it’s time for one to be carried out, or if the specified time has passed, the system then executes the actions for that job.

WP-Cron runs using two files. The first is cron.php, which is located in your site’s wp-includes directory. It houses all the functions needed for scheduling your events. The second is wp-cron.php, which is responsible for executing WordPress cron tasks.

WordPress uses the wp_schedule_event function in your functions.php file to schedule recurring tasks. There are three required parameters involved:

  • $hook. The hook is the name given to a function. A custom hook is needed to execute a task. It would look something like ‘add_action( ‘my_hookname’, ‘my_function’ );‘.
  • $recurrence. This refers to how often you want the event to occur. The default options are ‘hourly’, ‘twicedaily’, and ‘daily’. It would look something like ‘wp_schedule_event( time(), ‘daily’, ‘my_task_hook’ );‘.
  • $timestamp. This is the date and time you want the scheduled event to first happen. WordPress uses UTC/GMT time. You can read more about time constants in the WordPress Codex.

A fourth and optional parameter is $arg. This is for arguments you may want to add to the hooked functions, such as a post or user ID.

How to Schedule WordPress Cron Tasks With a Plugin (In 4 Steps)

As we’ve touched on, you can manually add code to your files to create and manipulate cron jobs. However, the easier method, especially when you’re first getting started, is to use a plugin. Let’s take a look at how you can use one to schedule events.

Step 1: Download the Advanced Cron Manager Plugin

There are a handful of plugins available designed to help you manage cron tasks with ease. However, it’s important to choose one that is regularly updated.

We recommend using the Advanced Cron Manager plugin:

The Advanced Cron Manager WordPress plugin.

This plugin is available in both a free and a premium version and has a user-friendly interface. With Advanced Cron Manager you can modify, add, delete, and run WP-Cron events.

You can also manually execute, pause, or unschedule any event. In addition to individual and recurring tasks, you can use the plugin to customize your job schedules.

You can install Advanced Cron Manager directly from your admin dashboard or download it from the WordPress Plugin Directory then upload it to your site. Once you’ve added it, be sure to activate it.

Step 2: View Your Cron System and Schedule

After you install and activate the plugin, you can find it under Tools in your admin dashboard. When you click on Cron Manager, it will bring you to an overview where you can view all your scheduled WordPress cron tasks:

The Cron Manager page of the Advanced Cron Manager WordPress plugin.

It will tell you what each event is, its schedule, and the next time it’s set to run. To the right, you’ll have the option of enabling a server schedule. You can also filter tasks by intervals. For example, you can see which jobs occur hourly, daily, etc.

Under each event, you’ll find four links. If you click on Details, you’ll see a dropdown box where you can learn more about the task. If you’re using the Pro version of the plugin, you can find additional insights in the Logs tab.

In the Implementation tab, there will be a code snippet for the event that you can copy and paste to use in your .php files:

The Implementation tab of the Advanced Cron Manager WordPress plugin.

The next link is Execute Now, which you can use to run the event immediately instead of waiting for the indicated schedule. The Pause link lets you suspend the task until you decide to run it again.

Finally, you can click on the Remove link to take the event off your schedule or eliminate it from your WP-Cron system entirely. You can perform Bulk Actions to either Execute Now for all tasks or Remove them simultaneously.

Step 3: Create a New Cron Event

Once you familiarize yourself with the event system and schedule, you can use it to create your own tasks. For this demonstration, we’ll create a job to automatically update our WordPress plugins on a daily basis.

The first step is to make sure the task is not already on the cron schedule. You can do this by going to Tools > Cron Manager, then searching for the event you’re looking to add.

Next, click on the Add new event button at the top of the page:

The 'add new event' button from the Advanced Cron Manager WordPress plugin.

It will open a panel from the right side of the window:

The panel to add details for a new event in the Advanced Cron Manager WordPress plugin.

Under the Hook section, you can add a name to identify the task. Keep in mind you should only use lowercase letters, numbers, and underscores. In this case, the action is to update our plugins, so we’ll name it wp_update_plugins.

Next, you can choose the date it will first execute. If you leave this section empty, WP-Cron will automatically run the job the next time it’s queued (or the next time someone visits your site).

Under Schedule, you can choose how often the event will occur: hourly, once daily, or twice daily. If you want it to be a one-time event, choose Don’t repeat. For this example, we’ll choose once daily.

Once you’re done, you can click on the Add event button. Your task will now be included in the Cron Manager schedule.

Step 4: Add a Corresponding Action Hook for Your New Cron Event

When you create a cron job, you need to tell WordPress what to do when that event is triggered. To do so, it’s critical to add a custom PHP hook and function.

So, continuing with our example from above, we named our hook wp_update_plugins. Now we need to associate that hook with the function in our functions.php file. You can get the code for this under Details > Implementation for the event as we described in Step 2.

Our code would be as follows:

function cron_wp_update_plugins_feb4bf82() {
    // do stuff
}

add_action( 'wp_update_plugins', 'cron_wp_update_plugins_feb4bf82', 10, 0 );

Simply copy the code and navigate to your functions.php file. You can do so via the WordPress theme editor or using File Transfer Protocol (FTP) and an FTP client such as FileZilla. Paste the code and save your changes.

Tips and Best Practices for Using WordPress Cron Tasks

The more you familiarize yourself with the WP-Cron system, the easier it will be to use it to streamline your workflow. However, to get the most out of this tool, there are a few best practices to keep in mind.

Scheduling repetitive tasks using cron may save you time, but you don’t want to overdo it. Adding too many complex events can hurt the performance of your site. It’s best to keep jobs as simple as possible.

Also, as you learn more about cron tasks, you may want to try manually adding and scheduling them beyond the Advanced Control Manager plugin, such as through cPanel or your wp-config.php file. Some developers choose this route because they want additional customization options such as modified event intervals.

While you can customize event intervals beyond the default options mentioned earlier, be cautious with this approach. For example, you could set a job to run every minute. However, initiating tasks this frequently adds excessive server overhead.

Finally, make sure your cron system is configured to alert you by email any time there’s an issue. This way, you can correct it in a timely manner. The Pro version of Advanced Control Manager lets you define your notification options so you’re aware if an event execution fails.

Conclusion

As a WordPress developer, part of maintaining an efficient workflow is automating repetitive tasks when and wherever possible. WP-Cron makes it easier for you to do just that.

In this post, we explained how you can schedule WordPress cron tasks using a plugin in four simple steps:

  1. Install and activate Advanced Cron Manager.
  2. View your cron system and schedule.
  3. Create a new cron event.
  4. Add the corresponding action hook for your new cron event.

Do you have any questions about using WordPress Cron tasks? Let us know in the comments section below!

The post A Quick Guide to Using WordPress Cron Tasks appeared first on Torque.

Sharing is Awesome, Thank You! :)

Share this Blue 37 post with your friends
close-link