Skip to content

Instantly share code, notes, and snippets.

@esgy
Last active December 11, 2015 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esgy/4578637 to your computer and use it in GitHub Desktop.
Save esgy/4578637 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: p47 Cron Job
* Plugin URI: http://point47.com
* Description: demo for a running cron job
* Version: 1.0
* Author: Sorin Gitlan
* Author URI: http://point47.com
*/
add_action( 'init', function(){
// first remove any scheduled event
$time = wp_next_scheduled( 'p47_cron_hook' );
wp_unschedule_event( $time, 'p47_cron_hook' );
// create the cron job event
if( !wp_next_scheduled( 'p47_cron_hook' )){
wp_schedule_event( time(), 'two-minutes', 'p47_cron_hook' );
// Creates a single cron event that will execute one hour from now.
//wp_schedule_single_event( time() + 3600, 'p47_cron_hook');
}
});
// Create a Options menu entry in the Admin menu
add_action( 'admin_menu', function(){
add_options_page( 'Cron settings', 'Cron Settings', 'manage_options', 'p47-cron', function(){
$cron = _get_cron_array();
$schedules = wp_get_schedules();
?>
<div class="wrap">
<h2>Cron Event Scheduled</h2>
<?php
foreach ($schedules as $name) {
echo '<h3>'. $name['display'] .' : ' . $name['interval'] . '</h3>';
}
?>
</div>
<?php
} );
} );
add_action('p47_cron_hook', function(){
$str = time();
wp_mail( 'forapathy@gmail.com', 'Scheduled WP cron', 'This email was sent at: '.$str );
});
// add new interval values that can be used in wp_schedule_event()
add_filter('cron_schedules', function($schedules){
$schedules['two-minutes'] = array(
'interval' => 120,
'display' => 'Every two minutes'
);
$schedules['ten-minutes'] = array(
'interval' => 600,
'display' => 'Every ten minutes'
);
return $schedules;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment