我试着设置一个cronjob。它似乎运行le_schedule
功能,但不是le_do_this
作用我还尝试了一些cronjob插件,有些说它可以运行,有些说它失败了。
在这种情况下,它将尝试获取一个文件并保存它,但它没有。为什么这不起作用?
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\' );
我清除了时间表,以便能够在每次页面加载时重新运行。