我正在我的网站上设置一些transactionals电子邮件,如果用户注册后3天没有发布,我想发送一封。以下是我所拥有的:
function set_mail_html_content_type() {
return \'text/html\';
}
add_action( \'user_help_signup\', 10 ,2 );
function user_help_signup( $ID, //DURATION SINCE SIGN UP ) {
if ( //DURATION SINCE SIGN UP > 3days || count_user_posts( $post->post_author ) > 1 )
return;
$to = get_user_by( \'id\', $post->post_author )->user_email;
$subject = \'Need help ?\';
$headers = array(\'Content-Type: text/html\');
$message = \'<h3>Hi {display_name}! </h3> <p>
You signed up 3 days ago on mysite.com and we wanted to know if we could help you to get started \';
wp_mail( $to, $subject, $message, \'Content-Type: text/html\' );
}
但是,我找不到关于如何检索注册后的持续时间的任何信息。我怎样才能做到这一点?
最合适的回答,由SO网友:Howdy_McGee 整理而成
WordPress没有“注册后持续时间”值,因此您需要通过查找用户注册日期和当前日期之间的差异来计算它。我建议将用户置于条件之上:
function set_mail_html_content_type() {
return \'text/html\';
}
add_action( \'user_help_signup\', 10 ,2 );
function user_help_signup( $ID, //DURATION SINCE SIGN UP ) {
$curr_user = get_user_by( \'id\', $post->post_author );
$reg_date = new DateTime( $curr_user->user_registered );
$curr_date = new DateTime();
$days_reg = intval( $curr_date->diff( $reg_date )->format( "%a" ) );
if ( $days_reg > 3 || count_user_posts( $post->post_author ) > 1 ) {
return;
}
$to = $curr_user->user_email;
$subject = \'Need help ?\';
$headers = array(\'Content-Type: text/html\');
$message = \'<h3>Hi {display_name}! </h3> <p>
You signed up 3 days ago on mysite.com and we wanted to know if we could help you to get started \';
wp_mail( $to, $subject, $message, \'Content-Type: text/html\' );
}
我们知道这一点
$curr_user->user_registered 保存用户注册的日期,以及
DateTime() 将获取当前日期,以便我们可以使用内置的DateTime方法
diff() 获取差异并将其作为
day format as %r%a. 有一个好的
StackOverflow Answer 得到两个日期之间的日差。