每天特定时间的WP_Schedule_Event

时间:2015-02-27 作者:Mortzea

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

但我想是代码的问题。。

不工作

历元时间戳:1427488800

2015年3月28日凌晨1:10:00

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\');
}

4 个回复
最合适的回答,由SO网友:amir rasabeh 整理而成

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

现在有两种解决方案:

禁用WP Cron,使用真正的Cron作业并对其进行自定义

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

<在中使用自定义间隔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...
}
你可以看到这些图坦卡门

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.nextscripts.com/tutorials/wp-cron-scheduling-tasks-in-wordpress/

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

一二

SO网友:Goran Jakovljevic

而不是time(), 使用strtotime() 函数,以便您可以指定一天中的时间-它将使用今天的日期和您指定的时间。因此,在您的情况下:

strtotime(\'16:20:00\'); // 4:20 PM
中的用法wp_schedule_event 函数如下所示:

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

SO网友:vancoder

好吧,1427488800决定于2015年3月27日举行,所以您的活动甚至还没有开始。

此外,请记住,计划的WP事件只有在有人当时到达现场时才会触发。

SO网友:Rob P

这个代码对我有用,我相信它更接近原始问题。您希望在希望执行时获取UNIX时区。一旦cron执行并从WP中删除,它就会在指定的时间重新创建自己。

在下面的示例中,我让它在每天早上6点为AEST(GMT+10)工作。所以我把它安排在每天格林尼治时间20:00。

if (!wp_next_scheduled(\'cron_name\')) {
    $time = strtotime(\'today\'); //returns today midnight
    $time = $time + 72000; //add an offset for the time of day you want, keeping in mind this is in GMT.
    wp_schedule_event($time, \'daily\', \'cron_name\');
}

结束

相关推荐

以正确的方式在多站点上运行WP Cron

我有多个网站的WordPress Multisite。我设置了DISABLE_WP_CRON 到true 在我的wp-config.php.如果我们使用wget或curl设置cron任务,则有30秒的规则来执行PHP脚本。它太小了,无法发送成吨的电子邮件通知和做其他事情(可能远程SMTP服务器连接很慢,可能是来自bbPress的大量电子邮件通知或其他任何东西)。也许我们可以用这样的东西?php -q wp-cron.php 但它只能在多站点中对一个站点运行cron(每个站点在不同的MySQL表中