I am having difficulty doing this and have checked over previous questions however they do not seem to be working.
So far i have disabled the default wordpress cron by adding below to my wp-config.php:
define('DISABLE_WP_CRON', true);
Then i have attempted to schedule my task to run every 5 mins from within my plugins main php file:
function my_cron_schedules($schedules){
if(!isset($schedules["5min"])){
$schedules["5min"] = array(
'interval' => 5*60,
'display' => __('Once every 5 minutes'));
}
if(!isset($schedules["30min"])){
$schedules["30min"] = array(
'interval' => 30*60,
'display' => __('Once every 30 minutes'));
}
return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
function schedule_my_cron(){
wp_schedule_event(time(), '5min', 'fivemin_schedule_hook');
}
if(!wp_get_schedule('fivemin_schedule_hook')){
add_action('init', 'schedule_my_cron',10);
}
function fivemin_schedule_hook() {
get_feed();
}
So the above appears to be scheduling my event within database however have 100's of entries when checking cron schedule with:
<?php print_r(get_option('cron')); ?>
I have also made sure to update my crontab with below:
* * * * * wget -q -O - http://wordpress.com/wp-cron.php?doing_wp_cron
However my task does not appear to be running and am concerned about the amount of entries within database for this 5min job.
Each entry looks like below:
Array ( [1524308364] => Array ( [fivemin_schedule_hook] => Array ( [40cd750bba9870f18aada2478b24840a] => Array ( [schedule] => 5min [args] => Array ( ) [interval] => 300 ) ) )
I have tried debugging wp-cron.php by echoing out the $hook when it tries to fire and my hook is shown when i visit wp-cron.php directly. The actual function however just does not seem to fire.
Try this:
Replace this:
function schedule_my_cron(){
wp_schedule_event(time(), '5min', 'fivemin_schedule_hook');
}
if(!wp_get_schedule('fivemin_schedule_hook')){
add_action('init', 'schedule_my_cron',10);
}
..with this:
function schedule_my_cron(){
// Schedules the event if it's NOT already scheduled.
if ( ! wp_next_scheduled ( 'my_5min_event' ) ) {
wp_schedule_event( time(), '5min', 'my_5min_event' );
}
}
// Registers and schedules the my_5min_event cron event.
add_action( 'init', 'schedule_my_cron' );
// Runs fivemin_schedule_hook() function every 5 minutes.
add_action( 'my_5min_event', 'fivemin_schedule_hook' );
//add_action( 'my_5min_event', 'another_function_to_call' );
//add_action( 'my_5min_event', 'another_function_to_call2' );
But a more appropriate/preferred way is to add this in the activation function for your plugin:
wp_schedule_event( time(), '5min', 'my_5min_event' );
Example:
register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
if ( ! wp_next_scheduled ( 'my_5min_event' ) ) {
wp_schedule_event( time(), '5min', 'my_5min_event' );
}
}
..which would be used in place of the following:
function schedule_my_cron(){
// Schedules the event if it's NOT already scheduled.
if ( ! wp_next_scheduled ( 'my_5min_event' ) ) {
wp_schedule_event( time(), '5min', 'my_5min_event' );
}
}
// Registers and schedules the my_5min_event cron event.
add_action( 'init', 'schedule_my_cron' );
And add this somewhere in the deactivation function for your plugin:
wp_clear_scheduled_hook( 'my_5min_event' );
Example:
register_deactivation_hook( __FILE__, 'my_plugin_deactivation' );
function my_plugin_deactivation() {
wp_clear_scheduled_hook( 'my_5min_event' );
}
See https://codex.wordpress.org/Function_Reference/wp_schedule_event#Examples for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With