0

I'm setting up some transactionals emails on my site and I'd like to send one 3 days after a user signed up if he didn't post. Here is what I have:

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' ); 
}

However, I can't find any info on how to retrieve the duration since sign up. How can I achieve this?

  • 3
    I believe you're looking for $user->user_registered which you would then compare to the current date to get the difference. – Howdy_McGee Nov 30 '15 at 15:52
  • @Howdy_McGee that would make sense as an answer, please :) – Rarst Nov 30 '15 at 17:38
1

WordPress doesn't have a "Duration Since Signup" value so you'll need to calculate it by finding the difference between the user registration date and the current date. I suggest getting the user above the conditional:

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' ); 
}

We know that $curr_user->user_registered holds the date that the user registered and DateTime() will get the current date so we can use the built-in DateTime method diff() to get the difference and put it in as day format as %r%a. There's a good StackOverflow Answer on getting the day difference between two dates.

| improve this answer | |
1

Grab a user object for $ID and do some date math (Almost, but not quite, the same answer as Howdy's):

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
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.