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

Logout users upon login, based on caps/role?

There are a couple of “timing” issues you’re running into here.

When you’re calling wp_get_current_user() this isn’t really available at the moment of logging in, so to capture the logging-in user, you have to use a slightly different approach.

The same is with logout, as it uses the same method of getting the current user.

In the solution below you’re capturing the logging-in user directly from the wp_login hook, and then instead of calling wp_logout(), you’re calling the actual functions that do the logging-out for you. And, instead of adding multiple hooks, you’re doing it all within the one hook: wp_login

There’s also a sanity check to ensure the $user is actually an \WP_User object, or you’ll get a fatal error on checking the capability.

function logout_pending_users( $username, $user ) {
    
    if ( $user instanceof \WP_User && !$user->has_cap( 'read' ) ) {

        wp_destroy_current_session();
        wp_clear_auth_cookie();
        wp_set_current_user( 0 );
        
        wp_redirect( 'https://example.com/pending/' );
        
        exit;
    }
}
add_action( 'wp_login', 'logout_pending_users', 100, 2 );

Related Posts:

  1. what’s the meaning of the field wp_capabilities in table wp_usermeta
  2. Add Custom User Capabilities Before or After the Custom User Role has Been Added?
  3. How to Structure a New Role/Capability Scheme?
  4. How to allow Unfiltered HTML in a wordpress multisite install
  5. Is there any way to check for user login and send him to login?
  6. Limit role to one plugin [duplicate]
  7. Menu page with minimum capability as ‘Subscriber’ doesn’t allow ‘Admin’ to access it?
  8. How to determine which capability to use?
  9. Allow contributor user role to perform copy operation PHP
  10. Buddy Press restrict the capability to edit users
  11. What is more secure checking capabilities of user or checking role of user in WordPress plugin development
  12. Need edit profile link in the menu for logged in users
  13. How do I create a custom role capability?
  14. Send user activation email when programmatically creating user
  15. Can someone explain what wp_session_tokens are, and what are they used for?
  16. Change default admin page for specific role(s)
  17. How to Change the Entire WordPress Admin panel Look and Feel?
  18. Init action hook running late after PayPal’s return url?
  19. How to check WordPress website username and password is correct
  20. Log in from one wordpress website to another wordpress website
  21. WordPress Capabilities: edit_user vs edit_users
  22. Problems after wp_set_password() containing an apostrophe
  23. Allowing Custom Capability to Manage Plugin Options
  24. Two-step login process – Is it possible?
  25. How to add more than 1 user role to sub-menu pages
  26. Odd behaviour with submenu link creation
  27. How do I approach removing menu items on the fly based on settings in my plugin?
  28. How to restrict plugin’s sub-menu pages to admin/subscribers?
  29. How to not let a user with a new role edit users that have administrator role?
  30. Execute plugin for specific user role(s) only
  31. query users by role
  32. Hide plugin dashboard menu item for specific roles
  33. Proper way to pass credentials in a custom login form to avoid “headers already sent”
  34. get_posts() not working when accessing with a custom user role
  35. External Authentication, session_tokens not destroyed on logout
  36. Verify if user is wordpress logged in from another app since wordpress 4.0
  37. How to customize login process
  38. wp_insert_user() function password never match
  39. Does wp_login only trigger before an user signs in into the admin panel?
  40. Set different custom menu items for different user roles
  41. Prevent third party plugin’s admin page access based on user type
  42. Is it possible to make sure that only my plugins output is shown to the enduser?
  43. Password field is empty when using wp_signon();
  44. Plugin Development for registered users
  45. You do not have sufficient permissions to access this page on a submenu
  46. wp_dropdown_roles() to replace option value = code
  47. Create a custom capability to allow an ‘Editor’ to edit only ‘Subscriber’ users
  48. Enqueue script globally
  49. WP Multisite login not working on one subsite. Possibly cookies/ history issue?
  50. Hide custom post type by user roles
  51. Lock out all WordPress Administrators except two specific users
  52. WordPress custom post type capabilities issue
  53. Check user’s role and store in variable
  54. WordPress: Custom User Role cannot access Custom Post Type | “Sorry, you are not allowed to access this page”
  55. Logout after clicking URL link results in “headers already sent” error
  56. How do I validate extra pin field on my WordPress login form page?
  57. How to use gettext for specific user role
  58. How to redirect home page to another page after login for all user?
  59. current_user_can(‘administrator’) not working in custom login
  60. Unable to access custom plugin backend
  61. How to share user data across multiple WordPress websites?
  62. How To Create A File Archive in WordPress?
  63. How can I change my assigned user role in WordPress 3.5.1?
  64. insufficient permissions; coding an action for plugin governed by custom capability
  65. Allowing duplicating users with same user_login and user_email
  66. How to Login a User inside a Plugin and Redirect to page?
  67. Using custom IDP with WP
  68. Hiding the WordPress login and password fields from login page
  69. Enable a role named ‘backend_user’ to access my plugin pages
  70. Add custom parameter for custom user role
  71. How to give custom roles the capability to edit one Menu instead of every Menu
  72. redirect_to how to make it simply work with get parameter or similar?
  73. How I can give access to my custom plugin for editor roles user?
  74. Remove all capabilities in separate method fails versus included in method
  75. Remove from a div by class name from post page if post author role is not administrator
  76. Adding admin for specific users
  77. New Users are saved with no role selected
  78. WordPress User Management Departmental Managers
  79. Plugin capabilities
  80. Multiple Users Logged In Causing Incorrect Account Returned
  81. Get user logged in status from within a plugin. $current_user not defined
  82. Create a Custom Login System in WordPress [closed]
  83. how can I insert a link on login page
  84. user can login from single account detail from multiple locations(computer) at the same time [closed]
  85. Remove default wordpress roles
  86. how to works woocommerce cart hash
  87. Enqueue a file that’s not js or css
  88. WordPress Plugin Boilerplate: Addition of 3rd party scripts and styles [closed]
  89. How to format custom fields when editing an attachment?
  90. Woocommerce – Provide Associated Category Link for Product List Plugin [closed]
  91. how to create category with code in wordpress using form
  92. How can I call wp-load.php in my plugin file
  93. add_filter img_caption_shortcode not implemented
  94. Import user data using CSV
  95. How to create new content type and flushes rewrite rules without visiting the permalinks page?
  96. Enqueueing common php scripts in a plugin
  97. How to add Plugin functionality in WordPress Frontend Menus
  98. How do I get variables from my plugin’s settings page?
  99. How add default term meta to prevent an error?
  100. plugin content on front-page only. Nowhere else
Categories plugin-development Tags capabilities, login, logout, plugin-development, user-roles
WordPress: get recent posts, delete the current category
503 Service Unavailable error was encountered

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