Last active
December 11, 2015 09:18
-
-
Save esgy/4578637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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( '[email protected]', '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