$cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
使用下面的示例插件,我确认上面的代码(我也在问题中引用)是实际调用计划任务的代码。它设置 0.0.1 超时并访问 wp-cron.php。这意味着如果有 100 个任务,加载所有任务需要 1 秒。所以它对页面加载速度有轻微的影响。但这似乎是不用太担心的事情。
<?php
/* Plugin Name: Sample Cron Task */
// I used `heavy` because this code was initially written to test how it affects the server response if a heavy task is registered as a cron job. So forget about the naming.
add_action('admin_menu', 'sample_cron_heavy_task');
function sample_cron_heavy_task() {
add_options_page(
'Sample Cron Heavy Task',
'Sample Cron Heavy Task',
'manage_options',
'sample_cron_heavy_task',
'sample_cron_heavy_task_admin');
}
function sample_cron_heavy_task_admin() {
?>
<div class="wrap">
<?php
wp_schedule_single_event(time(), 'my_action_name');
$cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
// executes the registered task immediately
wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
echo get_option('sample_cron_heavy_task');
?>
</div>
<?php
}
add_action('my_action_name', 'myevent');
function myevent() {
$msg = date('Y m d h:i:s A') . ': the cron task is called.<br />';
update_option('sample_cron_heavy_task', $msg);
}