jeudi 3 décembre 2015

WordPress invalid key error when sending password to new subscriber

First, let me mention that this is in a multi-site test environment so...

I am building a custom approval flow for new subscribers and I have a function that acts on "set_user_role" and if the new role is "subscriber" it sends the password.

Here is my code which I pretty much copied from the original function found here: http://ift.tt/1QwqyFO

public function set_user_role( $user_id, $role = null, $old_roles = null ) {
    global $wpdb, $wp_hasher;
    $user = get_userdata( $user_id );
    if ( in_array( "subscriber", $user->roles ) ) {
        // email welcome message to users.
        $options = $this->get_options();
        $key = wp_generate_password( 12, false );
        do_action( 'retrieve_password_key', $user->user_login, $key );

        if ( empty( $wp_hasher ) ) {
            require_once ABSPATH . WPINC . "/class-phpass.php";
            $wp_hasher = new PasswordHash( 8, true );
        }
        $hashed = time() . ":" . $wp_hasher->HashPassword( $key );
        $wpdb->update( $wpdb->users, array( "user_activation_key" => $hashed ), array( "user-login" => $user->user_login ) );

            $message = sprintf( __( "Username: %s" ), $user->user_login ) . "\r\n\r\n";
            if ( $options['welcome_message'] != "" ) {
                $message .= __( $options['welcome_message'] ) . "\r\n\r\n";
            }
            $message .= __( "To set your password, visit the following address:" ) . "\r\n\r\n";
            $message .= "<" . network_site_url( "itnet/wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ), 'login' ) . "\r\n\r\n";
            $message .= wp_login_url() . "\r\n\r\n";

            $message .= __( "Adios!" ) . "\r\n\r\n";

            wp_mail( $user->user_email, sprintf( __( "[%s] Your username and password info" ), $blogname ), $message );
        }
    }

I have tried doing this with the original function as well and it didn't even send the email to the user address so I can't be sure if it would have worked.

Any help would be, well, helpful :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire