Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

Add/Edit/Delete a Cron Job in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
November 29, 2021
5 minutes read
Add/Edit/Delete a Cron Job in WordPress

Cron is used when WordPress deals with scheduled activities.The word cron originate from time base work schedule in Unix .wordpress utilize cron for a number of operations.These planned jobs consist of checking for the latest version for WordPress, looking for the 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 the 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 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, twice daily) interval. The action will activate when somebody visits your WordPress website if the scheduled time has passed.

Function

wp_schedule_event($timestamp,$repetition,$hook,$args);

$timestamp: The first time that you want the event to occur.
$repetition: It Defines the time of next Schedule repetition it may be in hours,twice-daily, 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 helps you understand how it works.This plugin is used for sending automatically mail.

<?php
/*
Plugin Name:cron_job
Version: 1.0
Author: www.flippercode.com
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
function job_activate() 
{
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 () 
{
wp_mail
 (
'example@yoursite.com',
'Automatic mail',
'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.

cron_job_plugin

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 an 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

delete_action('hourly_event_hook', 'cron_job_delete');
// clean the scheduler
function cron_job_delete()
{
wp_clear_scheduled_hook('hourly_event_hook');
}

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.