Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_schedule_event hook scheduled but not working

I'm trying to trigger cron job from WordPress Plugin that I'm writing (It's gonna take all new Products and export them to CSV every day) so the problem is that when I'm put this code in functions.php all working great and the code is valid but from the plugin folder it's scheduled and I can see it (with Cron View Plug-in) but not executed.. I found another same questions but there was no answer.. It seems like it's not really been triggered 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
}
like image 571
Nick Avatar asked Jun 15 '15 08:06

Nick


1 Answers

Try to move add_action outside of a 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('[email protected]', 'Automatic email', 'Cron works!');
}
like image 186
Vladimir Atamanenko Avatar answered Oct 14 '22 01:10

Vladimir Atamanenko