【问题标题】:Wordpress wp_schedule_event at specific timeWordpress wp_schedule_event 在特定时间
【发布时间】:2012-06-13 13:11:19
【问题描述】:

我每小时使用以下功能在 Wordpress 中激活/停用和运行 cronjob。

我已经检查了 Wordpress 代码并访问了教程网站,但没有看到在特定时间运行 wp-cron 的示例,例如“每天上午 23.59”。这可能吗?

register_activation_hook(__FILE__, 'cron_activation');

add_action('twitter_cron', 'cron_function');

register_deactivation_hook(__FILE__, 'cron_deactivation'); 

function cron_activation() {
wp_schedule_event(time(), 'hourly', 'twitter_cron');
}

function my_deactivation() {
    wp_clear_scheduled_hook('twitter_cron');
}

function do_this_hourly() {

// my twitter function

if (!wp_next_scheduled('twitter_cron')) {
wp_schedule_event( time(), 'hourly', 'twitter_cron' );
}

【问题讨论】:

  • 本网站与编程有关,并非 wordpress 支持网站。在使用 wordpress.org 支持论坛时,您可能会更幸运。
  • 您要激活/停用什么? (旁注,有一个WP specific stackoverflow
  • 我正在激活一个插件.. 但我会在 WP 堆栈溢出中发布这个问题。谢谢。

标签: php wordpress php-5.3


【解决方案1】:

我找到了解决办法:

wp_schedule_event('1339631940', 'daily', 'my_wp_cron' );  

其中 '1339631940' 是 2012 年 6 月 13 日星期三 23:59:00 +0000 的 linux 时间戳。

【讨论】:

  • 杰森,你的解决方案不起作用,我知道你是如何“接受”这个答案的,但我当然没有。
【解决方案2】:

在 wp_schedule_event($timestamp, $recurrence, $hook, $args);第一个参数是当前时间。 WP 找到 DB 中的最后一个事件(WP_OPTIONS 行 CRON),查看间隔,如果 $timestamp 在之后,则执行 cron。

要在特定时间运行 cron:将 $recurrence 设置得更小(每天 18 小时)并在特定时间之前通过测试停止 cron

$hcron = 3; // 03:00 AM
$t = time();
$on = mktime($hcron,0,0,date("m",$t+(6-$hcron)*3600),date("d",$t+(6-$hcron)*3600),date("Y",$t+(6-$hcron)*3600));
if ($t>=$on) { add_action('cron'...)}

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 2013-11-14
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多