WordPress的 - 如何安排功能来运行每48小时

问题描述 投票:0回答:2

我有一系列保存在我的数据库的报价,我想一个烧毛,随机引用从数据库中拉出并在我的网站每48小时显示。

我发现代码安排这次使用自定义的cron安排事件每48小时发生。然而,我假设我错误地执行它,因为虽然单个随机引用是显示在网站上了,这是每个页面刷新时间切换到一个新的随机的报价,相对于每48小时一次。

function quote_custom_cron_schedule( $schedules ) {
$schedules['every_two_days'] = array(
    'interval' => 172800, // Every 48 hours
    'display'  => __( 'Every 48 hours' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'quote_custom_cron_schedule' );

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

//Hook into that action that'll fire every 48 hours
 add_action( 'quotes_cron_hook', 'quotes_cron_function' );

//Function that executes via cron
function quotes_cron_function() {
$args = array(
'post_type' => 'quote',
'orderby' => 'rand',
'posts_per_page'=> 1,
);

  $quotes = new WP_Query( $args );
  $quotes_output = '';

  if( $quotes->have_posts() ):
    while ( $quotes->have_posts() ) : $quotes->the_post();
      $quotes_title = get_the_title();
      $quotes_content = get_the_content();
      $quotes_output .= '<div id="quote"><p>&quot;' . $quotes_content . '&quot;</p><p class="quote-name">' . $quotes_title . '</p></div>';
    endwhile;
  else:
      $quotes_output = '';
  endif;

  wp_reset_postdata();
  echo $quotes_output;
}

然后我在我的header.php文件如下:

<?php do_action( 'quotes_cron_hook' ); ?>

这是执行的功能,但它执行的每个页面刷新时间。我需要的功能再次改变报价之前等待48小时。我觉得我可能失去了一些东西简单在这里。

php wordpress loops cron schedule
2个回答
1
投票

你的问题是likly这样的:

if ( ! wp_next_scheduled( 'quote_cron_hook' ) ) {
    wp_schedule_event( time(), 'every_two_days', 'quotes_cron_hook' );
}

当你有quote_cron_hook insted的quotes_cron_hook一个有s一个没有。这种不匹配会导致病情永远不会失败,因为该事件被命名为错误的,这是从来没有计划,这种情况在经过每一个请求。这反过来运行具有wp_schedule_event的开始时间time功能(见下文)。

如果你看一下下面wp_schedule_event链接的文档。在这里你有time()这是第一个参数

$时间戳(整数)(必需)要发生事件的第一次。这必须是在UNIX时间戳格式。 WP cron的使用UTC / GMT时间,而不是本地时间。使用时间(),这始终是GMT在WordPress。

请注意,这是你想要事件发生的第一时间与time()就是,现在,因为它的命名错了,它执行这个每个页面加载,然后运行现在。

你当然也可以设置time()到别的东西,但它只是掩盖了问题。

其他的东西

这也可能是错误的。

  wp_schedule_event( time(), 'every_two_days', 'quotes_cron_hook' );

更具体地说,在看文档

https://codex.wordpress.org/Function_Reference/wp_schedule_event

$复发(字符串)(必需的)事件应该多久再次发生。有效值:

  • 每小时
  • 每天两次
  • 日常

默认值:无

我没有看到every_two_days在那里,除非它是无证。说实话,我没有使用WP调度得多。该Default: None可能是你实际在这里,像这样它退到无WP不能识别该值。但究竟是做什么我不知道。

更其他的东西

正如我在评论中说,(这可能是确定供您使用,但请记住它)

当活动请求发生在WP调度程序只能运行,所以WP调度只能被授权者,一个工作不会给定的时间之前运行。有没有承授人,这将在以后的特定时间运行,它运行在规定的时间之后的下一个请求

这种加入WP-config.php文件

 define('DISABLE_WP_CRON', true);

禁用内置在cron /调度。然后进入C-面板或编辑的crontab(crontab -e命令),并添加此cron作业运行每分钟(或一些有用的间隔)

 php -q pathtowp/wp-cron.php

这个做什么,它的WordPress采取scheduar时序的控制离开,并给它的“实际”科雷应用程序(在Linux上)。这样,它不是依靠页面加载。

但是,正如我说,因为这是一个后有人要查看,正常的WP行为可能是完全对你罚款,因为它是。通常,如果我做的调度我只是依靠真正的CRON,什么这就是为什么我提到它。

希望能帮助到你!


0
投票

我想你想这个错误的方式。你在你的头,它每次都将触发do_action( 'quotes_cron_hook' )动作在页面加载,然后允许quotes_cron_hook每触发时挂到该行动已quotes_cron_function()。这似乎与你报道的行为是一致的。

问题是,设置WP cron作业不会强制挂钩,只在特定的时间间隔运行。在你的情况下,quotes_cron_hook动作触发,每次页面加载,但它也触发一次每48小时。

在我看来,像你期望的功能就是拉一个“临时”的报价,但后来改变它每48小时。要做到这一点,你想是这样的:

if ( ! wp_next_scheduled( 'change_quote_hook' ) ) {
    wp_schedule_event( time(), 'every_two_days', 'change_quote_hook' );
}

add_action( 'change_quote_hook', 'change_quote_function' );

function change_quote_function() {
      $args = array(
          'post_type' => 'quote',
          'orderby' => 'rand',
          'posts_per_page'=> 1,
      );

      $quotes = new WP_Query( $args );
      $quotes_output = '';

      if( $quotes->have_posts() ):
          while ( $quotes->have_posts() ) :
              $quotes->the_post();
              // Set the temporary post using a WordPress option
              update_option( 'quote_post', get_the_ID() );
          endwhile;
      endif;

      wp_reset_postdata();
}

然后,cron作业会触发此功能每48小时一次(或多或少人士指出),并设置好它就可以显示的报价。然后,你的头可以做的动作,以每次显示的报价页面加载。

add_action( 'display_quote', 'display_quote_function' );
function display_quote_function() {
    // Get the quote post ID
    // The default, 1, will be returned if the option is not set
    $quote_id = get_option( 'quote_post', 1 );

    $quote = get_post( $quote_id ); 
    $quotes_title = quote->post_title;
    $quotes_content = quote->post_content;
    $quotes_output = '<div id="quote"><p>&quot;' . $quotes_content . '&quot;</p><p class="quote-name">' . $quotes_title . '</p></div>';
    echo $quotes_output;
}

do_action('display_quote');
© www.soinside.com 2019 - 2024. All rights reserved.