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).