Wp Cron



Wp cron schedule

In this article we are going to see how to use WordPress Cron or WP Cron to register custom cron schedules and cron events.

You are going to learn:

By default the WordPress tasks that have been set up to run with wp-cron.php only run when a WordPress page is visited by a user, if the site has low traffic then tasks make take a while to action, if the site is high traffic and has caching then this can also cause reliability issues, instead you can manually set up the cron tasks to run at a. When a WP-Cron hook fails you’ll get notified soon after. A hook “fails” when it takes longer than 5 minutes to complete. You’ll get an email within 24 hours (or based on the plugin’s settings) to notify you of any failures. Sometimes a plugin runs some code that abruptly exits the process and there is no way of knowing when it stopped.

  • Tests the WP Cron spawning system and reports back its status. Command documentation is regenerated at every release. To add or update an example, please submit a pull request against the corresponding part of the codebase.
  • WP-Cron is important. So make sure it keeps working. Think of this plugin as insurance, but free. WordPress, plugins, themes, and servers are all moving parts that can be updated at anytime.
  • Test WordPress Cron Events?

See all WordPress Cron schedules? See all WordPress Cron schedules?

We can use the function wp_get_schedules() which show all the exiting schedules something like below:

View this gist on GitHub

Add WordPress Cron Schedules Add WordPress Cron Schedules

In WordPress we have a filer cron_schedules which allow us to add custom schedule.

We have see we can use the function wp_get_schedules() to see all the existing schedules.

In above example we have below schedules:

  • hourly
  • twicedaily
  • daily
  • weekly

But, We can use the cron_schedules to add our own custom schedules. Let’s add a every_ten_minutes schedule to perform some action.

View this gist on GitHub
Wp CronCron

Here, We have added a new schedule with 600 seconds interval means for every 10 minutes.

Add WordPress Cron Event Add WordPress Cron Event

We have register the custom schedule every_ten_minutes for every 10 minutes.

Now, Lets add the a custom schedule event or cron event.

We can register the schedule event with function wp_schedule_event().

See below code snippet:

View this gist on GitHub
Cron

Here, We have register the prefix_cron_hook schedule event. This scheduled event trigger for every 10 minutes.

But, We have to add a action which is same as our scheduled event.

We can use the same action prefix_cron_hook to perform some task. So, Lets add a sample task.

Add WordPress Cron Task Add WordPress Cron Task

We can use the function add_action() to perform our scheduled event with hook prefix_cron_hook.

See below code for reference:

View this gist on GitHub

Here, We have used function error_log() into our scheduled event task. So, Wherever our event prefix_cron_hook event trigger (every 10 minites) then our hook prefix_cron_hook called.

And our function prefix_cron_task() called which is hooked in action prefix_cron_hook.

In short; the string Called is added into the debug.log file (if WP_DEBUG & WP_DEBUG_LOG is true from wp-config.php file)

Test WordPress Cron Events? Test WordPress Cron Events?

We have an amazing WordPress plugin called WP Crontrol. The plugin WP Crontrol provide a user interface in which we can see the all registered scheduled and events.

Install and Activate the WP Crontrol plugin from WordPress back-end (wp-admin) Install and Activate the WP Crontrol plugin from WordPress back-end (wp-admin)

Follow below steps to install the WP Crontrol plugin.

  • Go to Plugins > Add new
  • Search for WP Crontrol
  • Click on the Install now and then Activate button
Cron

See below screenshot for reference:

Install and Activate WP Crontrol with WP CLI Install and Activate WP Crontrol with WP CLI

Alternatively, If you have a WP CLI setup then you can install the WP Crontrol plugin with WP CLI command.

Use below command which install and activate the WP Crontrol plugin.

If installed but not activate then use blow command to activate the WP Crontrol with command line

See all WordPress Cron Schedules? See all WordPress Cron Schedules?

To see all the existing Cron schedules navigate to to Settings > Cron Schedules.

See below screenshot for reference:

Wp-cron Scheduled Task

Here, You can see our registered schedule every_ten_minutes which indicate the interval 600 seconds (Every 10 minutes).

See all WordPress Cron Events See all WordPress Cron Events

Wp Cron Cli

To see all cron event navigate to Tools > Cron Events.

See below screenshot for reference:

Here, You can see our scheduled event prefix_cron_hook which is recurrence (trigger for every) 10 minutes.

Wp-cron Wordpress

Manually Run the WordPress Cron Event Manually Run the WordPress Cron Event

Yes, The plugin WP Crontrol allow us to execute the Cron Event manually.

Just click on Run Now action link which appear blow the scheduled event on hover.

Wp Cron Job

See below screenshot for reference:

Now, On click on Run now to and see the debug.log file which show you the string Called. You can see something below string:

See below screenshot for reference:

Wordpress Cron Jobs

Here, For testing I have just used code error_log( ‘Called’ );. You can use your own task.

Wordpress Cron Init

NOTE: In the debug.log file we cant see the logging data until we enable the debug log. We can use set constant DEBUG_LOG to true to enable the debug log.