Update Password Form for registered users in WordPress

update password

In this tutorial, I am gonna cover an important part of the update password form in most of the WordPress websites. If you are having a WordPress website and accepting users to register in your website then this specific requirement is going to be really important for you.

Whenever you develop a website with a user management system, then it is very important to allow your registered users to update their passwords from time to time. But in WordPress, there is no default user password update feature available in frontend pages. Only the admin can update the user password.

So today I am going to help you get your own Password update form created and you can display this form on any page which is accessible to your registered users. So In this tutorial, I will cover up a very brief introduction for all types of users even if they are new to WordPress. So let’s get started

1: Open to your theme’s function.php file.

You can find your function.php file under wp-content/themes/theme_name

2: Add the below Update Password Codes on your function.php file

function change_password_form() { ?>

	<form action="" method="post" class="update-forms">
	    <h4>Update Password</h4>
        <label for="current_password">Enter your current password:</label>
        <input id="current_password" type="password" name="current_password" title="current_password" placeholder="Enter Current Password" required>
        <label for="new_password">New password:</label>
        <input id="new_password" type="password" name="new_password" title="new_password" placeholder="Enter New Password" required>
        <label for="confirm_new_password">Confirm new password:</label>
        <input id="confirm_new_password" type="password" name="confirm_new_password" title="confirm_new_password" placeholder="Confirm New Password" required>
        <input type="submit" value="Change Password">
    </form>
<?php }
function change_password(){
	if(isset($_POST['current_password'])){
		$_POST = array_map('stripslashes_deep', $_POST);
		$current_password = sanitize_text_field($_POST['current_password']);
		$new_password = sanitize_text_field($_POST['new_password']);
		$confirm_new_password = sanitize_text_field($_POST['confirm_new_password']);
		$user_id = get_current_user_id();
		$errors = array();
		$current_user = get_user_by('id', $user_id);
		// Check for errors
		if (empty($current_password) && empty($new_password) && empty($confirm_new_password) ) {
		$errors[] = 'All fields are required';
		}
		if($current_user && wp_check_password($current_password, $current_user->data->user_pass, $current_user->ID)){
		//match
		} else {
			$errors[] = 'Password is incorrect';
		}
		if($new_password != $confirm_new_password){
			$errors[] = 'Password does not match';
		}
		if(strlen($new_password) < 6){
			$errors[] = 'Password is too short, minimum of 6 characters';
		}
		if(empty($errors)){
			wp_set_password( $new_password, $current_user->ID );
			echo '<h2>Password successfully changed!</h2>';
		} else {
			// Echo Errors
			echo '<h3>Errors:</h3>';
		    foreach($errors as $error){
		        echo '<p>';
		        echo "<strong>$error</strong>";
		        echo '</p>';
		    }
		}
    }
}
function cp_form_shortcode(){
	    change_password();
        change_password_form();
}
add_shortcode('changepassword_form', 'cp_form_shortcode');

So after adding above code, its time to save your function.php file and go ahead for next step.

4: Add Shortcode to your preferred page.

You can now add the shortcode [changepassword_form] on your targeted page where you want to show Update Password Form for your registered users.

I hope this tutorial will help you get your problem sorted out.

Please don’t forget to share this tutorial with your friends.

Get Your Quick Tasks Completed

Hire us today to get all your tasks completed with 100% satisfaction.

Leave a Reply

Your email address will not be published. Required fields are marked *

bg author

Manish Bajpai

I am a freelance Web Developer, Zoho One Expert, And Part time blogger. I love developing futuristic websites.

Fill Out this Form to Us

First Name
First Name
Field is required!
Field is required!
Last Name
Last Name
Field is required!
Field is required!
Email Address
Email Address
Field is required!
Field is required!
Phone Number
Phone Number
Field is required!
Field is required!
What you want me to Do?
Field is required!
Field is required!
Number of Hours Required
-
+
Field is required!
Field is required!
Total Cost
$0.00
Field is required!
Field is required!

Minimum Amount to Hire

$25

This is the minimum amount to hire us instantly. Remaining total amount will need to be paid as per the progress of work.