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

redirect_to not Including Hashtag from URL

function after_login_redirect_to_destination(){
    if (!is_user_logged_in() && !is_home()){
        $redirect = home_url() . '/wp-login.php?redirect_to=' . urlencode( $_SERVER['REQUEST_URI'] );
        wp_redirect( $redirect );
        exit;
    }
}
add_action( 'wp', 'after_login_redirect_to_destination', 3 );

the above sample code works and redirect to the login page and after successfull login it will redirect to home page. Whenever a non-logged in user try to access the home page, it will redirect to the login page and after successfull login then it will redirect back to destination page. Here the destination page(home page in this case) . You can modify the code above to your redirect to your custom login page and after success it will then redirect to the current page you were

Hope that helps!

Related Posts:

  1. Action wp_login_failed not working if only one field is filled out
  2. Change register form action url
  3. Restricting frontend acess based on user role otherwise redirect to login form
  4. wp_get_referer not working properly after wp_redirect
  5. Login redirects and query strings
  6. How To Change Wp Register/Login URL Permanently To My Custom Page
  7. Adding rewrite rule dynamically for search results redirecting to 404 URL
  8. How can I login as admin after redirect to custom login page
  9. Redirect wp-login
  10. Changed Wordress Address URL Accidently
  11. Do more action after login successfully
  12. How to redirect home page to another page after login for all user?
  13. Hide Author page from others
  14. Require re-login when logged-in user attempts to access restricted page
  15. This webpage has a redirect loop issue
  16. Trying to re-direct users to specific page based on an ACF variable
  17. WordPress site login Redirect
  18. Problem in auto login after registration
  19. WordPress Redirect After logging
  20. A way to redirect users when they log in based on their current page
  21. Remember me doesn’t work with www?
  22. Trim the repeated value in URL
  23. redirect not logged in users to specific page and redirect them back where they come from
  24. Login form- no feedback
  25. Replace the “wp-admin” in the URL with “xyz-admin” in WordPress dashboard
  26. Can’t access wordpress mgt dashboard until propogation finished?
  27. In Django, how do I know the currently logged-in user?
  28. Can I programmatically login a user without a password?
  29. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  30. Is there any way to rename or hide wp-login.php?
  31. How to login with email only no username?
  32. How can I redirect user after entering wrong password?
  33. Increase of failed login attempts, brute force attacks? [closed]
  34. Login page ERROR: Cookies are blocked due to unexpected output
  35. Redirect user to original url after login?
  36. Separate registration and login for different roles
  37. SSO / authentication integration with external ‘directory service’
  38. Preventing session timeout
  39. How reduce wordpress login session timeout time?
  40. How to prefill WordPress registration with social details
  41. Check for correct username on custom login form
  42. Disallow user from editing their own profile information
  43. I can’t access my site via wp-admin
  44. ‘Password field is empty’ error when using autofill in Chrome
  45. Removing username from the ‘wordpress_logged_in’ cookie
  46. How to show ‘login error’ and ‘lost password’ on my template page?
  47. What is $interim_login?
  48. Custom login form
  49. How to prefill the username/password fields on the login page
  50. wp_signon returns user, but the user is not logged in
  51. Adding extra authentication field in login page
  52. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  53. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  54. wp_signon() does not authenticate user guidance needed
  55. What exactly is ReAuth?
  56. What are the differences between wp_users and wp_usermeta tables?
  57. Login members using web services
  58. Make my wordpress blog remember my login “forever”
  59. How to check in timber if user is loggedin?
  60. How do I change the language of only the login page?
  61. Disable WordPress 3.6 idle logout / login modal window / session expiration
  62. Stop WordPress from logging me out (need to keep me logged in)
  63. Woocommerce registration page [closed]
  64. How to disable autocomplete on the wp-login.php page
  65. Share login data/cookies between multiple installations
  66. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  67. How to pass users back and forth using session data?
  68. How do I change the logo on the login page?
  69. Why does WordPress hide the reset password key from the URL?
  70. Is it possible to sign in with user_email in WordPress?
  71. How to use current_user_can()?
  72. Avoid to load default WP styles in login screen
  73. WordPress registration message
  74. How to fake a WordPress login?
  75. how to display the wordpress login and register forms on a page?
  76. Does wp_logout_url() destroy a session? (Logging out question)
  77. How can I send a welcome email to a user AFTER they login for the first time?
  78. Can not login with correct username and password
  79. Website Visible only to Registered users
  80. How can i increase the login expiration length?
  81. How do I use add_action from a class method?
  82. How to remove the WordPress logo from login and register page?
  83. How can I add a custom script to footer of login page?
  84. Brute force attack?
  85. Customize wp_new_user_notification_email()
  86. Need to execute a cron job
  87. Password change when the user login first time
  88. Login email after registration never sent or received
  89. How can I create a separate blog that is private?
  90. How to keep always logged in development environment
  91. Add Confirm Password field in wp-login.php Password Reset page
  92. Integrate recaptcha and wp_signon – what is needed?
  93. Stop users from logging in from multiple locations
  94. stop redirection on /wp-admin call to /wp-login
  95. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  96. custom login page redirect to logged in user profile page
  97. Email address or username used to login in wordpress
  98. How do I check if a post is private?
  99. Front-end login: Redirect user to the post they had created
  100. Receiving “This content cannot be displayed in a frame” error on login page
Categories login Tags login, template-redirect, urls, wp-redirect
Use fetch_feed(); Totally Outside of WordPress?
trying to locate the correct file to edit my internal linking anchor tags

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