If you’re using custom code to create users in WordPress, you may need to create a password reset link that you can email to new users once their account is created. Here’s how to do it.
Creating the Password Reset URL
Here’s the code to create the URL:
// CREATE A PASSWORD RESET LINK
$user_object = new WP_User( $user_id );
$key = get_password_reset_key( $user_object );
$reset_password_url = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_object->user_login ), 'login' );
The only thing you need to do is supply the $user_id.
The key function is get_password_reset_key(). That creates a key and stores a hashed version in the database field for that user. The “key” query string supplies that key to the login page, where it is compared with the database key.
I hope this was helpful. Let me know how it works for you in the comments! – Brian
![Brian Shim](https://brianshim.com/wp-content/uploads/2023/11/IMG_7536_small_vert.jpg)
I am a freelance web developer and consultant based in Santa Monica, CA. I’ve been designing websites using WordPress and from scratch using HTML, CSS, PHP, and JavaScript since 2010. I create websites and web applications for businesses, nonprofits, and other organizations. I have a degree in Electrical Engineering (BSEE) from California Institute of Technology and a degree in Engineering Management (MSEM) from Stanford University.
Discover more from Web Developer Tips and Tricks
Subscribe to get the latest posts sent to your email.
Please Leave a Question or Comment