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. Redirection plugin – how to let the editor access the ‘redirection’ menu?
  6. How can I make an Ajax login form work with FORCE_SSL_ADMIN enabled?
  7. SSL redirect loop using WordPress HTTPS Plugin
  8. Redirection Plugin: Redirect all URLs with a regular expression
  9. How to get user-meta from Social Login registered users?
  10. Which hook should be used to validate custom form fields on the login form?
  11. flush_rewrite_rules not working on plugin deactivation, invalid URLs not showing 404
  12. Can Not Redirect from Plugin-Registered Admin Page
  13. Tracking last login and last visit
  14. Detecting $_GET parameters from any page + Cookie
  15. How to send “Location” header on plugin form submit event?
  16. Prevent Brute Force Attack
  17. Login with email (WP Modal Login)
  18. Check if the front end user is log in or not
  19. How to create a word press user with hashedpassword
  20. Is there any good tutorial to write custom login, registration and password recovery forms? [closed]
  21. How to redirect Old Post URL to new Post and keep Old post Comments?
  22. What’s the _wp_desired_post_slug value for?
  23. Redirect all blog links from WordPress.com site to new domain
  24. WordPress login with Phone Number [closed]
  25. Cannot access wp-admin after disabling all plugin
  26. wp_authenticate but not logged in
  27. how can I link to a PlugIn admin-sub-menu page after processing a formular
  28. How does automatic redirection for the redirection plugin work?
  29. How do i login when i cant access wp-login.php?
  30. Changed permalink structure. Need help with redirecting old posts
  31. Landing Page Redirect Chain | http->https->https www
  32. Single central login for front end users from any site
  33. Redirection based on location but without affecting search bots
  34. how to create site exit messages with destination url displayed
  35. Plugin: Google Analytics for Dashboard error – Timestamp is too far from current time
  36. “Request has expired” with “Make your site social” (Gigya) plugin
  37. WordPress Shortcode to get URL Parameters $_GET[‘name’] redirects for no reason at all
  38. How to save generated JWT token to cookies on login?
  39. How to fix the woocommerce – adaranth.com redirection issue [closed]
  40. Redirecting thousands of posts that currently have no category in their permalink when changing permalink structure to include category
  41. easy steps to make front end form without plugin
  42. Change wp-login to custom URL login page
  43. WordPress 4.3 broke meta redirect (with url params plugin)
  44. Edit Permalink Structure For Custom Post Type or Modify .htaccess?
  45. Too many login attempts
  46. Custom Login Page — wp_signon Headers Already Sent?
  47. How can I redirect some pages to new subdomain? [closed]
  48. Add to array, redirect and display
  49. Custom PHP Page Using WordPress login
  50. How to Use the Filter “sidebar_login_widget_form_args”
  51. Manage PDF downloads and protected pages
  52. PHP mobile redirect Endless loop
  53. How to force load a page in plugin?
  54. login in wordpress using gmail account
  55. wp redirect 301 not working in wordpress page
  56. How can I replace content on site generated from plugin without changing plugin
  57. Redirect unloggedin users
  58. force logged in user to stay in the dashboard
  59. login form should redirect to register page for in 1st login next time it should redirected to home page
  60. Identify User Language, Redirect to the corresponding page and Save the chosen language as Cookie
  61. Hidden permalink different from displayed permalink
  62. redirect to homepage once action is completed
  63. Display value of the GET parameter in a new URL page
  64. Share login credential with QR code
  65. How to update all in-site redirect URLs to destination URLs at once
  66. Why is my site still showing insecure icon even if I have SSL certificate?
  67. Reverse count page view and show on other page
  68. How can I show login popup when user clicks on download button
  69. When the user entered an unauthorized url redirect to login page
  70. Why does WordPress use cookies for /wp-admin and /wp-content/plugins for non-admin users [duplicate]
  71. .htaccess file doesn’t work, with hundred tries
  72. Identify if the_post hook is being called from the admin post list
  73. using wordpress login details for other website / application / forum?
  74. wp_signon returns user, in popup window, but the user is not logged in
  75. How to Create Custom Dashboard for my Laundry Website?
  76. User content database [closed]
  77. Clone a Post Tite and Custom Field into Custom Post type
  78. Direct URL to a template via plugin
  79. AJAX login without a plugin does not work. when add a action to function.php
  80. Plugins effecting layout & login
  81. Redirect default login page to a custom page [duplicate]
  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. Need to change contact email depending where user is from
  85. Redirect url in plugin to somewhere else?
  86. HTTP Error 403 When Trying to Login
  87. What plugin(s) are best for this User registration task? [closed]
  88. Trouble Removing Plugin [closed]
  89. How to link that “logged in” in “you must be logged in to post a comment” with custom login page on WordPress?
  90. Simple WordPress function / plugin to redirect a site
  91. URL Redirect on GoDaddy platform [closed]
  92. how to make wordpress remember my choice
  93. WordPress Homepage Login
  94. I cannot login after installing the wp-login timeout setting plugin
  95. whole website redirected to another page
  96. Issue: Unable to Access WordPress Admin and Database Errors
  97. Fatal Error in my WP “Call to undefined function get_option()”
  98. Custom Login and Password Reset for BuddyPress Website
  99. Lost Password of my site, how to reset wordpress password?
  100. wp_logout function not executed
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
  • 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