【问题标题】:wp cron task update results every 5 minwp cron 任务每 5 分钟更新一次结果
【发布时间】:2015-04-30 13:28:26
【问题描述】:

我有一个从 json 脚本输出数据的结果函数/简码。这个 json 脚本每 5 分钟更新一次,所以我想为这个函数创建一个 cron 任务,每 5 分钟更新一次,而不复制页面上的内容。

如何在 wordpress 页面上做到这一点?

function resultpage() {
    $output = '';

    $output .= '<div id="content">';
    $output .= '<div id="part-1">';
    $output .= getgames("lol");
    $output .= '</div>';
    $output .= ' <div id="part-2">';
    $output .= getgames("counterstrike");
    $output .= ' </div>';
    $output .= ' <div id="part-3">';
    $output .= getgames("dota2");
    $output .= ' </div>';
    $output .= ' <div id="part-4">';
    $output .= getgames("hearthstone");
    $output .= ' </div>';
    $output .= '</div>';

    return $output;
}
add_shortcode('resultpage', 'resultpage');

 add_filter( 'cron_schedules', 'cron_add_5min' );

 function cron_add_5min( $schedules ) {
    $schedules['5min'] = array(
        'interval' => 5*60,
        'display' => __( 'Once every five minutes' )
    );
    return $schedules;
 }

add_action( 'init', 'register_result');

function register_result() {
    // Make sure this event hasn't been scheduled
    if( !wp_next_scheduled( 'resultpage' ) ) {
        // Schedule the event
        wp_schedule_event( time(), '5min', 'resultpage' );
    }
}

【问题讨论】:

    标签: php json wordpress cron


    【解决方案1】:

    您可以按照此说明进行操作Customizing the WP Schedule event。但是您可以手动添加一个 cron 选项卡。

    1. 创建一个用于编写函数的 URL。
    2. 运行命令crontab -e
    3. 将作业添加为示例:

    */5 * * * * curl http://your_domain.com/file_where_function_is_written.php

    【讨论】:

    • 它是写在一个主题function.php中的吗?我不确定我可以通过 url 访问它吗?
    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2010-10-19
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2022-12-18
    • 2013-11-17
    相关资源
    最近更新 更多