How to resend username and password to user in WordPress using retrieve_password function

Well, there are no such method where you can resend your existing user a reset password from your side. they must actually click on the "lost password" link and fill up the form in order to retrieve the activation email to reset their password. The method was retrieve_password() which is located in wp-login.php. Hence, you won't be able to use this method and even if you can, you will need to post it over from a form in order to utilized this method. The most easiest way to reuse this method is to recreate it by removing certain condition so that it makes life easier for you.

In my case, my system already has the user email addresses. Hence, all i need to do was to remove the checking for email address and start sending verification from the user whether they wish to reset their password.

/**
 * Handles sending password retrieval email to user.
 *
 * @uses $wpdb WordPress Database object
 *
 * @return bool|WP_Error True: when finish. WP_Error on error
 */
function retrieve_password($user_email) {
	global $wpdb, $current_site;

	$errors = new WP_Error();

	// redefining user_login ensures we return the right case in the email
	$user_login = $user_email;

	do_action('retreive_password', $user_login);  // Misspelled and deprecated
	do_action('retrieve_password', $user_login);



	$key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
	if ( empty($key) ) {
		// Generate something random for a key...
		$key = wp_generate_password(20, false);
		do_action('retrieve_password_key', $user_login, $key);
		// Now insert the new md5 key into the db
		$wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
	}
	$message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
	$message .= network_site_url() . "\r\n\r\n";
	$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
	$message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
	$message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
	$message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";

	if ( is_multisite() )
		$blogname = $GLOBALS['current_site']->site_name;
	else
		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
		// we want to reverse this for the plain text arena of emails.
		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

	$title = sprintf( __('[%s] Password Reset'), $blogname );

	$title = apply_filters('retrieve_password_title', $title);
	$message = apply_filters('retrieve_password_message', $message, $key);

	if ( $message && !wp_mail($user_email, $title, $message) )
		wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );

	return true;
}

The above method take in an email address and send out the email to the user to request a reset of password. You can change the message to your liking but you will need to know the user password in order to use this method. Hope it helps.

Share
Livedrive Simple, Secure Online Backup Increase sales with social commerce at BigCommerce. Learn More!