I try to set up a cronjob. It seems like it run the le_schedule
function but not the le_do_this
function. I also tried some cronjob plugins, some says it runs and some says it fails.
In this case it will try to get a file and save it but it doesn't. Why doesn't this work?
add_action( 'wp', 'le_schedule' );
add_action( 'le_event', 'le_do_this' );
function le_schedule() {
if ( ! wp_next_scheduled( 'le_event' ) ) {
wp_schedule_event( time(), 'daily', 'le_event');
}
}
function le_do_this() {
$response = wp_remote_get('http://www.example.com/file.txt');
if( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body($response);
if( ! empty( $body ) ) {
$put = get_template_directory() . '/cache/cache.txt';
file_put_contents( $put, $body );
}
}
}
wp_clear_scheduled_hook( 'le_event' );
wp_clear_scheduled_hook( 'le_do_this' );
I clear the schedule to be able to run fresh on every pageload.