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
Related Posts:
- Adding “Remember Me” in custom login
- How build a custom login/register form with error handling?
- How to log out everywhere else, destroy all sessions “all other devices”?
- How to redirect users to custom lostpassword page?
- WordPress 4 invalid username special charachters issue
- Remove “Remember Me” from login form
- Change WordPress default registration error text [Error: This username is invalid because it uses illegal characters. Please enter a valid username.]
- Anyway to output the registration form like the login form with wp_login_form()?
- Overriding WP login credentials
- Wp-login appears White Screen, Error: Cannot modify header information
- How to: PHP Log Out Link?
- Login/logout in header
- Force Users To Relogin
- how to prevent wordpress admin from logging in via woocommerce my-account page
- How do I create a function that modifies a message in the wp-login.php file?
- Logout redirects to default page
- Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
- Custom Login page, redirection and restrictions
- Check if user had autologin & if so, logout
- Log out without confirmation request (nonce)
- Change button link to add nonce
- Infinite loop when logging out using custom login form
- Change Login or Logout text based on status
- Redirect users by role to custom pages
- How to hide header and footer from page template
- How do you create a front end form that enables the editing of member-specific custom fields in WordPress?
- Convert hex color to RGB values in PHP
- Deprecated: mysql_connect()
- Where does PHP store the error log? (PHP 5, Apache, FastCGI, and cPanel)
- How can I fetch loop of post titles via AJAX?
- How to change the wp-login.php page title?
- Should I write a PHP function in home.php?
- How to exclude pages from the search results
- How can I limit the length of the previous/next posts in my WordPress Theme?
- how to put the logo in the center of other items in the navigation bar
- Debugging an error: wp_enqueue_style was called incorrectly
- How do I hide specific user profile fields?
- Using PHP to toggle stylesheet and header image, set in cookie
- Crop image from get_theme_mod Customizer field
- How can I get content inside one shortcode to another shortcode?
- Putting PHP variables into javascript [duplicate]
- wp_loaded hook block script enquequing
- Insert a button on a page with random number generation
- Change the site tagline (or similar) based on current page
- hover image appears below placeholder instead of overlayed
- Trying to remove post thumbnail with plugin
- How to find error in my code when the error message is pointing to WP core file?
- WP_Query Check if post has one or more attached medias
- I cant receive an HTTP POST request correctly
- Hooking into ‘authenticate’ causes login to submit on page load
- Pass variable from one function to another
- Issues getting PHP to display in category pages
- Upload multiple images to a Woocomerce product
- Position banner to right of logo in header – messed up my header file
- WordPress Quick Question . How to Get Parent Link in Submenu in My Code
- How to edit widget code to add unique class name to each div?
- Exclude category
- WordPress wp_add_inline_style unable to give hex colors?
- Parse error: syntax error, unexpected end of file
- PHP Strict Standards: Only variables should be assigned by reference
- Woocommerce Product_cat for current category page
- Using CFDB7 vs Custom MySQL Database [closed]
- Get featured image with custom size outside WordPress
- How to use the checked() function to check for a non-empty var
- How to add post thumbnail dynamically using Jquery?
- How to do something when user profile rendereing?
- GD star rating function problem [closed]
- Custom functions for string data calculations
- How are these PHP functions applied to a specific page in WordPress?
- Display additional user fields
- “pre_get_posts” orderby custom date field in different format?
- Woocommerce template file outputting tags
- How to update posts’ custom taxonomy selection?
- Reliable way to add nonce to HTTP Header in WordPress?
- readable version of wp_query output
- How to Call on WordPress Custom Fields without a Plugin
- How to defer javascript from within the functions.php file without losing all styling?
- How to use Contact Form 7 shortcode value in a page?
- Save custom field on WP_List_Table
- How to empty price of simple and variable product when product changed into out off stock
- Cannot transfer ajax post data from js to php file
- Create Schema in WordPress with PHP
- Add element after navigation element title don’t works like I expect
- Combine multiple queries, array_unique returns nothing
- How to make `wp-list-table` show the` custom-fields` I have in Custom-Post
- Woocommerce custom attributes list help
- custom fields anchor points php
- How to delete specific element when it is not homepage?
- Global filter not working
- Search results template listing all posts
- How do I correct this filecount?
- Stripe button is not working in a template page
- How to properly send many mysql queries via php and what happens with ajax requests after they are sent?
- Using catch_that_image() for galleries?
- Automatically create a loop for post ID
- PHP code rendered in HTML
- Conditional Content Display
- How to use custom fields to replace top-level parent title with an image using wp_list_pages?
- How to add margin underneath woocommerce total price amount -> productpages
- Query posts by custom fields (object post)