How to Create a WordPress Password Reset Link in PHP

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

Shares

Leave a Comment