Wp_schedule_event is scheduled to run but not working
I'm trying to get a cron job running from a WordPress plugin I'm writing (it collects all new products and exports them to CSV every day), so the problem is when I put this code in functions.php everything works fine and the code is valid. but from the plugin folder it planned and I can see it (with Cron View Plug-in) but not executed. I found some more questions, but there was no answer. It doesn't seem like it really worked, or something is blocking it. take a look at my code.
function csv_init(){
add_action('my_hourly_event', 'Download_CSV_with_args');
}
function starthere(){
// some code here
$file = $_SERVER['DOCUMENT_ROOT'].'/wp-content/csv_settings.php';
$content = serialize($args);
file_put_contents($file, $content);
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event');
$schedule = wp_get_schedule( 'my_hourly_event' );
echo wp_next_scheduled( 'my_hourly_event' ).'<br>';
if ($schedule){
echo '<h3>The "'.$schedule.'" Cron Job is running..</h3>';
}else {
echo '<h3>There are no Cron Jobs that running..</h3>';
}
}
function Download_CSV_with_args() {
//execution of my code
}
+3
source to share
1 answer
Try moving add_action
outside the function:
function starthere(){
if (!wp_next_scheduled('my_hourly_event')) {
wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
}
}
add_action( 'my_hourly_event', 'Download_CSV_with_args' );
function Download_CSV_with_args() {
wp_mail('you@yoursite.com', 'Automatic email', 'Cron works!');
}
+2
source to share