WordPress Change Password Form Without Plugin

WordPress Change Password Form Without Plugin
199 Views

Are you need simple Change Password form without plugin for your WordPress website? Yes, Then i have created [cxc_change_pwd_form] shortcode with quick simple code. So you can put Change Password form anywhere like in Pages, Posts and Widget etc… Therefore you just need to put [cxc_change_pwd_form] shortcode where you need Change Password form. So you need to follow below steps.

screenshot webbydemo.in 2022.11.29 15 56 41

Code for shortcode.

You need to put below code in theme’s functions.php file. If you have created child theme then add this in function file of child theme.

Change Password Form | Shortcode: [cxc_change_pwd_form]

<?php
add_shortcode( 'cxc_change_pwd_form', 'cxc_change_pwd_form_callback' );

function cxc_change_pwd_form_callback() {
	ob_start();
	if ( is_user_logged_in() ) {

		global $changePasswordError, $changePasswordSuccess;

		if ( !empty( $changePasswordError ) ) {
			?>
			<div class="alert alert-danger">
				<?php echo $changePasswordError; ?>
			</div>
		<?php } ?>

		<?php if ( !empty( $changePasswordSuccess ) ) { ?>
			<br/>
			<div class="alert alert-success">
				<?php echo $changePasswordSuccess; ?>
			</div>
		<?php } ?>

		<form method="post" class="wc-change-pwd-form">
			<div class="change_pwd_form">

				<div class="log_pass">
					<label for="user_oldpassword">Old Password</label>
					<input type="password" name="user_opassword" id="user_oldpassword" />
				</div>

				<div class="log_pass">
					<label for="user_password">New Password</label>
					<input type="password" name="user_password" id="user_password" />
				</div>

				<div class="log_pass">
					<label for="user_cpassword">Confirm Password</label>
					<input type="password" name="user_cpassword" id="user_cpassword" />
				</div>

				<div class="log_pass">
					<?php
					ob_start();
					do_action( 'password_reset' );
					echo ob_get_clean();
					?>
				</div>

				<div class="log_user">
					<?php wp_nonce_field( 'changePassword', 'formType' ); ?>
					<button type="submit" class="register_user">Submit</button>
				</div>

			</div>
		</form>
		<?php
	}
	$change_pwd_form = ob_get_clean();
	return $change_pwd_form;
}
?>

Code for Change Password

In this code i am validating form and Change Password in WordPress on wp action. Add it in functions.php file after above code. If this code will not work then change action wp to init.

<?php
add_action( 'wp', 'cxc_user_change_pwd_callback' );

function cxc_user_change_pwd_callback() {

	if ( isset( $_POST['formType'] ) && wp_verify_nonce( $_POST['formType'], 'changePassword' ) ) {
		global $changePasswordError, $changePasswordSuccess;

		$user = wp_get_current_user();

		$changePasswordError = '';
		$changePasswordSuccess = '';
		$u_opwd = trim( $_POST['user_opassword'] );
		$u_pwd = trim( $_POST['user_password'] );
		$u_cpwd = trim( $_POST['user_cpassword'] );

		if ( $u_opwd == '' || $u_pwd == '' || $u_cpwd == '' ) {
			$changePasswordError .= '<strong>ERROR: </strong> Enter Password.,';
		}

		if ( !wp_check_password( $u_opwd, $user->data->user_pass, $user->ID ) ) {
			$changePasswordError .= '<strong>ERROR: </strong> Old Password wrong.,';
		}

		if ( $u_pwd != $u_cpwd ) {
			$changePasswordError .= '<strong>ERROR: </strong> Password are not matching.,';
		}

		if ( strlen( $u_pwd ) < 7 ) {
			$changePasswordError .= '<strong>ERROR: </strong> Use minimum 7 character in password.,';
		}

		$changePasswordError = trim( $changePasswordError, ',' );
		$changePasswordError = str_replace( ",", "<br/>", $changePasswordError );

		if ( empty( $changePasswordError ) ) {
			wp_set_password( $u_pwd, $user->ID );
			do_action( 'wp_login', $user->user_login, $user );
			wp_set_current_user( $user->ID );
			wp_set_auth_cookie( $user->ID );
			$changePasswordSuccess = 'Password is successfully updated.';
		}
	}
}
?>
Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.