• Hi,
    I’m trying to redirect users the first time they login so they can change passwords to a cleaner one than what they receive in the register email. In functions.php I have:

    //redirect on first login
    function redirect_password_profile_update($user_id, $old_user_data) {
        $user = new WP_User( $user_id );
        if( $user->data->user_pass != $old_user_data->user_pass) {
            //password has changed
            update_metadata("user",$user_id,"changed_password",true);
        }
    }
    add_action("profile_update", "redirect_password_profile_update", 10, 2);
    
    function redirect_password_login_redirect($redirect_to, $url_redirect_to = '', $user = null) {
    
        if( isset($user->ID) ) {
            $changed_password = get_metadata("user", $user->ID, "changed_password",true);
            if( $changed_password != true ) {
                return get_bloginfo('url') . "/wp-content/themes/xchange12/change-password.php/";
            } else {
                return $redirect_to;
            }
        }
    }
    add_filter('login_redirect', 'redirect_password_login_redirect',10,3);
    
    function redirect_password_password_reset( $user ) {
        //password has been reset to a random one. so the changed_password meta data should be reset as well
        if( isset($user->ID) ) {
            delete_metadata("user", $user->ID, "changed_password");
        }
    }
    
    And in change-password.php I have:
    
    <form action="" type="POST">
    <label>Password</label>
    <input type="password" name="new_pass" id="new_pass">
    <input type="submit" value="Submit">
    </form>
    <?php
    require(ABSPATH.'/wp-load.php');
    $new_pass = $_POST['new_pass'];
    $user_id = get_current_user_id();
    $user_login = get_userdata($user_id);
    $user_login = $user_login->user_login;
    
    wp_update_user(array('ID' => $user_id, 'user_pass' => $new_pass));
    wp_redirect(home_url());

    Can anyone tell me why this is not working? It updates the database with a blank value in the user_pass section and upon hitting submit i get the error message: Warning: get_object_vars() expects parameter 1 to be object, null given in /Applications/MAMP/htdocs/wordpress/xchange/wp-includes/user.php on line 1404

    and it does not redirect

    Thank you in advance

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. Or better still – use the pastebin. As it stands, your code may now have been permanently dmaged/corrupted by the forum’s parser.]

Viewing 1 replies (of 1 total)
  • Thread Starter bmd91

    (@bmd91)

    Hi,
    I’m trying to redirect users the first time they login so they can change passwords to a cleaner one than what they receive in the register email. In functions.php I have:

    //redirect on first login
    function redirect_password_profile_update($user_id, $old_user_data) {
        $user = new WP_User( $user_id );
        if( $user->data->user_pass != $old_user_data->user_pass) {
            //password has changed
            update_metadata("user",$user_id,"changed_password",true);
        }
    }
    add_action("profile_update", "redirect_password_profile_update", 10, 2);
    
    function redirect_password_login_redirect($redirect_to, $url_redirect_to = '', $user = null) {
    
        if( isset($user->ID) ) {
            $changed_password = get_metadata("user", $user->ID, "changed_password",true);
            if( $changed_password != true ) {
                return get_bloginfo('url') . "/wp-content/themes/xchange12/change-password.php/";
            } else {
                return $redirect_to;
            }
        }
    }
    add_filter('login_redirect', 'redirect_password_login_redirect',10,3);
    
    function redirect_password_password_reset( $user ) {
        //password has been reset to a random one. so the changed_password meta data should be reset as well
        if( isset($user->ID) ) {
            delete_metadata("user", $user->ID, "changed_password");
        }
    }

    And in change-password.php I have:

    <form action="" type="POST">
    <label>Password</label>
    <input type="password" name="new_pass" id="new_pass">
    <input type="submit" value="Submit">
    </form>
    <?php
    require(ABSPATH.'/wp-load.php');
    $new_pass = $_POST['new_pass'];
    $user_id = get_current_user_id();
    $user_login = get_userdata($user_id);
    $user_login = $user_login->user_login;
    
    wp_update_user(array('ID' => $user_id, 'user_pass' => $new_pass));
    wp_redirect(home_url());

    Can anyone tell me why this is not working? It updates the database with a blank value in the user_pass section and upon hitting submit i get the error message: Warning: get_object_vars() expects parameter 1 to be object, null given in /Applications/MAMP/htdocs/wordpress/xchange/wp-includes/user.php on line 1404

    and it does not redirect

    Thank you in advance

    [ Please do not bump, that’s not permitted here. ]

Viewing 1 replies (of 1 total)
  • The topic ‘Force Password Change First Time Login’ is closed to new replies.