How to Create Custom Reset or Forget Password in WordPress

Advertisement

Many Personal, Blog and or even Business sites that are now running in WordPress platform, the easy and customizable CMS today and when the site goes larger, ineluctable it need custom functionality like Create Custom Reset/Forget Password, Update Profile where user can update their personal information without accessing admin panel and in this tutorial I would like to share you how we can achieve Forgot/reset password functionality using built-in WP functions, it’s very simple just follow the step by step process below.

PHP code

This contains PHP script to update user password and send email containing new password if successful.


<?php
	global $wpdb;

	$error = '';
	$success = '';

	// check if we're in reset form
	if( isset( $_POST['action'] ) && 'reset' == $_POST['action'] )
	{
		$email = trim($_POST['user_login']);

		if( empty( $email ) ) {
			$error = 'Enter a username or e-mail address..';
		} else if( ! is_email( $email )) {
			$error = 'Invalid username or e-mail address.';
		} else if( ! email_exists($email) ) {
			$error = 'There is no user registered with that email address.';
		} else {

			// lets generate our new password
			$random_password = wp_generate_password( 12, false );

			// Get user data by field and data, other field are ID, slug, slug and login
			$user = get_user_by( 'email', $email );

			$update_user = wp_update_user( array (
					'ID' => $user->ID,
					'user_pass' => $random_password
				)
			);

			// if  update user return true then lets send user an email containing the new password
			if( $update_user ) {
				$to = $email;
				$subject = 'Your new password';
				$sender = get_option('name');

				$message = 'Your new password is: '.$random_password;

				$headers[] = 'MIME-Version: 1.0' . "\r\n";
				$headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				$headers[] = "X-Mailer: PHP \r\n";
				$headers[] = 'From: '.$sender.' < '.$email.'>' . "\r\n";

				$mail = wp_mail( $to, $subject, $message, $headers );
				if( $mail )
					$success = 'Check your email address for you new password.';

			} else {
				$error = 'Oops something went wrong updaing your account.';
			}

		}

		if( ! empty( $error ) )
			echo '<div class="error_login"><strong>ERROR:</strong> '. $error .'</div>';

		if( ! empty( $success ) )
			echo '<div class="updated"> '. $success .'</div>';
	}
?>

HTML Code

This contains simple HTML form code where we put our new password for reset.


	<form method="post">
		<fieldset>
			<p>Please enter your username or email address. You will receive a link to create a new password via email.</p>
			<p><label for="user_login">Username or E-mail:</label>
				<?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
				<input type="text" name="user_login" id="user_login" value="<?php echo $user_login; ?/>" /></p>
			<p>
				<input type="hidden" name="action" value="reset" />
				<input type="submit" value="Get New Password" class="button" id="submit" />
			</p>
		</fieldset>
	</form>

Page Template

Below is the complete custom reset password source code you can try just copy and paste the snippet below to try it yourself.


<?php
/*
Template Name: Reset Page
*/

get_header()
?>
<div class="wrapper">

	<?php
		global $wpdb;

		$error = '';
		$success = '';

		// check if we're in reset form
		if( isset( $_POST['action'] ) && 'reset' == $_POST['action'] )
		{
			$email = trim($_POST['user_login']);

			if( empty( $email ) ) {
				$error = 'Enter a username or e-mail address..';
			} else if( ! is_email( $email )) {
				$error = 'Invalid username or e-mail address.';
			} else if( ! email_exists( $email ) ) {
				$error = 'There is no user registered with that email address.';
			} else {

				$random_password = wp_generate_password( 12, false );
				$user = get_user_by( 'email', $email );

				$update_user = wp_update_user( array (
						'ID' => $user->ID,
						'user_pass' => $random_password
					)
				);

				// if  update user return true then lets send user an email containing the new password
				if( $update_user ) {
					$to = $email;
					$subject = 'Your new password';
					$sender = get_option('name');

					$message = 'Your new password is: '.$random_password;

					$headers[] = 'MIME-Version: 1.0' . "\r\n";
					$headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers[] = "X-Mailer: PHP \r\n";
					$headers[] = 'From: '.$sender.' < '.$email.'>' . "\r\n";

					$mail = wp_mail( $to, $subject, $message, $headers );
					if( $mail )
						$success = 'Check your email address for you new password.';

				} else {
					$error = 'Oops something went wrong updaing your account.';
				}

			}

			if( ! empty( $error ) )
				echo '<div class="message"><p class="error"><strong>ERROR:</strong> '. $error .'</p></div>';

			if( ! empty( $success ) )
				echo '<div class="error_login"><p class="success">'. $success .'</p></div>';
		}
	?>

	<!--html code-->
	<form method="post">
		<fieldset>
			<p>Please enter your username or email address. You will receive a link to create a new password via email.</p>
			<p><label for="user_login">Username or E-mail:</label>
				<?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
				<input type="text" name="user_login" id="user_login" value="<?php echo $user_login; ?/>" /></p>
			<p>
				<input type="hidden" name="action" value="reset" />
				<input type="submit" value="Get New Password" class="button" id="submit" />
			</p>
		</fieldset>
	</form>
</div>

<?php get_footer() ?>

To those who didn’t know yet how to create custom page template visit WP Codex for complete and detailed tutorial.

Happy coding ^_^

Advertisement