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 do I implement the WordPress Iris picker into my plugin on the front-end?
  35. Hook ‘wp_enqueue_scripts’ priority has no effect
  36. Hook *after* user password change?
  37. Does anyone have a visual breakdown of core hooks and when they are fired?
  38. Wp_update_post: Infinite loop even with remove_action solution, OOP
  39. Multisite – Redirect All Users to Subsite Home Page on Subsite Login
  40. hook for lostpassword form
  41. how to update current logged user username
  42. Seeking Hook Whenever a Custom Taxonomy Term Has Been Added
  43. How can i trigger an action manually?
  44. Can I trigger the publish_post hook by using wp_insert_post?
  45. WooCommerce New customer email Hook? [closed]
  46. How to check what kind of saving it is?
  47. Override wp_delete_post and move to trash instead for custom post type
  48. Hook all http requests
  49. Setting Cookie with init hook causes ‘header already sent’
  50. wp_authenticate but not logged in
  51. How To Make Sure That My Action Hook Executes Last
  52. Does update_comment_meta hook exists?
  53. Can not set custom title on some WordPress setups
  54. BuddyPress User Profile Menu
  55. Checking post format during xmlrpc_publish_post
  56. Hook for when a page template is changed
  57. How to save generated JWT token to cookies on login?
  58. How to fix ‘WordPress redirection loop problem in wp-login.php page’?
  59. Get all posts with a duplicate name
  60. Save acf field data via acf/save_post before post is saved
  61. Redirect in form handler causing form to be submitted twice
  62. WooCommerce – Redirect to a product after login
  63. Checking login status before wp_get_current_user is initialised
  64. Add media library tab
  65. Conditional hook [closed]
  66. Add_menu_page() error message -> “You do not have sufficient permissions to access this page”
  67. Add a new confirmation page before saving
  68. How to pass argument to wp_footer hook with data from a template
  69. Redirecting from login
  70. How to use a 3rd party library to send emails?
  71. I would like to send a notification email (Asana) whenever something is published (posts, pages, custom post types) [duplicate]
  72. Woocommerce table is missing a heading–can I add it by use of a hook?
  73. Alternative to new_to_publish Hook for Custom Statuses
  74. WordPress redirect redirecting too many times or not at all
  75. Force file download in WordPress
  76. How to use pre_get_posts
  77. hooks for automatic approve user registration according to data in custom fields
  78. How to render an element, that was saved as a template, using a hook?
  79. How to use embed_content hook?
  80. Possibility to login without password
  81. Action hook with wrapper html
  82. call php file from form and use wp functions
  83. wp_insert_post hook for a wordpress plugin
  84. Session management issues with WordPRess 404 Error page
  85. Redirect users with “.” (dot) in their username and replace with “-” (dash) to correct profile
  86. Conditional Login Redirect
  87. Cookie cant be read even cookie is present
  88. How to create callback function which returns all posts with specific data?
  89. Check for empty username or password on login
  90. Unable to log in as admin
  91. How to work with hooks and Posts to posts plugin?
  92. When is get_currentuserinfo() needed?
  93. disable publish button until condition is not met
  94. WordPress ReAuth =1 Loop with wpCAS
  95. How can I modify the code generated for a PDF by the Add Media button in Classic Editor?
  96. Do New_to_publish hooks work for custom post types
  97. Creating my own “recent blog posts” static Gutenberg block, can’t use react hooks in the frontend
  98. How do I set a custom post type Category after import using wp_set_post_terms
  99. Replacing specific Gutenberg blocks
  100. login link with auto_redirect
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
  • 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