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?
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.
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 <no-reply@mysite.com>'."\r\n";
$this->form_for_email = compact('confirmation_to', 'confirmation_subject', 'confirmation_message', 'confirmation_headers');
}
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 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.