Cron in WordPress

Cron is a standard UNIX utility for running scheduled tasks at a specific time, date, or interval.

However, WordPress Cron is a bit different from the standard utility, and so you need to understand what Cron is in WordPress and how it will work on your site before delving into the actions and process.

What is a WordPress Cron?

WordPress has its own Cron to handle tasks such as checking for updates, scheduling a publication, clearing the shopping cart, and many others. All WordPress Cron tasks are handled through WP-Cron.

By default, WordPress supports the following time intervals:

  • Every hour.
  • Twice a day
  • Daily
  • Weekly

You can add your own interval, and to do this you need to filter cron_schedules. As an example, let’s add a thirty-second interval:

// Add a filter.
add_filter( 'cron_schedules', 'wpz_cron_30s' );

// Describe the function.
function wpz_cron_30s( $schedules ) { 
	$schedules['30_seconds'] = array(
		'interval' => 30,
		'display'  => esc_html__( 'Every 30 seconds' ),
	);
	return $schedules;
}

Managing Cron Tasks in WordPress

To monitor, debug, and manage scheduled actions in WordPress, you can use the command line (WordPress CLI).

And for the vast majority of users and developers, a plugin will suffice WP Crontrol.

Despite the fact that the plugin allows you to create tasks through the interface, we use it only for monitoring and debugging.

Creating Cron tasks in WordPress (WP-Cron)

The most reliable and simple method:

// Describe the deactivation function.
function wpz_deactivate() {
	// Remove the 'wpz_cron' task
	wp_clear_scheduled_hook( 'wpz_cron' );
}

// Add the main event.
add_action( 'init', function() {

	// Create our scheduler event.
	add_action( 'wpz_cron', 'wpz_run_cron' );

	// Register an event in case of deactivation.
	register_deactivation_hook( __FILE__, 'wpz_deactivate' );

	// Add our event to WP-Cron.
	if ( ! wp_next_scheduled ( 'wpz_cron' ) ) {
		wp_schedule_event( time(), '30_seconds', 'wpz_cron' );
	}

});

// Describe the function for the scheduler.
function wpz_run_cron() {
	// The function will run once every thirty seconds (We added a new, thirty-second interval for WP-Cron above).
}

If the theme in which this code is inserted is activated and there is no scheduled action named wpz_cron, then add wpz_cron. The wpz_run_cron function will run every 30 seconds. If the user has changed the theme, then remove wpz_cron event from the scheduler.

If you think about it, it’s simple enough, and if you’ve installed the WP Control plugin, you’ll see:

WP Crontrol Events

One-Time Cron Tasks

In WordPress, you can also add an event that is triggered only once.

// Create the wpz_event event.
add_action( 'wpz_event', 'wpz_in_an_hour', 10, 3 );

// Describe the function for the wpz_event event.
function wpz_in_an_hour( $arg1, $arg2, $arg3 ) {
    // Function Description.
}
// Add the wpz_event event to the scheduler (it will run after 1800 seconds from the current time)
wp_schedule_single_event( time() + 1800, 'wpz_event', array( $arg1, $arg2, $arg3 ) );

One-time tasks themselves are rarely used, but it is worth specifying that they are very convenient to use in tasks that are started at a certain interval.

How useful is the publication?

Click on a star to rate it!

Average score 5 / 5. Number of grades: 1

No ratings yet. Rate it first.

Similar posts

WordPress Template Hierarchy full guide

A guide to the template hierarchy in WordPress All modern WordPress themes consist of templates, style sheets, javascript, and images. Together, these files determine how your site will look to users. Templates with specific names affect certain areas of your website. Generally, a WordPress theme should contain templates for displaying categories, dates, archives, individual posts,…
Read more

What are plugins in WordPress and how do they work?

If you’re new to WordPress, you’re probably asking yourself: “What are plugins in WordPress?” This is a fairly common question because, in addition to introducing one of many new terms into your vocabulary, WordPress plugins are also an important building block of every single WordPress site. This article will answer your question, and then we’ll…
Read more

How to install a plugin on WordPress – a step-by-step guide for beginners

Installing plugins on WordPress using the admin panel is so easy that you’ll probably never need the skills to manually install plugins via FTP/SFT or using WP-CLI. But the technical part can be useful if the WordPress plugin directory is overloaded or not available at all. Installing plugins on WordPress from a repository The easiest…
Read more