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?
- Unable to logout correctly after wp-login file was modified
- Automatic excerpt is not shown with the_excerpt() command
- Using ob_start and ob_get_clean with wordpress shortcode
- Change “No Comments” link to “My String” on Blog Post (Find Snippet in Code or use CSS, PHP Solution) [closed]
- Return product description
- How to add the sidebar to all the pages except the home page? [closed]
- Header not properly displaying on archive.php
- Custom meta box values are not getting saved for my custom post type
- Get the last 5 products from each category
- How to say if meta_value is greater than 0 in an array?
- Getting users with a specific meta data and then querying their posts?
- Why does the post_type_link hook everything twice?
- Adding a tag_ID column into Categories inside the admin dashboard?
- How to pass a variable into an add_filter() function?
- locale filter function running multiple times
- Multiple requests external data api dynamic block gutenberg
- Why does the Woocommerce grouped template prints the unpublished products?
- Woocommerce with Lazy Load php and ajax
- Adding conditional text to a PHP Shortcode Template
- How do I get the content excerpt of the recent post?
- How to check $_GET isset for a parameter and value? [closed]
- How to deal with too many $_POST variable conditions from ajax request at backend? [closed]
- Making your own custom post views count
- Save Custom CSS file in the upload folder dynamically?
- How to use two meta_compare in an array?
- How to update and save user metadata on page visits?
- Verify if a category is the child of another category
- How to remap one of the TinyMCE Advanced Editor button to open the wordpress media library?
- Simple AJAX notification when the new post is added to the database
- How to optimize update_post_meta?
- Warning: in_array() null given in PHP function
- Contact Form 7 – Replace database configured form template with a static file
- AJAX wp_insert_user WORKS but responds with “The site is not enabled”
- Why can’t I return a value from $wpdb->get var?
- How to get User Time Zone in WordPress?
- Redirection after submitting duplicate comment
- Convert code – not work
- Fix error Gravatar alt
- HTML Special Characters in URL string [closed]
- how to save checkbox data for custom setting?
- How to remove sidebar primary widget on Mobile on category page [closed]
- How to keep the capability of users and disable Gutenberg editor in WordPress?
- Upload multiple files in randomly generated folder using wp_upload_bits
- Is it possible to change content in my PHP file from WordPress dashboard
- ACF put a comma after the last repeater field value [closed]
- Create page template via functions.php?
- Loading two post layouts for the same post with different url
- Code snippet to show current php version inside “At a Glance” box in admin
- parsing nested blocks (in columns, etc) via PHP
- Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
- getting url from variable that calls picture of current page, not working. Only displays everything instead of url
- How to upload WP user avatar on woocommerce account page [closed]
- Use post ID in functions.php file adminside
- How to show childs of certain custom post type in option list?
- Short code template + ajax
- str_replace with the_content is not working
- How do I populate custom field with current user role in Woocommerce [closed]
- WPML best page selector with php
- How to disable send e-mail notification new comments for some posts
- trying to put an active hover to my custom nav category buttons [closed]
- How to edit HTML of my website on WordPress? [closed]
- Redirect first comment (Thanks for comment) with show Autor name and beginning of the comment
- Large WordPress CRON job
- Fixing Deprecated: get_the_author_lastname
- WordPress query undefined offset in loop
- Referencing Images in javascript to display on wordpress page
- Most commented posts by time period (last 12h, last 24h and etc)
- How to: Conditionally Enqueue JS and Stylesheets, for Custom Post Type (Single and Archive Templates)
- Divi change project category slug
- Add meta tags to a custom header
- Ajax filter button display all posts
- Check If Post Was Published More Than 6 Months Ago Using get_the_date
- Where are the src and srcset sizes coming from?
- What is the right way to add PHP code to a certain part of a page