Skip to content

Instantly share code, notes, and snippets.

@steve10287
Created December 21, 2017 15:48
Show Gist options
  • Save steve10287/c9e55dee1e65bd39146f1e8b28da8bb1 to your computer and use it in GitHub Desktop.
Save steve10287/c9e55dee1e65bd39146f1e8b28da8bb1 to your computer and use it in GitHub Desktop.
Wordpress Force Password Reset
<?php
function force_pw_reset_edit_profile( $user ) {
// Proper authentication
if ( ! current_user_can( 'edit_users' ) ) return;
// Do not show on user's own edit screen
if ( get_current_user_id() == $user->ID ) return;
?>
<table class="form-table">
<tr>
<th scope="row">Force Password Reset</th>
<td>
<label for="force_pw_change">
<input name="force_pw_change" type="checkbox" id="force_pw_change" <?php
checked( get_user_option( 'force_pw_change', $user->ID ), TRUE )?> value="1">
Force Password Reset</label>
</td>
</tr>
</table>
<?php
}
add_action( 'edit_user_profile', 'force_pw_reset_edit_profile' );
function force_pw_update_profile( $user_id ) {
// Proper authentication
if ( ! current_user_can( 'edit_users' ) ) return;
// Do not show on user's own edit screen
if ( get_current_user_id() == $user_id ) return;
if ( !empty( $_POST['force_pw_change'] ) ) {
update_user_option( $user_id, 'force_pw_change', TRUE, FALSE );
}
}
add_action( 'edit_user_profile_update', 'force_pw_update_profile' );
function check_user_force_pw() {
if ( false == is_user_logged_in() ) return;
$user_id = get_current_user_id();
$userdata = get_userdata($user_id);
if ( get_user_option( 'force_pw_change', $user_id ) ) {
wp_logout();
wp_redirect(wp_lostpassword_url() . '&fpr=1&fpremail=' . urldecode($userdata->user_email) ); exit;
}
}
add_action( 'init', 'check_user_force_pw' );
function un_force_pw_change($user) {
update_user_option( $user->ID, 'force_pw_change', FALSE, FALSE );
}
add_action ( 'after_password_reset', 'un_force_pw_change' );
add_filter( 'login_message', 'password_reset_message_text_change' );
function password_reset_message_text_change( $message ) {
$action = $_REQUEST['action'];
if( $action == 'lostpassword' && isset($_REQUEST['fpr']) && isset($_REQUEST['fpremail']) ) {
$message = '<script>alert("You must reset your password to access the members area.");setTimeout(function(){ document.body.style.opacity = 0; document.getElementById("user_login").setAttribute("value", "' . urldecode($_REQUEST['fpremail']) . '"); document.getElementById("lostpasswordform").submit(); }, 50);</script>';
return $message;
}
return $message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment