检索自注册以来的持续时间

时间:2015-11-30 作者:Graham Slick

我正在我的网站上设置一些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\' ); 
}
但是,我找不到关于如何检索注册后的持续时间的任何信息。我怎样才能做到这一点?

2 个回复
最合适的回答,由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 得到两个日期之间的日差。

SO网友:s_ha_dum

获取的用户对象$ID 做一些约会数学(几乎,但不完全,与Howdy的答案相同):

function user_help_signup( $ID ) {
  $user = get_user_by(\'id\',$ID);
  $user_reg = new DateTime($user->data->user_registered);
  $now = new DateTime();
  $diff = $user_reg->diff($now);

  if ( $diff->days > 3 || count_user_posts( $post->post_author ) > 1 ) {
      return;
  }
  //... your code
}

相关推荐

Register email as username

我在Wordpress多站点中使用Wordpress 4.9.4,我需要允许用户使用其电子邮件地址作为用户名进行注册。查看代码,我发现阻止我完成的是ms函数。php有一个正则表达式过滤除a-z以外的其他字符的用户名,因此当电子邮件是用户名时,我会得到“用户名只能包含小写字母(a-z)和数字。”我在ms函数中更改了该正则表达式。php,它工作得很好。现在我的问题是,我不想每次更新wordpress时都重新编码wpmu\\u validate\\u user\\u signup函数。在这个问题上有什么好的做法