Home » WordPress: Create Reset Password Link

WordPress: Create Reset Password Link

Last updated on June 15, 2021 by

Sometimes, while developing a registration or login functionality we may need to create a reset password link in WordPress. In this article, we will see how to create a reset password link in WordPress. Let’s just dive into it.

Table of Contents
1. WordPress Reset Password Link
2. Code Explanation

Let’s assume, we have a registration page and upon successful registration, an email sent to the user to set a new password via WordPress reset password link. You can easily get that link with the following code:

<?php

/*@ Create set password link */
$user = new WP_User( (int) $user_id );
$reset_key = get_password_reset_key( $user );
$user_login = $user->user_login;

$rp_link = '<a href="' . network_site_url("wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode($user_login), 'login') . '">Set password link</a>';

Now, you can use the $rp_link variable to send reset password link in the email.

Notes: Make sure you have correctly passed the $user_id variable in WP_User() class in the above example code.

Using the WP_User() class we are retrieving the newly created user’s object with the help of user_id. Then we are passing that user object into the get_password_reset_key() function to retrieve the reset password key for a user which is stored in the users’ database table.

The reset password key that is stored in the database allows the $user to have its password changed.

Lastly, we have constructed our full reset password link with the help of network_site_url() function which returns the current site URL.

Additionally, read our guide:

  1. How To Add Back Button In Elementor
  2. 403 Error When Updating In Elementor
  3. How To Add Multiple Post Types In Posts Widget In Elementor
  4. How to Add Products Per Page Dropdown in WooCommerce
  5. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  6. How to Create a Plugin in WordPress from Scratch
  7. How to Disable Admin Bar in WordPress Without Plugin
  8. How To Send Custom Emails in WordPress
  9. How to Allow Preview of Draft Post Without Login in WordPress
  10. Import Users From CSV In WordPress Programmatically
  11. Dynamically Populate A Select Field’s Choices In ACF
  12. How To Create Database Table In WordPress
  13. Difference Of require, include, require_once And include_once
  14. PHP: Get Day Name From Date
  15. PHP: Get Year From Date
  16. How To Update Pivot Table In Laravel

We hope this article helped you to create reset password link in WordPress.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

Leave a Comment

*

code