WordPress 调度未触发

WordPress Scheduling Not Firing

我想使用 WordPress 调度在一段时间后回显一个字符串。下面是我在 functions.php 中的代码。通过 echo 语句不会打印任何内容,也没有错误。请指导我做错了什么。

function add_new_intervals($schedules) {
    // add weekly and monthly intervals
    $schedules['now'] = array(
        'interval' => 1,
        'display' => __('Once Weekly')
    );

    return $schedules;
}

add_filter( 'cron_schedules', 'add_new_intervals');

function my_activation() {
    if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
        wp_schedule_event( current_time( 'timestamp' ),'now', 'my_hourly_event');
    }
}

add_action('wp', 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');

function do_this_hourly() {
    echo "This is the Text";
}

您做错的一件事是期望在 cron 作业 运行 时能够看到回显。更好的测试是让它在数据库中进行更改,例如:

`update_option('my_cron_test', current_time())`;

WP Cron 作业(像大多数 cron 作业一样)旨在 运行 中需要按计划发生的功能。它们无意创建 "user output".

WP Cron 作业的一些技巧:
您的代码将尝试 运行 该功能每小时运行一次,但 WP 的设置方式可能会(也可能不会) 运行,具体取决于网站是否被访问。

这个弱点有很多解决方案,最简单/最有效的是在您的服务器上设置一个触发 WP cron 的 crontab 作业。像这样:

/15 * * * wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron

有关这方面的更多帮助(这可能很棘手),请查看这些文章:
Properly Setting Up WP Cron Jobs
Mastering WP Cron