Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to hook a logout funtion for specific usr role in wordpress?

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

  • wp_logout
  • get_user_by()

Related Posts:

  1. Adding “Remember Me” in custom login
  2. How build a custom login/register form with error handling?
  3. How to log out everywhere else, destroy all sessions “all other devices”?
  4. How to redirect users to custom lostpassword page?
  5. WordPress 4 invalid username special charachters issue
  6. Remove “Remember Me” from login form
  7. Change WordPress default registration error text [Error: This username is invalid because it uses illegal characters. Please enter a valid username.]
  8. Anyway to output the registration form like the login form with wp_login_form()?
  9. Overriding WP login credentials
  10. Wp-login appears White Screen, Error: Cannot modify header information
  11. How to: PHP Log Out Link?
  12. Login/logout in header
  13. Force Users To Relogin
  14. how to prevent wordpress admin from logging in via woocommerce my-account page
  15. How do I create a function that modifies a message in the wp-login.php file?
  16. Logout redirects to default page
  17. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  18. Custom Login page, redirection and restrictions
  19. Check if user had autologin & if so, logout
  20. Log out without confirmation request (nonce)
  21. Change button link to add nonce
  22. Infinite loop when logging out using custom login form
  23. Change Login or Logout text based on status
  24. Redirect users by role to custom pages
  25. How to hide header and footer from page template
  26. How do you create a front end form that enables the editing of member-specific custom fields in WordPress?
  27. Unable to logout correctly after wp-login file was modified
  28. Automatic excerpt is not shown with the_excerpt() command
  29. Using ob_start and ob_get_clean with wordpress shortcode
  30. Change “No Comments” link to “My String” on Blog Post (Find Snippet in Code or use CSS, PHP Solution) [closed]
  31. Return product description
  32. How to add the sidebar to all the pages except the home page? [closed]
  33. Header not properly displaying on archive.php
  34. Custom meta box values are not getting saved for my custom post type
  35. Get the last 5 products from each category
  36. How to say if meta_value is greater than 0 in an array?
  37. Getting users with a specific meta data and then querying their posts?
  38. Why does the post_type_link hook everything twice?
  39. Adding a tag_ID column into Categories inside the admin dashboard?
  40. How to pass a variable into an add_filter() function?
  41. locale filter function running multiple times
  42. Multiple requests external data api dynamic block gutenberg
  43. Why does the Woocommerce grouped template prints the unpublished products?
  44. Woocommerce with Lazy Load php and ajax
  45. Adding conditional text to a PHP Shortcode Template
  46. How do I get the content excerpt of the recent post?
  47. How to check $_GET isset for a parameter and value? [closed]
  48. How to deal with too many $_POST variable conditions from ajax request at backend? [closed]
  49. Making your own custom post views count
  50. Save Custom CSS file in the upload folder dynamically?
  51. How to use two meta_compare in an array?
  52. How to update and save user metadata on page visits?
  53. Verify if a category is the child of another category
  54. How to remap one of the TinyMCE Advanced Editor button to open the wordpress media library?
  55. Simple AJAX notification when the new post is added to the database
  56. How to optimize update_post_meta?
  57. Warning: in_array() null given in PHP function
  58. Contact Form 7 – Replace database configured form template with a static file
  59. AJAX wp_insert_user WORKS but responds with “The site is not enabled”
  60. Why can’t I return a value from $wpdb->get var?
  61. How to get User Time Zone in WordPress?
  62. Redirection after submitting duplicate comment
  63. Convert code – not work
  64. Fix error Gravatar alt
  65. HTML Special Characters in URL string [closed]
  66. how to save checkbox data for custom setting?
  67. How to remove sidebar primary widget on Mobile on category page [closed]
  68. How to keep the capability of users and disable Gutenberg editor in WordPress?
  69. Upload multiple files in randomly generated folder using wp_upload_bits
  70. Is it possible to change content in my PHP file from WordPress dashboard
  71. ACF put a comma after the last repeater field value [closed]
  72. Create page template via functions.php?
  73. Loading two post layouts for the same post with different url
  74. Code snippet to show current php version inside “At a Glance” box in admin
  75. parsing nested blocks (in columns, etc) via PHP
  76. Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
  77. getting url from variable that calls picture of current page, not working. Only displays everything instead of url
  78. How to upload WP user avatar on woocommerce account page [closed]
  79. Use post ID in functions.php file adminside
  80. How to show childs of certain custom post type in option list?
  81. Short code template + ajax
  82. str_replace with the_content is not working
  83. How do I populate custom field with current user role in Woocommerce [closed]
  84. WPML best page selector with php
  85. How to disable send e-mail notification new comments for some posts
  86. trying to put an active hover to my custom nav category buttons [closed]
  87. How to edit HTML of my website on WordPress? [closed]
  88. Redirect first comment (Thanks for comment) with show Autor name and beginning of the comment
  89. Large WordPress CRON job
  90. Fixing Deprecated: get_the_author_lastname
  91. WordPress query undefined offset in loop
  92. Referencing Images in javascript to display on wordpress page
  93. Most commented posts by time period (last 12h, last 24h and etc)
  94. How to: Conditionally Enqueue JS and Stylesheets, for Custom Post Type (Single and Archive Templates)
  95. Divi change project category slug
  96. Add meta tags to a custom header
  97. Ajax filter button display all posts
  98. Check If Post Was Published More Than 6 Months Ago Using get_the_date
  99. Where are the src and srcset sizes coming from?
  100. What is the right way to add PHP code to a certain part of a page
Categories PHP Tags logout, php, wp-login-form
How to get User Time Zone in WordPress?
How to change the order of the subcategory and category in a post?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress