Cron is used when wordpress deals with scheduled activities .The word cron originate from time base work scheduler in Unix .wordpress utilize cron for number of operations.These planned job consist of checking for latest version for wordpress,looking for theme as well as plugins updates and also publishing scheduled article.
One of many typical myths regarding cron in wordpress is usually that cron is consistently functioning searching to get jobs to perform ,this really isn’t the case. Cron is function whenever a front end or admin page is loaded on the website .Each time a page is required, wordpress tests if there are any kind of cron jobs to perform.
Any visit to your website can induce cron, regardless of whether from visitor or even a search engine bot.
For example, you could set a cron job to delete temporary files every week to free up disk space.
In this blog we will discuss about how to add a new cron job by using wp_shedule_event and also to edit and delete this job.
Add a New Cron Job
Schedules a hook which will be executed by the wordpress actions on a specific (Daily,Hourly,twicedaily) interval. The action will activate when somebody visits your wordpress website, if the scheduled time has passed.
$timestamp: The first time that you want the event to occur.
$repetition: It Define the time of next Schedule repetition it may be in hours,twicedaily,daily.
$hook: The name of a hook that we execute.
$args: (optional) Arguments to pass to the hook function(s).
Here we will make a new plugin to create a cron job that help you understand how it works.This plugin is used for sending automatically mail.
Description: How to create a simple cron job
register_activation_hook ( __FILE__, 'job_activate' );
//set all details of a time, frequency and name of an action hook
//Set Function Name
wp_schedule_event( time(), 'hourly', 'hourly_event_hook' );
add_action( 'hourly_event_hook', 'cron_job_example' );
//DEFINE work that you want to do hourly
function 'cron_job_example ()
'Hello, this is an automatically scheduled email from WordPress.'
Add this code to your plugin folder by making a new corn_job.php file and then activate your corn_job plugin.After activation this plugins look like given below image.
Edit a Cron Job
You can edit the cron job just by changing the time() of event This must be in a UNIX timestamp format. WP cron uses UTC/GMT time, not local time.
You can change the re-occurrence schedule. Here we are using hourly but you can change it for hourly to daily or twice in a day.
You can also edit function work .Here we are sending automatic email. But you can also assign any other work of our interest.
Delete a cron job
If you using cron jobs in a Plugin, then don’t forget to deactivate or delete the cron if you don’t require it anymore by using delete_aciton
// clean the scheduler