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 use login_redirect with a user capability

OK, it wasn’t easy to catch, but… There is one major problem in your code…

First check you make is:

if ( is_user_logged_in() == 1 ) {

And is_user_logged_in() is based on global $current_user variable. But… As you can read in login_redirect hook docs:

The $current_user global may not be available at the time this filter
is run. So you should use the $user global or the $user parameter
passed to this filter.

So this condition won’t be satisfied – so your code won’t change anything.

You should use $user variable that is passed as param, so this should do the trick:

public function login_redirect( $redirect_to, $request, $user ) {

    if ( is_a ( $user , 'WP_User' ) && $user->exists() ) {

        if ( $user->has_cap( 'manage_woocommerce' ) ) {

            $redirect_to = get_admin_url() . 'admin.php?page=my-page';

        }

    }

    return $redirect_to;
}
add_filter( 'login_redirect', array( $this, 'login_redirect' ), 10, 3 );

Related Posts:

  1. wordpress custom login successful redirect hook
  2. Janrain/Simple Modal under Redirected Domain
  3. WordPress Admin login redirect to homepage
  4. How do I to override login redirect on specific pages?
  5. How to create WordPress login for different users, with different roles?
  6. Login Customizer doesn’t change the background of the register form
  7. easy steps to make front end form without plugin
  8. Removing Automatic Redirects Without Plugin
  9. Change wp-login to custom URL login page
  10. WordPress 4.3 broke meta redirect (with url params plugin)
  11. Edit Permalink Structure For Custom Post Type or Modify .htaccess?
  12. Login problem after installing my written plugin [closed]
  13. Too many login attempts
  14. Get ‘Headers already sent’ error for the plugin I am creating when I try to login
  15. Mask and Track Outbound Links
  16. Custom Login Page — wp_signon Headers Already Sent?
  17. How can I redirect some pages to new subdomain? [closed]
  18. Theme My Login Shortcode Doesn’t Return Anything
  19. Add to array, redirect and display
  20. Possibility to login without password
  21. how do i change my website facebook login button to another text immediately user login? [closed]
  22. WordPress unable to write files in the server
  23. Conditional custom redirect
  24. Custom PHP Page Using WordPress login
  25. How to Use the Filter “sidebar_login_widget_form_args”
  26. Making the ‘add to cart’ button redirect to PayPal
  27. Manage PDF downloads and protected pages
  28. PHP mobile redirect Endless loop
  29. How to force load a page in plugin?
  30. If I use an alternative login (e.g. CAS or other SSO) plugin, is my site protected from the recent brute force login attempts?
  31. login in wordpress using gmail account
  32. wp redirect 301 not working in wordpress page
  33. Preserve Search engine index while shifting to new Domain
  34. How can I replace content on site generated from plugin without changing plugin
  35. Linking form to user meta fields
  36. WordPress Multisite Profile Picture Sync Error with Nextend Social Login Plugin
  37. Redirect unloggedin users
  38. Plugin or ways to limit number of users logging in the website,
  39. force logged in user to stay in the dashboard
  40. login form should redirect to register page for in 1st login next time it should redirected to home page
  41. Identify User Language, Redirect to the corresponding page and Save the chosen language as Cookie
  42. www redirects to another directory in wordpress
  43. Hidden permalink different from displayed permalink
  44. redirect to homepage once action is completed
  45. Display value of the GET parameter in a new URL page
  46. Share login credential with QR code
  47. Redirect to a page while maintaining search query parameters without causing an infinite loop
  48. Which membership plugin for a simple sign in? Personal areas for customers
  49. Discern a specific plugin’s action hooks
  50. Can’t add new plugin or themes on a wordpress multisite network from the primary site as administrator. Error: ERR_TOO_MANY_REDIRECTS
  51. How to update all in-site redirect URLs to destination URLs at once
  52. Login/password protected “client page”
  53. How to send new visitor to a splash page for only one time in wordpress?
  54. Why is my site still showing insecure icon even if I have SSL certificate?
  55. Reverse count page view and show on other page
  56. https rewrite not working for All in one security Brute force > rename login url
  57. On button click, redirect users to registration page instead of another page
  58. How can I show login popup when user clicks on download button
  59. When the user entered an unauthorized url redirect to login page
  60. How to show private pages based on a user’s role?
  61. redirect to a custom page
  62. Why does WordPress use cookies for /wp-admin and /wp-content/plugins for non-admin users [duplicate]
  63. .htaccess file doesn’t work, with hundred tries
  64. Identify if the_post hook is being called from the admin post list
  65. using wordpress login details for other website / application / forum?
  66. Infinite 301 redirects after definitions in “Redirections” plugin?
  67. wp_signon returns user, in popup window, but the user is not logged in
  68. How to Create Custom Dashboard for my Laundry Website?
  69. wp_login_form() ignoring login_form action hook
  70. User content database [closed]
  71. Clone a Post Tite and Custom Field into Custom Post type
  72. Direct URL to a template via plugin
  73. Auto-login from backend
  74. AJAX login without a plugin does not work. when add a action to function.php
  75. Plugins effecting layout & login
  76. Redirect default login page to a custom page [duplicate]
  77. How to redirect from plugin page
  78. wp-admin will not redirect to wp-login.php
  79. Template redirect inside of plugin not redirecting
  80. Adding google authenticator and use only email address of user
  81. Can’t redirect to previous page after using GET
  82. Adding a Filter to Sidbar Login Plugin to Change Login Button Lable
  83. First argument is expected to be a valid callback for cp_admin_init and _canonical_charset
  84. how to protect wordpress website
  85. Need to change contact email depending where user is from
  86. Redirect url in plugin to somewhere else?
  87. WordPress ReAuth =1 Loop with wpCAS
  88. HTTP Error 403 When Trying to Login
  89. Advice on setting up private site
  90. Checking url from plugin [duplicate]
  91. What plugin(s) are best for this User registration task? [closed]
  92. Trouble Removing Plugin [closed]
  93. Change All Login/Signup Links in Plugin
  94. How to link that “logged in” in “you must be logged in to post a comment” with custom login page on WordPress?
  95. Simple WordPress function / plugin to redirect a site
  96. plugin for different writers [closed]
  97. How to resolve these findings from security audit
  98. ERROR: Cookies are blocked due to unexpected output – no access to FTP
  99. How to Find WordPress site has backdoor login Codes
  100. URL Redirect on GoDaddy platform [closed]
Categories plugins Tags login, plugins, redirect
Is it possible to install WordPress Multisite on a subdomain with subdirectories?
Only allow the post author and admin to comment on 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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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