帖子到期后向所有注册用户发送电子邮件

时间:2014-01-31 作者:agis

我有一个功能,当一篇文章在三天后在CPT内发布时,页面会重定向到另一个URL/PERMALINK。

代码如下:

add_action(\'template_redirect\', \'check_the_date_for_stats\');

function check_the_date_for_stats() {
  if ( is_singular(\'debate\') ) { // adjust myCPT with your real cpt name
    $pl = get_permalink( get_queried_object() ); // permalink
    $is_stats = array_key_exists( \'stats\', $GLOBALS[\'wp_query\']->query ); // is stats?
    $is_cm = array_key_exists( \'comments\', $GLOBALS[\'wp_query\']->query ); // is comments?
    $ts = mysql2date(\'Ymd\', get_queried_object()->post_date_gmt ); // post day
    $gone = ($ts + 3) < gmdate(\'Ymd\'); // more than 3 days gone?
    if ( $gone && ( ! $is_stats && ! $is_cm ) ) {
       // more than 3 days gone and not is stats => redirect to stats
       wp_redirect( trailingslashit($pl) . \'/stats\' );
       exit();
    } elseif( ! $gone && ( $is_stats || $is_cm ) ) {
       // we are in 3 days frame and trying to access to stats => redirect to post
       wp_redirect( $pl );
       exit();
    }
  }
}
有没有可能在3天后向所有注册用户发送一封带有标准消息的电子邮件,如果有,我该怎么做?我知道这个功能:

<?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?>但我不知道如何将其与我的功能链接,以便在三天后向所有注册用户发送电子邮件。

有人能给我一些提示,告诉我如何才能做到这一点吗?谢谢

1 个回复
最合适的回答,由SO网友:Mridul Aggarwal 整理而成

如果站点上有太多注册用户,请不要这样做。下面是代码中经过编辑的部分。

} elseif( ! $gone && ( $is_stats || $is_cm ) ) {
   // we are in 3 days frame and trying to access to stats => redirect to post

   // Start the code to send the emails
   $subject = \'3 days have passed\';
   $users = get_users();
   foreach($users as $user) {
      $message = \'Hello \'.$user->display_name.\', \'.PHP_EOL;
      $message .= \'The 3 days have past since the post.\';
      wp_mail($user->user_email, $subject, $message);
   }
   // End the code to send the emails

   wp_redirect( $pl );
   exit();
}

结束

相关推荐

If is_single in functions.php

我希望删除wpautop筛选器仅对我博客中的帖子起作用。因为在某些页面,我需要autop,而在某些页面,我需要它不在那里。我使用以下规则,这是在我的主题函数中。php:remove_filter( \'the_content\', \'wpautop\' ); 因为我只想在博客上看到它,所以我在考虑if语句,我在Wordpress中搜索了条件标记页面,发现我可以使用is\\u single。但它不起作用。这是我现在使用的代码。if(is_single() ){ remove_f