6

It is possible to create a new user by API with the following line:

$user_id = wp_insert_user( $user_data ); 

I wonder how to send the newly created user an email that contains his password? Is there any function in Wordpress API that handles this job or should I create and send an email by myself?

3 Answers 3

36

As David guessed (but didn't specify), there is some functionality inside Wordpress to do this: wp_new_user_notification($user_id, $user_pass).

So, rewriting the above code, it should look like this (code has been edited after parameter deprecation in 4.3.1):

$user_id = wp_insert_user( $user_data ); wp_new_user_notification( $user_id, null, 'both' ); 

Edit: Please also see @ale's comment below.

Sign up to request clarification or add additional context in comments.

5 Comments

is this deprecated now?
@AndrewWelch I read sth about this as well; but it still seems to be in the source of the (currently latest) WordPress 4.5.2. When I checked the source I found out that just the possibility to pass a password to the function was deprecated. In stead, WordPress will now generate a new reset password key for the user, and sends the reset password link via email to the user. Note: i have not tried this yet (was for research purposes only as preparation for a project). I will confirm (or someone else please do) when we'll be touching this.
Not working in my case. Might be because i am using the latest version of wordpress. My code is in page template
According to do the docs (link in my answer), it's available since WP 2.0 and not deprecated. What exactly didn't work? Did you see any errors / log entries?
Since 4.3.1. the second parameter of this function was deprecated. Now you should pass 'null' as a value. So password should not be passed as an argument to this function. And as a third parameter we have the '$notify' option that can accepts: admin, user, both. This parameter is specifying to whom the notification e-mail should be sent.
6

I assume you are generating the password and adding it to the $user_data array?

If not, you can use this to generate a password -

$this->password = wp_generate_password(6, false); $user_data['user_pass'] = $this->password; 

And while there probably is a way of hooking in to the generic WP send password email, I just use my own. That way, I can customise the content, and make it look like other emails from my site.

Note that I have set up a Class for registration, so if you have not, you will need to remove instances of $this->.

function prepare_email(){ $confirmation_to = $_REQUEST['email_address']; $confirmation_subject = 'Confirmation - Registration to My Site'; $confirmation_message = 'Hi '.$_REQUEST['first_name'].',<br /></br />Thank you for registering with My Site. Your account has been set up and you can log in using the following details -<br /><br />' .'<strong>Username:</strong> '.$_REQUEST['username'] .'<br /><strong>Password:</strong> '.$this->password .'<br /><br />Once you have logged in, please ensure that you visit the Site Admin and change you password so that you don\'t forget it in the future.'; $headers = 'MIME-Version: 1.0'."\r\n"; $headers.= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $confirmation_headers = $headers.'From: My Site <[email protected]>'."\r\n"; $this->form_for_email = compact('confirmation_to', 'confirmation_subject', 'confirmation_message', 'confirmation_headers'); } 

1 Comment

Thank you very much for sharing your code. Yes, using my own email would help us to make all emails standardized.
0

The accepted answer is now obsolete. The answer using wp_new_user_notification() works and will send a WordPress-like mail.

Still, you might want to send your own mail as David Gard suggested. Here is a bit of code that does this. You can use it as a function, or as a method in a class.

/** * Send mail with a reset password link * * @param int $user_id User ID */ function notify_new_user( $user_id ) { $user = get_userdata( $user_id ); $subject = '[website] Connection details'; $mail_from = get_bloginfo('admin_email'); $mail_to = $user->user_email; $headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $mail_from, ); $key = get_password_reset_key( $user ); if ( is_wp_error( $key ) ) { return; } $url_password = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ) ); $body = '<p>HTML body in your own style.</p>'; $body .= '<p>It must include the login identifier: ' . $user->user_login . '</p>'; $body .= '<p>And the link: ' . $url_password . '</p>'; wp_mail( $mail_to, $subject, $body, $headers ); } 

If you don’t want to have the HTML content in the function, you can use a template engine. Here is an example you could add to the above code.

// Set up Mustache $path = get_stylesheet_directory() . '/mails'; $dir = wp_get_upload_dir(); $m = new \Mustache_Engine( [ 'cache' => $dir['basedir'] . '/mustache_cache', 'loader' => new \Mustache_Loader_FilesystemLoader( $path ), 'partials_loader' => new \Mustache_Loader_FilesystemLoader( $path . '/layouts' ), ] ); // Variable data in the mail $data['mail']['title'] = $subject; // {{mail.title}} $data['identifier'] = $user->user_login; // {{identifier}} $data['url-password'] = $url_password; // {{url-password}} $body = $m->render( 'mail-template-new-user', $data ); 

Mustache tags documentation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.