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|
01 Create Reset Password Link
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.
02 Code Explanation
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:
- How To Add Back Button In Elementor
- 403 Error When Updating In Elementor
- How To Add Multiple Post Types In Posts Widget In Elementor
- How to Add Products Per Page Dropdown in WooCommerce
- “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
- How to Create a Plugin in WordPress from Scratch
- How to Disable Admin Bar in WordPress Without Plugin
- How To Send Custom Emails in WordPress
- How to Allow Preview of Draft Post Without Login in WordPress
- Import Users From CSV In WordPress Programmatically
- Dynamically Populate A Select Field’s Choices In ACF
- How To Create Database Table In WordPress
- Difference Of require, include, require_once And include_once
- PHP: Get Day Name From Date
- PHP: Get Year From Date
- 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!