Last active
December 20, 2019 16:30
-
-
Save barryhughes/ddb1394774e5432d8de30d5080a913ba to your computer and use it in GitHub Desktop.
Handy dandy WordPress password reset link generator (command line). Useful when we want to create a URL to share with a customer because they are for whatever reason having difficulties using the regular password reset facilities.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generates a password reset link for a user (which we can share directly). | |
* | |
* Run this script through WP CLI and supply the user email as the final arg. | |
* | |
* @see https://developer.wordpress.org/cli/commands/eval-file/ | |
*/ | |
if ( ! class_exists( 'WP_CLI' ) ) { | |
exit( "Use WP CLI to run this script.\n" ); | |
} | |
if ( empty( $args ) ) { | |
WP_CLI::error( "Please supply the user email address." ); | |
} | |
// We expect the final arg to be the user email address. | |
$email = filter_var( end( $args ), FILTER_SANITIZE_EMAIL ); | |
// Make sure it looks like an email address. | |
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { | |
WP_CLI::error( "Sorry, that does not look like a valid email address. Try again." ); | |
} | |
$user = get_user_by( 'email', $email ); | |
if ( ! $user ) { | |
WP_CLI::error( "Sorry, but we cannot locate a user with that email address. Try again." ); | |
} | |
// Generate the password reset link...but first, we need to stop captcha code (or other | |
// security plugin stuff) from interfering. | |
add_filter( 'allow_password_reset', '__return_true', 1000 ); | |
$key = get_password_reset_key( $user ); | |
$login = rawurlencode( $user->user_login ); | |
$url = network_site_url( "wp-login.php?action=rp&key=$key&login={$login}", 'login' ); | |
if ( empty( $url ) || ! filter_var( $url, FILTER_VALIDATE_URL ) ) { | |
WP_CLI::error( "Sorry, but password reset URL generation failed." ); | |
} | |
WP_CLI::line( "OK, here is the password reset link for {$email}:\n\n\t{$url}\n" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment