How to use cron job in WordPress with programming

Cron is a time-based job scheduler in Unix-like operating systems. It allows you to automate tasks that need to be run periodically, such as backing up your database or publishing a post on your WordPress site at a specific time. In this blog, we will explore how to use cron in WordPress with programming.

First, let’s start by understanding how cron works in WordPress. WordPress uses the wp-cron.php file to run scheduled tasks. When a scheduled task is due, WordPress checks if wp-cron.php needs to be executed. If it does, WordPress executes wp-cron.php and runs the scheduled tasks.

To schedule a task in WordPress, you can use the wp_schedule_event() function. This function takes three arguments:

  1. The timestamp of the first instance of the scheduled event.
  2. The interval at which the event should repeat.
  3. The name of the function to be executed when the event occurs.

Here’s an example of how to use wp_schedule_event() to schedule a task that runs every hour:

// Schedule the event to run every hour
wp_schedule_event( time(), 'hourly', 'webkul_hourly_event' );

// Hook into the webkul_hourly_event
add_action( 'webkul_hourly_event', 'webkul_hourly_function' );

function webkul_hourly_function() {
    // Do something here
}

In the example above, we scheduled an event to run every hour using the ‘hourly’ interval. We named the event ‘webkul_hourly_event’ and hooked it into the ‘webkul_hourly_function’ function.

Full source code

/**
 * Plugin Name: Webkul cron job blog
 * Plugin URI: https://webkul.com/
 * Description: cron job
 * Author: webkul
 * Version: 1.0.0
 * Author URI: https://webkul.com/
 * Text Domain: webkul
 */


add_filter( 'cron_schedules', 'webkul_custom_schedule', 10, 1 );
add_action( 'wp', 'webkul_scheduled_event');
add_action( 'webkul_check_next_scheduled', 'webkul_update_price_according_to_crone' );

/**
 * Set event time.
 *
 * @param array $schedules all schedules list.
 * @return array $schedules return event schedules.
 */
function webkul_custom_schedule( $schedules ) {
	$time                                 = 60; // minutes
	$schedules['webkul_set_crone_time'] = array(
		'interval' => $time,
		'display'  => __( 'Webkul Every x Minutes', 'webkul' ),
	);
	return $schedules;
}


/**
 * Schedule event.
 *
 * @return void
 */
function webkul_scheduled_event() {
	// Schedule an action if it's not already scheduled.
	if ( ! wp_next_scheduled( 'webkul_check_next_scheduled' ) ) {
		wp_schedule_event( time(), 'webkul_set_crone_time', 'webkul_check_next_scheduled' );
	}
}

/**
 * Update price according to crone.
 *
 * @return void
 */
function webkul_update_price_according_to_crone() {
	// Write a code your want to run.
}

Now, let’s take a look at how to use cron with programming in WordPress. There are two ways to run cron jobs in WordPress: using the built-in WP-Cron system or using a server-side cron job.

Using the built-in WP-Cron system is the easiest way to schedule tasks in WordPress. However, it has some limitations, such as not being able to run tasks when there is no traffic on your site. To overcome this limitation, you can use a server-side cron job.

To use a server-side cron job, you will need to create a PHP file that contains the code to run your scheduled tasks. Then, you will need to set up a cron job on your server to execute the PHP file at the desired interval.

Here’s an example of how to create a PHP file that runs a scheduled task:

<?php
// Load WordPress
require_once( 'wp-load.php' );

// Schedule the event to run every hour
wp_schedule_event( time(), 'hourly', 'webkul_hourly_event' );

// Hook into the webkul_hourly_event
add_action( 'webkul_hourly_event', 'webkul_hourly_function' );

function webkul_hourly_function() {
    // Do something here
}

In the example above, we created a PHP file that loads WordPress, schedules an event to run every hour using wp_schedule_event(), and hooks into the event using add_action(). We named the event ‘webkul_hourly_event’ and hooked it into the ‘webkul_hourly_function’ function.

Now, let’s take a look at how to set up a server-side cron job to execute the PHP file. To set up a server-side cron job, you will need to access your server’s control panel and navigate to the cron jobs section. Then, you can add a new cron job with the following command:

php /path/to/php/file.php

Replace ‘/path/to/php/file.php’ with the actual path to your PHP file.

In conclusion, using cron in WordPress can help you automate tasks and save time. By using the wp_schedule_event() function, you can easily schedule tasks in WordPress. If you need to run tasks when there is no traffic on

For more details, you can follow the document. See the blog click here


Source link