Creating cron jobs in WordPress is actually really simple but yet, so many WordPress developers don’t understand WordPress cron jobs or they understand the concept of WordPress cron jobs but don’t know how to set them up appropriately.

This article is targeted to slightly more advanced WordPress developers while aiming to explain what WordPress cron jobs are, how they work, and how to use them appropriately.

Most of the content in this article was taken from an answer I originally posted on the WordPress Stack Exchange to the question “WP Cron desn’t execute when time elapses“.

What Are Cron Jobs?

I would like to clear the air a bit about WordPress cron jobs before moving forward.

Cron jobs are tasks that executed until completed at specified time intervals of time, which usually reoccur numerous times. Cron jobs are especially useful for automatically executing tasks that might have originally needed to be manually executed.

Web Server Cron Jobs vs. WordPress Cron Jobs

There is a difference between a cron job which is setup via your web hosting platform or web server and the cron jobs setup on WordPress. Cron jobs setup directly on the server are guaranteed to execute dead on the time intervals specified.

A WordPress cron job is exactly the same as a cron job setup via your web hosting platform or web server. They both execute tasks automatically for you at specified time intervals.

Executing Automated Tasks and Scheduling Events in WordPress with WordPress Cron Jobs

There is one draw back to using the WordPress cron jobs. WordPress cron jobs require a page visit to anywhere on your WordPress website in order for your tasks to be executed. That is because WordPress is a “pseudo-cron system”.

The positive to using WordPress cron jobs is that it’s incredibly simple to execute functionality specific to your WordPress website.

WordPress Cron Schedules

Firstly, define your custom WordPress cron job schedules using the WordPress filter hook cron_schedules.

Example: Defining a WordPress cron schedule every: 30 minutes, 1 hour, 2 hours.

if ( ! function_exists( 'tbd_15_cron_schedules' ) ) {
    function tbd_15_cron_schedules( Array $schedules = array() ) {

        # Avoid conflict with other cron jobs.
        $prefix                      = 'cron_';// Example Reference: cron_30_mins
        $schedule_options            = array();
        $schedule_options['30_mins'] = array(
            'display'  => '30 Minutes',
            'interval' => '1800'
        $schedule_options['1_hours'] = array(
            'display'  => 'Hour',
            'interval' => '3600'
        $schedule_options['2_hours'] = array(
            'display'  => '2 Hours',
            'interval' => '7200'
        # Add each custom schedule into the cron job system.
        foreach ( $schedule_options as $schedule_key => $schedule ) {
            $schedules[ $prefix . $schedule_key ] = array(
                'interval' => $schedule['interval'],
                'display'  => __( 'Every ' . $schedule['display'] )

        return $schedules;
    add_filter( 'cron_schedules', 'tbd_15_cron_schedules' );

Schedule WordPress Task at Specific Time Interval (WordPress Cron Schedule)

You need to decide where and when to actually schedule the event. Here is just an example snippet of code, which makes a call to a custom class method (This can be done with functions as well):

Here is the code which actually schedules the event:

if ( ! function_exists( 'tbd_15_schedule_task' ) ) {

    function tbd_15_schedule_task( Array $task = array() ) {

        # Must have task information.
        if ( ! $task ) {
            return false;

        # Set list of required task keys.
        $required_keys = array(

        # Verify the necessary task information exists.
        $missing_keys = array();

        foreach ( $required_keys as $key ) {

            if ( ! array_key_exists( $key, $task ) ) {
                $missing_keys[] = $key;


        # Check for missing keys.
        if ( ! empty( $missing_keys ) ) {
            return false;

        # Task must not already be scheduled.
        if ( wp_next_scheduled( $task['hook'] ) ) {
            wp_clear_scheduled_hook( $task['hook'] );

        # Schedule the task to run.
        wp_schedule_event( $task['timestamp'], $task['recurrence'], $task['hook'] );

        return true;


Task Executed Automatically by WordPress at Specified Time Interval (WordPress Cron Schedule)

Now, all you need to do is make a call to the name of your custom cron task. In this example the cron task name is custom_imap_import.

if ( ! function_exists( 'tbd_15_do_imap_import' ) ) {

    function tbd_15_do_imap_import() {
        // .... Do stuff when cron is fired ....

    add_action( 'custom_imap_import', 'tbd_15_do_imap_import' );


So in this example, tbd_15_do_imap_import(); is called every 30 minutes (assuming you have enough traffic to your website).

Extra Information About Scheduling Events in WordPress using WordPress Cron Jobs

  • Requires a page visit in order for your cron job to fire at correct times.

Example: If you scheduled a task at 30 minute intervals, but no one visits your site for 4 hours, your cron job won’t be fired until that visitor comes to your site 4 hours later. If you really truly need your task fired every 30 minutes, then it is advised to setup a legitimate cron job via your web hosting provider to visit your website at the desired intervals.

WordPress cron jobs don’t make your website slow!

Maybe you are thinking what if the cron-script takes a long time to be executed, will the visitors have to wait until the script is executed? Nope! How can that be possible? If you look at the wp-cron.php file you will find a line


It’s a php.ini configuration that sets that if you stop loading the site/script the script won’t stop executing.

If you look at the wp-includes/cron.php file you’ll find a line similar to this:

wp_remote_post( $cron_url, 
array('timeout' => 0.01,
 'blocking' => false, 
 'sslverify' => apply_filters('https_local_ssl_verify', true)) );

That means WordPress will wait only 0.01 second for triggering the execution then it will abort but as you have set ignore_user_abortto true the script will be executing. This functionality is a huge advantage to execute large scripts in WordPress cron jobs.

WordPress functions related to WordPress Cron Jobs

  1. wp_schedule_event
  2. wp_schedule_single_event
  3. wp_clear_scheduled_hook
  4. wp_next_scheduled
  5. wp_unschedule_event
  6. wp_get_schedule
  7. wp_get_schedules

WordPress Stack Exchange

Remember, this article is just a summary of an answer to a question (WP Cron doesn’t execute when time elapses) I wrote on WordPress Stack Exchange.

If you have a question related to WordPress, you can asked it on WordPress Stack Exchange and someone will very likely provide an answer or solution to your question or issue.

If you don’t get an answer in somewhat of a timely manner, feel free to contact me with a link to your question on WordPress Stack Exchange and I will try to find time to answer it for you.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] WP Cron doesn’t execute when time elapses asked by: @Mike Madern
  2. [WordPress Development] Cron jobs repeating themselves asked by: @urok93
  3. [WordPress Development] wp_schedule_event run in background or not? asked by: @Damithatt
  4. [WordPress Development] How do i schedule cron in wordpress for each second? asked by: @Frank
  5. [WordPress Development] Need to execute a cron job asked by: @Jose David Garcia Llanos
  6. [WordPress Development] Do WordPress cron jobs slow down page loading? asked by: @urok93
  7. [WordPress Development] Run function with linux cron job in WordPress asked by: @chap
  8. [WordPress Development] Multiple wp_schedule_event cron jobs in plugin cause multi-execution asked by: @Entrit
  9. [WordPress Development] How to have a cron execute at a certain time on a certain day of the week asked by: @Pete

Leave a Reply