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 redirect users based on role and content of redirect_to?

The login_redirect hook does seem to be the right hook.

Can you try this :

(Adapted from the Codex )

function redirect_users_by_role( $redirect_to, $request, $user ) {

    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

        if ( in_array( 'staff', $user->roles ) ) {

            if ($admin_url === $request){

                return home_url('/resources');

            } else{

                return $redirect_to;

            }

        }
    } else {

        return home_url('/access-denied');

    }
}


add_filter( 'login_redirect', 'redirect_users_by_role', 10, 3 );

Related Posts:

  1. How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
  2. PHP If user is logged in & on home page redirect
  3. Problem with login form
  4. Infinite loop when logging out using custom login form
  5. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  6. wp-login.php?redirect_to=https problem
  7. Redirect users not logged in to the standard login page (and back) from some posts and pages
  8. wp_login_url always redirects me to wp-admin
  9. wp-login – unable to redirect user to a custom login url
  10. Redirect to current URL and append specified URL parameter on unsuccessful login through Elementor login form widget
  11. Login to wordpress by clicking a link and specifying usernaname and password in url
  12. Log in / Log Out Custom Button
  13. Set Cache-Control header for 301 redirects
  14. Should `wp_login` be used since it’s deprecated?
  15. Moving code from theme header to functions.php
  16. Change CSS based on is_user_logged_in
  17. How to display login form anywhere, when user isn’t logged in, without redirecting?
  18. How can I open up my administrative panel to everyone?
  19. Showing latest post without 301 redirect
  20. Having Issue on Redirecting With Session in WordPress
  21. Using a $GET parameter from a URL, to redirect to a URL (WordPress)
  22. WordPress redirect redirecting too many times or not at all
  23. Using Argument from Function to Re-Direct Visitor (WordPress)
  24. I am receiving a pluggable.php warning sign on my only http:// page
  25. How can I add a new row in a separate database when someone registers via WordPress?
  26. Login redirect. Check user meta and redirect accordingly
  27. Where to insert redirect code based on http_referer?
  28. Is this a correct usage of ob_start() in my WordPress project?
  29. How to allow download url redirection only if user logged in WordPress site?
  30. One account with multiple logins
  31. Lost in trying to create user database system
  32. Shortcode to log user into current URL
  33. PHP Redirect condition
  34. PHP getting error when trying to access WP-Admin Dashboard
  35. I installed WordPress locally now how do I login?
  36. If user is logged in not working
  37. Change homepage content if user is logged in – BuddyPress
  38. Manually Create PHP Page For A Redirect
  39. Show login greeting above sub-menu links?
  40. Redirect to current user page upon link click
  41. Ajax URLs without #!, how to prevent falling into single.php on load or reload?
  42. is_user_logged_in returning nothing on custom page
  43. Writing a link cloaking plugin
  44. WordPress permalink issue
  45. WP insert post Redirect after function has executed
  46. Help with accessing wp-admin page and resolving error messages
  47. How to redirect non logged in mobile users to page on same site?
  48. How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
  49. Download file and redirect to page or vice versa
  50. Add more user roles to a PHP logout redirect function [closed]
  51. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  52. Can’t log in to WordPress wp-admin after adding code to functions.php
  53. 404 on old link after changing post url
  54. WordPress url By country code?
  55. Check if user had autologin & if so, logout
  56. how can i redirect dynamic URL in worpress base on ID in htaccess or wp-config or function.php
  57. Admin Panel 404 Error after login
  58. PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
  59. How to block specific user id in custom login form?
  60. Log out without confirmation request (nonce)
  61. Implementing a multilingual WordPress site by installing several instances of WordPress and redirecting
  62. WordPress on Ubuntu – “File not Found” for home.php – Trying to Redirect to Root Folder
  63. Having trouble creating two shortcodes, one for logged in user and one for visitors
  64. Call WP Rest-Api to GET /users/me returned NOTHING in console
  65. Remove all feeds and return custom 404 page
  66. Restrict wordpress access to logged users only
  67. Menu not updating for logged in users after redirect
  68. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  69. Issues adding Recaptcha v3 to WordPress Registration
  70. Redirect User when they click Menu Option
  71. Display specific page if user signed in
  72. Change Login or Logout text based on status
  73. WordPress: Redirect Main Site to Subsite in Multisite Where user is NOT logged in
  74. Refresh page after login with litespeed cache
  75. Redirect after login depending on the URL
  76. Redirecting the lost password page request when using a custon login page
  77. How do I send a POST request with params with WordPress REST API
  78. is_user_logged_in() not working in homepage
  79. ERR_TOO_MANY_REDIRECTS / To Force SSL Logins and SSL Admin Access
  80. WordPress Redirect / Add_Rewrite_Rule – Non Index.php Page
  81. Check if a user is logged into my WordPress site which is on a different server
  82. Override 404 when navigating to single php file
  83. Newbie question. Login/Registration. New PHP page
  84. Submit to itself don’t work
  85. Adding a sidebar to wp-login.php
  86. redirect 301 with special character like WIX site “#!”
  87. 301 redirect from webpage to wordpress page in the same root
  88. Submitting my form to the database and then redirecting to payment site
  89. List users in a dropdown for login
  90. Not logged in when using http
  91. add bootstrap modal after login in wordpress
  92. White screen after login attempt
  93. 3 domains, 1 wordpress install, redirecting and changing domains on live site
  94. Get permalink for a post from inside WordPress and route to a related site
  95. Link to external page has wordpress blog-url inside
  96. How to replace wp-admin login page to another location?
  97. How to use multiple 404 Error Pages in WordPress
  98. Check user last login date
  99. Prevent users from display default wordpress login form
  100. login redirect based on user role not work as expected
Categories PHP Tags login, php, redirect
the_terms characters not limiting
How to modify query on category pages to show only sticky posts?

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