If you are looking for a way to execute commands and tasks on your WordPress site several times a day, something like a specific day, week, month, etc., you’re struggling to accomplish this functionality, don’t worry, I will tell you how to overcome this with a WordPress cron jobs, a cron job is a task that is set to run at a specified interval.
WordPress cron is one of the most useful features. You can schedule events to run at specific intervals, but rather than act like a classic cron job, it is scheduled with events and sets and written to the database. The next time the user opens the site, the WordPress cron system checks if the event is scheduled, and if so, it raises the event.
The WordPress cron schedule events can be categorized into two flavors there are
1. Single events – It will be executed only once, and it will never fire until reschedule it
2. Recurring events -that occurs more than once using a time interval.
Here I am going to show you how to set cron job on WordPress with a simple examples, You can test yourself by using the following code in WordPress’s active theme functions.php file or by creating a custom plugin.
Note : To schedule periodic events, you need to create custom ‘actions’ that need also be registered in cron. When cron is executed, the function associated with the custom ‘action’ created previously is executed.
1.Let’s see the following scheduling single event WordPress Cron Job example where we are going to be send an email after 5 minutes post was published, here we used wp_schedule_single_event() WordPress Api to set schedule event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * schedule_post_publish_event runs when a Post is Published */ add_action( 'publish_post', 'schedule_post_publish_event' ); /** * when user posts this function will call */ function schedule_post_publish_event( $post_id ) { // Schedule the actual event wp_schedule_single_event( 5 * MINUTE_IN_SECONDS, 'post_publish_send_email'); } /** * this method will call when cron executes */ function post_publish_send_email() { //init time $time = date("Y-m-d h:i:sa", time()); //send email to admin wp_mail( 'admin@yoursite.com', 'New post has been published', 'New post has been published on' . $time ); } |
2. Let’s see the another recurring scheduling events WordPress Cron Job example where we are going to be send an email on a daily basis to users to visit our site, here we used wp_next_scheduled() and wp_schedule_event() WordPress Api to set schedule events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * add method to register event to WordPress init */ add_action( 'init', 'register_daily_notify_user_send_email_event'); /** * this method will register the cron event */ function register_daily_notify_user_send_email_event() { // make sure this event is not scheduled if( !wp_next_scheduled( 'notify_user_send_email' ) ) { // schedule an event wp_schedule_event( time(), 'daily', 'notify_user_send_email' ); } } /** * notify_user_send_email method will be call when the cron is executed */ add_action( 'notify_user_send_email', 'notify_all_user_send_email' ); /** * this method will call when cron executes */ function notify_all_user_send_email() { //here you can build logic and email to all users //send email to admin wp_mail( 'user@yoursite.com', 'wordpress tutorial', 'Visit my blog for wordpress tutorial ...!'); } |
3. Let’s see the another unschedule scheduling event WordPress Cron Job example with using above example, here we used wp_unschedule_event() and wp_clear_scheduled_hook() WordPress Api to unschedule events.
1 2 3 4 5 6 7 8 |
/** * Get the timestamp of the next scheduled cron run */ $time_stamp = wp_next_scheduled( 'post_publish_send_email' ); /** * Unschedule the next scheduled cron run */ wp_unschedule_event( $time_stamp, 'post_publish_send_email' ); |
1 2 3 4 |
/** * Clear the next scheduled cron run */ wp_clear_scheduled_hook( 'post_publish_send_email' ); |
By default, WordPress defines three intervals only “hourly“,”twicedaily“and “daily“. To add custom intervals with WordPress’s “cron_schedules” filter, we use some WordPress time constants. A place to ease our lives. For more information about these constants, see “Using Time Constants“. Here is a brief overview
1 2 3 4 5 6 |
MINUTE_IN_SECONDS = 60 (seconds) HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS MONTH_IN_SECONDS = 30 * DAY_IN_SECONDS YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS |
4. Let’s see the antoher customizing cron Intervals example, here we used “cron_schedules” filter to add our own intervals.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * Add our own custom cron interval callback */ function custom_cron_intervals_callback($schedules) { // add 'weekly' cron interval $schedules['weekly'] = array( 'interval' => WEEK_IN_SECONDS, 'display' => __('Once Weekly') ); // add 'monthly' cron interval $schedules['monthly'] = array( 'interval' => MONTH_IN_SECONDS, 'display' => __('Once a month') ); //return return $schedules; } /** * Add our own custom cron interval hook */ add_filter( 'cron_schedules', 'custom_cron_intervals_callback'); |
Hopefully, in this article, you have learned WordPress Cron Job is an excellent feature that can be used to schedule multiple events on WordPress sites. With some easy examples, we have seen how to use Cron WordPress for some functions to share. It can also be used for other situations. Please Subscribe to ScriptHere.Com by Email. You can also find us on Facebook and Twitter