cron job in wordpress

Posted: July 30, 2015 in Uncategorized

Here is a example how to add cron functionality in wordpress.remember this cron execute when some one access website.this runs at every five minutes

add_filter(‘cron_schedules’, ‘add_scheduled_interval’);

// add once 5 minute interval to wp schedules
function add_scheduled_interval($schedules) {
$schedules[‘minutes_5’] = array(‘interval’=>360, ‘display’=>’Once 5 minutes’);
return $schedules;
}
if (!wp_next_scheduled(‘minutes_5_cron’)) {
wp_schedule_event(time(), ‘minutes_5’, ‘minutes_5_cron’);
}
add_action(‘minutes_5_cron’, ‘min_cron_executor’);

function min_cron_executor() {

$to =’email’;
$subject = ‘The subject’;
$body = ‘Cron is going on’;
$headers = array(‘Content-Type: text/html; charset=UTF-8’);

wp_mail($to,$subject,$body,$headers);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s