0

I am trying to over-ride the wp_new_user_notification() function but I am having issues checking if the user has been approved or if "Send the new user an email about their account" is checked.

The reason I need to perform these checks is because I have noticed that the over-ride ignores if a user is approved or not and also ignores if "Send the new user an email about their account" checkbox is ticked or not.

if ( !function_exists('wp_new_user_notification') ) {
    function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {

        $user = new WP_User($user_id);
        $plaintext_pass = wp_generate_password();
        wp_set_password( $plaintext_pass, $user_id );

        $user_login = stripslashes($user->user_login);
        $user_email = stripslashes($user->user_email);

        if ( empty($plaintext_pass) )
        return;


        if(get_user_meta($current_user->ID,'active',true) == 1){
          $subject = "Thank you for registering for ".get_option('blogname')."";
          $message = "Thank you for registering for ".get_option('blogname')."\n";
          $message .= "Your registration for ".get_option('blogname')." has been approved.\n\n";
          $message .= "Your registration information is below.\n";
          $message .= "You may wish to retain a copy for your records.\n\n";
          $message .= "username: ".$user_login."\n";
          $message .= "password:  ".$plaintext_pass."\n\n";
          $message .= "You may log in here:\n";
          $message .= site_url( '/')."\n\n";
          $message .= "----------------------------------\n";
          $message .= "This is an automated message from ".get_option('blogname')."\n";
          $message .= "Please do not reply to this address\n";

          wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
        }


    }
}
  • have you looked at the source of the original function? – Milo May 15 '17 at 16:30
  • Yes but I couldn't find anything that checks if the "Send the new user an email about their account." is checked or not. – ahmed May 16 '17 at 8:52
0

I believe I found a solution. Not sure if it is the best solution but at least it serves the purpose:

if ( !function_exists('wp_new_user_notification') ) {
  function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {

    if($_POST['action'] == 'createuser'){

      if($_POST['send_user_notification'] == 1){

        $user = new WP_User($user_id);
        $plaintext_pass = $_POST['pass1'];
        wp_set_password( $plaintext_pass, $user_id );

        $user_login = stripslashes($user->user_login);
        $user_email = stripslashes($user->user_email);

        if ( empty($plaintext_pass) )
        return;

        if($_POST['activate_user'] == 1){
          $headers = "From: xxxxxx <xxxxxxx@xxxxxxx.xx>\r\n";
          $subject = "Thank you for registering for ".get_option('blogname')."";
          $message = "Thank you for registering here are your log in details keep them safe, your account is now active\n\n";
          $message .= "username: ".$user_login."\n";
          $message .= "password:  ".$plaintext_pass."\n\n";
          $message .= "Click here to log in:\n";
          $message .= site_url( '/')."\n\n";
          $message .= "----------------------------------\n";
          $message .= "This is an automated message from ".get_option('blogname')."\n";
          $message .= "Please do not reply to this address\n";

          wp_mail($user_email, $subject, $message, $headers);

        }else{

          $headers = "From: xxxxxx <xxxxxxx@xxxxxxx.xx>\r\n";
          $subject = "Thank you for registering for ".get_option('blogname')."";
          $message = "Thank you for registering here are your log in details keep them safe, your account is pending so we will be in touch shortly\n\n";
          $message .= "username: ".$user_login."\n";
          $message .= "password:  ".$plaintext_pass."\n\n";
          $message .= "----------------------------------\n";
          $message .= "This is an automated message from ".get_option('blogname')."\n";
          $message .= "Please do not reply to this address\n";

          wp_mail($user_email, $subject, $message, $headers);

        }

      }

    }
  }
}

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.