0

I want to give functionality to user, while their account is created in wordpress by administrator at that time set password email is sent to user, which contains custom page link with activation key, login, and action. I have created one shortcode which used in page for set password from front end. I was able to get data from url, and managed to check whether password is match and not empty, but how can i set this password for new users. Any help/suggestion is appreciated.

add_shortcode( 'RESET_PASSWORD' , 'reset_password_function' );
function reset_password_function() { 
    global $wpdb, $user_ID;

    if(isset($_GET['key']) && $_GET['action'] == "rp") {
        $reset_key  = $_GET['key'];
        $action     = $_GET['action'];
        $user_login = $_GET['login'];
        print_r($reset_key);
        echo '<br/>';
        print_r($action); 
        echo '<br/>';
        print_r($user_login); 

        // Print output like 
        // Kdfd3434Kdfrewfpd
        // rp
        // demo.test@abc.com
    }

    if($_POST['action'] == "agent_pwd_reset"){
        $error = array();

        if(empty($_POST['new_password'])) {
            $error[] = __('Can not set blank new password field','AA');
        } else {
            $agent_new_pass = $_POST['new_password'];
        }

        if(empty($_POST['confirm_password'])) {
            $error[] = __('Can not set blank confirm password field','AA');
        } elseif ($_POST['new_password'] !== $_POST['confirm_password']) {
            $error[] = __('Password not match.','AA');
        } else {
            $agent_confrim_pass = $_POST['confirm_password'];
            $success = __('Password match.','AA');
        }

        if ( count($error) == 0 ) {
            echo '<div class="col-md-6 col-md-offset-4 alert alert-success">'.$success. '</div>'; 
            // here i want to set new password for user
        } elseif ( count($error) > 0 ) {
            echo '<div class="col-md-6 col-md-offset-4 alert alert-danger error">' . implode("<br />", $error) . '</div>'; 
        }

    }
?>
    <div class="row">
       <div class="col-md-6 col-md-offset-3">
            <form class="form-horizontal password_reset_form" method="post" action="">
                <div class="form-group"> 
                    <label class="control-label col-sm-4" for="new_password">
                        <?php _e('New Password', 'AA'); ?>
                    </label>
                    <div class="col-sm-8">
                        <input type="password" class="form-control" name="new_password" id="new_password" required value="" />
                    </div>
                </div>

                <div class="form-group"> 
                    <label class="control-label col-sm-4" for="confirm_password">
                        <?php _e('Confirm Password', 'AA'); ?>
                    </label>
                    <div class="col-sm-8">
                        <input type="password" class="form-control" name="confirm_password" required id="confirm_password" value="" />
                    </div>
                </div>

                <input type="hidden" name="action" value="agent_pwd_reset" />

                <input type="hidden" name="reset_pass_nonce" value="<?php echo wp_create_nonce("reset_pass_nonce"); ?>" />

                <div class="form-group"> 
                    <div class="col-sm-8 col-sm-offset-4">
                        <input name="resetbtn" type="submit" id="resetbtn" class="resetbtn btn btn-primary" value="<?php _e('Reset Password', 'AA'); ?>" />
                    </div>
                </div>

            </form>
        </div>
    </div>

<?php 
}
0

The function you need is wp_set_password( $_POST['new_password'], $user_ID)

This will update the user with new password.

  • How to check user login and activation key, then we can use this function to update user passsoword. – Abhee Oct 16 '18 at 11:05
  • @Abhee I am not sure what you mean. Are these keys generated by your code or by WordPress? Also, are you talking about the default WordPress activation email? – dboris Oct 16 '18 at 11:15
  • I have customized wordpress default user activation email, i want to just give the functionality to user, to let them choose thier password from front end, while they recieve account createion mail. This email contains activation key, and user login, So i was able to fatch these value using $_GET['key'], $_GET['ulogin']. – Abhee Oct 16 '18 at 11:29
  • @Abhee To get help with that, you should show us the relevant code which generates/alters the activation email. And this is another question, you haven't asked this in your question. I answered specifically what you asked: how can i set this password for new users – dboris Oct 16 '18 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.