cronjob每3分钟一次

cronjob每3分钟一次

关于这个话题有很多关于stackoverflow的帖子,但是(我不知道为什么)对我来说什么都行不通。

我有的

function isa_add_every_three_minutes( $schedules ) {

    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );

    return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


function every_three_minutes_event_func()
{
    // do something
}

wp_schedule_event( time(), 'every_three_minutes', 'every_three_minutes_event_func' );

任何想法我在做什么错?

最佳答案

您需要使用add_action()将函数 Hook 到预定事件。

add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );

这是完整的代码。

// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    // do something
}
?>

关于php - Wordpress cronjob每3分钟一次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39442982/

10-09 06:47