How to use WordPress cron job with a practical example?

Home / News
How to use WordPress cron job with a practical example?

What is Corn's job?

It is a time-based schedule task of cron job with a practical WordPress. WordPress has core features some time need to update. If likely post update, schedule post, and time base image changes. WordPress corn job name comes from the UNIX system. It is a system software mainly used for time-based scheduling systems for uses many purposes.It is mainly used for time-based working like page/post content update or page load and schedule tasks.

Why use WordPress corn job with practical?

 

  • Sometimes need to time-based scheduling for WordPress core features and some plugins to manage. But some hosting providers do not provide the corn jobs later we use WordPress corn jobs.
  • We every time find out an easy solution that’s why will not use WordPress API, We use just WordPress system scheduler.
  • We define and control WordPress corn job scheduling. We define a time-based schedule 100% when start and where to end.

Custom Intervals

add_filter('corn_schedules', 'adding_new_cron_schedule');
function adding_new_cron_schedule($schedules){
 $schedules['ten_seconds'] = array(
	'interval' => 10, 
        'display' => _e('Every Ten Second') 
 );
 return $schedules;
}

Adding new custom hook

You must create a new hook for this task. Which give hook name and function name. Next, you get the hook name and call it your custom function. You can give it any name. This calling function will be executing what you run here.

Corn job working process

Before saying that, it is a time-based scheduling system. example( If we set our time-based 5 min after every interval. If you measure time 2:05 then the next time will be 2:10 after 2:15 ). This time change repeatedly because we setup it 5 min after changing everything. This is the best WP cron job with a practical session.There are several default intervals of WordPress.

  • hourly
  • twice-daily
  • daily
  • weekly

add_action("my_corn_custom_data","my_custom_method_cron_job");
function my_custom_method_cron_job(
	$args= [
	'post_title'=>'Creat a new title for every 10 second',
        ];
	wp_insert_post($args);
)

Scheduling the Task:

WordPress corn is a simple time-based task scheduler. We already know. WP-corn schedule gives us a function. Which are three-parameter.

WP_schedule_event( time(), 'ten_seconds', 'my_corn_custom_data' );

1. $timestamp – It is a UNIX timestamp
2. $recurrence – Interval time which use for recur in second
3. $hook – It is a custom hook

Finally, we check the error, and the task is not already scheduled. If you need help please feel free to contact us.

if(!WP_schedule_event('my_corn_custom_data')){
WP_schedule_event( time(), 'ten_seconds', 'my_corn_custom_data' );
}
If you went to use the WP cron job-related plugin. You can try it WP cron job with a practical session.
Leave a Comment