我有一个功能,当一篇文章在三天后在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 ); ?>
但我不知道如何将其与我的功能链接,以便在三天后向所有注册用户发送电子邮件。
有人能给我一些提示,告诉我如何才能做到这一点吗?谢谢
最合适的回答,由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();
}