wp_schedule_event每天在特定时间wordpress

时间:2015-02-27 21:43:50

标签: php wordpress cron

我写了以下代码,每天16:20运行我的函数。

但我猜是代码中的问题..

不工作

大纪元时间戳:1427488800

3/28/2015 1:10:00 AM

if( !wp_next_scheduled( 'import_into_db' ) ) {
wp_schedule_event( time('1427488800'), 'daily', 'import_into_db' );

function import_into_db(){

////My code

}
add_action('wp', 'import_into_db');
}

3 个答案:

答案 0 :(得分:2)

老问题,但如果有人寻找答案,那么它就是:

使用strtotime()并传递时间。

在他的案例中:

wp_schedule_event( strtotime('16:20:00'), 'daily', 'import_into_db' );

答案 1 :(得分:0)

wp_schedule_event第一个参数是您希望第一次执行计划的时期,它还不是2月28日。

检查您是否正确使用了该功能:http://codex.wordpress.org/Function_Reference/wp_schedule_event

答案 2 :(得分:0)

WP Cron运行,当有人访问您的网站时。 因此,如果没有人访问,则cron永远不会运行。

现在有2种解决方案:

  1. 禁用WP Cron,使用真实的cron作业并对其进行自定义。

https://support.hostgator.com/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job

  1. wp_schedule_event()中使用自定义间隔:

    function myprefix_custom_cron_schedule( $schedules ) {
        $schedules['every_six_hours'] = array(
            'interval' => 21600, // Every 6 hours
            'display'  => __( 'Every 6 hours' ),
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
    
    //Schedule an action if it's not already scheduled
    if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
        wp_schedule_event( time(), 'every_six_hours', 'myprefix_cron_hook' );
    }
    
    ///Hook into that action that'll fire every six hours
     add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
    
    //create your function, that runs on cron
    function myprefix_cron_function() {
        //your function...
    }
    

您可以看到这些tut

http://www.nextscripts.com/tutorials/wp-cron-scheduling-tasks-in-wordpress/

http://www.iceablethemes.com/optimize-wordpress-replace-wp_cron-real-cron-job/

http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

自定义Wp cron

http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules

http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

http://www.viper007bond.com/2011/12/14/how-to-create-custom-wordpress-cron-intervals/

http://www.sitepoint.com/mastering-wordpress-cron/

https://tommcfarlin.com/wordpress-cron-jobs/

http://www.paulund.co.uk/create-cron-jobs-in-wordpress

cron linux

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

http://www.thesitewizard.com/general/set-cron-job.shtml

http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

google search