questions

Filter wp_login function correctly

Rate this post

I hope you’re doing good.

I’m trying to add a custom validation to the WP_login auth. I’m doing this:

public function logoutIfInactive($user_login)
{
    $user = get_user_by('login',$user_login);

    if ( get_user_meta($user->ID, '_architect_active', 1) != 1 )
    {
        return new WP_Error( 'architect_disabled', 'Your architect account is pending of review' );
    }
}

And the hook:

add_filter('wp_login', [$this, 'logoutIfInactive'], -1);

The hook is working, but I can’t get the WP_Error to work. Not sure if I’m doing something wrong or if I should hook another function.

Thanks in advance.

 

✔️Solution:

The issue is that wp_login is an action hook that fires after the users is completely logged in. You need to use something in the middle of login process, best hook for this is wp_authenticate_user

public function logoutIfInactive($user)
{
    if ( get_user_meta($user->ID, '_architect_active', 1) != 1 )
    {
        new WP_Error( 'architect_disabled', 'Your architect account is pending of review' );
    }
    return $user;
}
add_filter('wp_authenticate_user', [$this, 'logoutIfInactive'], 10);

Leave a Reply

Your email address will not be published.

Back to top button