0

I am using plugin for login/logout(https://wordpress.org/plugins/login-logout-menu/) navigation link to be dynamic. Once user role logged in they will redirect to specific page and functionality is working fine. For logged out it should redirect to home page for specific role. I have tried using the hooks provided but it is not working. Currently admin and normal user role logged out to the default login redirect link which is in the wordpress.

 function redirect_after_logout() {

        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];

        if($role_name == 'employee'){
            $redirect_url = site_url();
            wp_safe_redirect( $redirect_url );
            exit;
        } 

    }
    add_action( 'wp_logout', 'redirect_after_logout'  );

1 Answer 1

1

The wp_logout action fires after the user is logged out. They no longer have a role.

However, since WordPress 5.5.0, the hook takes one parameter: the ID of the user who is being logged out. So you might be able to accomplish your goal by using that ID instead of wp_get_current_user().

function redirect_after_logout( $user_id ) {

    $current_user   = get_user_by( 'id', $user_id );
    $role_name      = $current_user->roles[0];

    if($role_name == 'employee'){
        $redirect_url = site_url();
        wp_safe_redirect( $redirect_url );
        exit;
    } 

}
add_action( 'wp_logout', 'redirect_after_logout'  );

If you're not using at least WordPress 5.5.0, this won't work (and you should probably update, too).

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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