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

is_user_logged_in() doesn’t work after custom login and redirect

The issue arises because the is_user_logged_in() function relies on the authentication cookies being recognized by the WordPress request lifecycle. When you set the authentication cookies using wp_set_auth_cookie(), they are sent to the browser, but they are not yet available to the same request that initiated the cookie setting. The cookies will only be recognized on subsequent requests.

// Handle login link
add_action('parse_request', function (&$wp) {
    if (array_key_exists('login_link', $wp->query_vars)) {
        $user = // get user and validate hashed token from url...

        // Log in the user
        wp_clear_auth_cookie();
        wp_set_current_user($user->ID);
        wp_set_auth_cookie($user->ID);

        // Set the global $current_user manually for the current request
        global $current_user;
        $current_user = $user;

        do_action('wp_login', $user->user_login, $user);

        // Redirect to home
        wp_safe_redirect(home_url());
        exit();
    }
});

1).global $current_user; $current_user = $user;: This ensures that the global $current_user variable is set for the current request.

2). wp_set_current_user($user->ID);: Even though you’ve already set the user in wp_set_auth_cookie(), this ensures that WordPress internally knows about the current user for this request.

By doing this, is_user_logged_in() will correctly recognize the logged-in status for the current request.

Related Posts:

  1. after login that will redirect user role into a page
  2. Firing a function AFTER redirect
  3. Is there a hook that runs after a user logs in?
  4. Is there a hook before the user is authenticated?
  5. WordPress auto login after registration not working
  6. Run javascript code after wp_login hook?
  7. Detecting post type within init action
  8. Authentication / login mechanism (non wp-admin)
  9. Redirect logged in users if they are on a specific page
  10. Using `auth_redirect` : keeps asking me to login even when I’m logged in
  11. How can I do customizations on login, registration and password recovery forms?
  12. Redirect users on specific post category or category page
  13. Stuck in redirect loop after using wp_login action
  14. How to get session token of current user in wp_login hook?
  15. Changing “Lost Password Email Link” to custom password reset page
  16. Custom action on login and “remember me”
  17. What’s hook to use immediately after a user is authentcated [duplicate]
  18. How can I force the user to log in, even if they’re already authenticated?
  19. Redirect customer to login if not logged in when proceeding to checkout
  20. Send along login credentials with comment content
  21. Is it possible to add the_content filter upon login?
  22. Custom Login Form – Redirect user to login page if not logged in
  23. A good hook to check authorization and redirect?
  24. What hook to use to redirect based on $post
  25. How to read and write session data?
  26. Template_redirect works, but headers aren’t sent when checking via cURL
  27. template_redirect hooks redirect wrong URL
  28. Updating user meta data from external link, user not logged in
  29. How to add a new link to the default register form’s footer links?
  30. Does wp_login hook fire on user registration?
  31. Changing login url
  32. Not able to set userId and email at wp_set_current_user in Worpdress
  33. Redirecting after login except for a specific page
  34. How to modify username before logging in?
  35. How To Apply Different Styles To All Blocks Based on Post Meta Value?
  36. PHP Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered
  37. Transform .wp-video to the native video player of the browser
  38. Securely log in a user without a password using a link?
  39. Redirect users not logged in to the standard login page (and back) from some posts and pages
  40. How do I override the user’s input when updating a custom post type’s permalink?
  41. change position of element using hook [closed]
  42. The acction hook stop working if i move it from plugin file to theme’s functions.php file
  43. admin_post hook not working
  44. $GLOBALS & global doesn’t work [closed]
  45. Handle POST request sent from an external site for login?
  46. External Authentication
  47. How to add action in wordpress custom plugin?
  48. How can I add new attributes in a Class when my addon is active?
  49. What is meaning of BEFORE and AFTER in this hook name?
  50. How do I change TinyMCE button “i” to create a i tag rather than em? [duplicate]
  51. How to use the password_reset hook to validate new password and display error
  52. How to display before H1 Title
  53. add_action failed to display function by a plugin
  54. Is there any Reference of WordPress hook and hacks? [closed]
  55. wp_footer content appearing in admin area
  56. getSaveContent.extraProps hook for core/post-title doesn’t add new attributes on to the frontend
  57. Thesis Theme -> hooks not working for custom function
  58. Hooks not working on live server
  59. Can I display text that’s generated by a hook in another place in the HTML?
  60. How to disable login and registration pages?
  61. How to set Post ID from context to an attribute in child block of Gutenberg Query Loop
  62. Callback API for getting response from fygaro payment gateway
  63. Use wordpress search hooks to search a second database
  64. Removing script tags that are loaded by other script tag
  65. How can I modify the code generated for a PDF by the Add Media button in Classic Editor?
  66. How to track all users logged into a site?
  67. Redirect all pages to the custom login page except for the registration page
  68. Do New_to_publish hooks work for custom post types
  69. wp_trash_post not firing as expected (also applies to trashed_post)
  70. How to authenticate/verify login credentials & check for user meta without logging in?
  71. WP: Override/update the_content from database with the publicly echo the_content
  72. Why callback function is not working in wp_head hook?
  73. What is the equivalent hook for both admin_head() and wp_head() combined, if any?
  74. Creating my own “recent blog posts” static Gutenberg block, can’t use react hooks in the frontend
  75. wp_login_url always redirects me to wp-admin
  76. woocommerce_add_to_cart_validation max quantity depending on user geoip [closed]
  77. theme.json is altering Group block HTML
  78. How do I set a custom post type Category after import using wp_set_post_terms
  79. Get post ID before add_theme_support
  80. How can I get the ID before after_setup_theme?
  81. Hook on opening a media/document
  82. Login and register by API
  83. Change password reqts with NO plugin without breaking resetpass link?
  84. Hooks to trigger a callback when adding, removing, rearranging or updating a widget in the widget area
  85. Search WordPress Hook for completed Elementor Update
  86. How to filter the URL of thumbnail size in the Media Grid and admin pages
  87. Why does before_delete_post run when opening a post in the backend?
  88. Redirecting logged in users to custom URL
  89. How to check if we are in the init hook?
  90. ‘At a Glance’ dashboard: combining infos
  91. Prefix user login_name and validate it is unique on Registration
  92. Is there a way to assign a default Category to a Post when the user creates a new Post?
  93. wp-login – unable to redirect user to a custom login url
  94. Why does my function create extra empty files?
  95. How to Dynamically Switch WordPress Front Page Without Causing Canonical Redirect?
  96. Show admin notice if metabox field is empty during save post
  97. Redirect to current URL and append specified URL parameter on unsuccessful login through Elementor login form widget
  98. The enqueue_block_assets is changing the styles of the editor interface
  99. update ACF field when saving post on back office
  100. Unable to trigger a frontend @wordpress/hooks action
Categories hooks Tags authentication, hooks, login, redirect
How to allow searching a custom meta key in admin list table?
order archive page by title in wp twenty twenty-four template

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