How to create a cron job in wordpress

how to create a cron job in wordpress

Cron is a job scheduler which executes in specific time. It is a Linux OS software used to schedule time based jobs. Cron jobs are like commands which are execute in the background.

Below is an example of cron job in Linux, in which it tells the OS to run at 8:00 every day.

00 08 * * * /usr/etc/file/

We can set time to run a cron job in the background. In WordPress, WP-Cron is used to run cron jobs, which is used to simulate a system cron. Run wp-cron jobs using certain time values hourly, twicedaily and daily. In this blog I will use daily to run a cron, which means it will run in the background in every 24 hours, in which a mail is sent. hourly, twicedaily, daily and weekly are default intervals in wordpress. Let me explain all with the help of an example. Use below code in functions.php to add cron jobs in wordpress:

Hourly Cron Job: Hourly interval will execute in every one hour.

add_action('wp', 'schedule_cron_hourly_function');
function schedule_cron_hourly_function() {
  if ( !wp_next_scheduled( 'my_cron_hourly' ) )
  wp_schedule_event(time(), 'hourly', 'my_cron_hourly');

add_action('my_cron_hourly', 'my_hourly_cron_function');
function my_hourly_cron_function() {
  wp_mail('any email here','Cron Job', 'Cron Job works');

Twicedalily Cron JobTwicedaily interval will execute in every 12 hours, means two times in a day.

add_action('wp', 'schedule_cron_twicedaily_function');
function schedule_cron_twicedaily_function() {
  if ( !wp_next_scheduled( 'my_cron_twicedaily' ) )
  wp_schedule_event(time(), 'twicedaily', 'my_cron_twicedaily');

add_action('my_cron_twicedaily', 'my_cron_twicedaily_function');
function my_cron_twicedaily_function() {
  wp_mail('any email here','Cron Job', 'Cron Job works');

Daily Cron Job: Daily interval will execute in every 24 hours, means one time in a day.

add_action('wp', 'schedule_cron_daily_function');
function schedule_cron_daily_function() {
  if ( !wp_next_scheduled( 'my_cron_daily' ) )
  wp_schedule_event(time(), 'daily', 'my_cron_daily');

add_action('my_cron_daily', 'my_cron_daily_function');
function my_cron_daily_function() {
  //you can add here custom code which will execute daily
  wp_mail('any email here','Cron Job', 'Cron Job works');

Weekly Cron Job: Weekly interval will execute in every 7 days.

add_action('wp', 'schedule_cron_weekly_function');
function schedule_cron_weekly_function() {
  if ( !wp_next_scheduled( 'my_cron_weekly_name' ) )
  wp_schedule_event(time(), 'daily', 'my_cron_weekly_name');

add_action('my_cron_weekly_name', 'my_cron_weekly_function');
function my_cron_weekly_function() {
  //you can add here custom code which will execute daily
  wp_mail('any email here','Cron Job', 'Cron Job works');

We can also add customer interval in wordpress using creating a filter. Use below code to add a custom interval in your wordpress, which will execute in every 10 seconds.
Custom interval Cron Job

add_filter( 'cron_schedules', 'my_custom_sheduler_function' );
function my_custom_sheduler_function( $schedules ) { 
    $schedules['ten_seconds'] = array(
        'interval' => 10,
        'display'  => esc_html__( 'Every Ten Seconds' ), );
    return $schedules;
add_action('wp', 'schedule_cron_function');
function schedule_cron_function() {
  if ( !wp_next_scheduled( 'my_cron_name' ) )
// add our custom above function `cron_schedules`
  wp_schedule_event(time(), 'cron_schedules', 'my_cron_for_ten_minutes');

add_action('my_cron_for_ten_minutes', 'my_cron_function');
function my_cron_function() {
  //you can add here custom code which will execute every 10 minutes
  wp_mail('any email here','Cron Job', 'Cron Job works');

We can also stop inbuilt cron jobs of wordpress by define DISABLE_WP_CRON to true in wp-config.php file which is located in root directory of wordpress. Add below code to stop inbuilt cron jobs in wordpress:

define('DISABLE_WP_CRON', true);

Some hosting providers have only shared hosting plans, so they do not provide access of system scheduler the we use wp-cron in wordpress in core and many plugins which needs scheduling system to perform time-based task. You can also use some wordpress plugins to run cron jobs in your website or webapp. The most common plugins are Advanced Cron Manager – debug & control, Cronjob Scheduler and WP Crontrol, you can use any one them to run wp-cron.

Features of WP-Cron

  • We can create custom time intervals.
  • If any task not run on time then it will add it in a queue.
  • Pre-defined time intervals in wordpress which saves our time. Pre-defined time intervals are hourly, twicedaily, daily and weekly.
  • Run in the background, did not take any effect on website speed. 

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Shiva Rai
Shiva Rai
20 Dec 2020

ThankYou for explaining wp_cron job using examples. WordPress cron jobs are easy as compare of implements cron jobs in any another technology

Add your comment