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 User to login page

Here is from my previous project. The function is hooked into the template_redirect action. Inside the function there are 2 conditionals. The first is the the one that will redirect logged in user away from the login page. And the other one is to redirect non logged in user to the login page.

// add a redirect for logged out user
add_action('template_redirect', 'redirect_user');

function redirect_user(){
    global $current_user;
    if (is_page('login-page-for-non-logged-in-user') && is_user_logged_in()){
        $return_url = get_bloginfo('url');
        wp_redirect($return_url);
    }
    // this part is untested
    if (!is_page('login-page-for-non-logged-in-user') && !is_user_logged_in()){
        $return_url = get_bloginfo('url');
        wp_redirect($return_url);
    }
    // end of untested part
}

Related Posts:

  1. Redirect user after login/registration globally
  2. Password change when the user login first time
  3. Restrict Access to wp-login.php
  4. wp-login.php — redirect logged in users to custom URL
  5. Redirect to login page
  6. How do I redirect a user when he is logged in after clicking on a menu
  7. How to auto login after registration? [duplicate]
  8. How to modify the Register link in the login page?
  9. How to redirect to home page after registration?
  10. Redirect Logged In User if page is wp-login.php and $_Get[‘level’] = X
  11. Redirect user to previous page after signup from custom form
  12. Login/Register redirect user with message
  13. Why I am not able to direct user to my blog section without login?
  14. Trying to re-direct users to specific page based on an ACF variable
  15. Keep the old referrer after failed login
  16. How to redirect new registrars to a custom registration page instead of WP default registration page?
  17. How to disable login and registration pages?
  18. Redirect after registration and auto login is not working
  19. Redirect user to original url after login?
  20. wp_redirect() function is not working
  21. Redirect page URL to home URL without using a plugin
  22. Login redirect to previous page
  23. Why is wp_redirect() preferable to a standard PHP header redirect?
  24. Redirect to referring page after logging in
  25. check first time login only
  26. How to change the default logout link on WordPress Admin
  27. How to redirect a sucessful registration to a page template?
  28. WordPress HTTPS redirect loop
  29. How to redirect after login, the working way?
  30. Custom login form redirect to external site
  31. handling login/logout redirects
  32. Redirect to custom url when registration fails?
  33. Using `auth_redirect` : keeps asking me to login even when I’m logged in
  34. Redirect to custom URL after registering from a page with registration form
  35. Redirect users after first login
  36. Stop unwanted WP redirection to new url
  37. Redirect to current page after wordpress login
  38. Redirect to “All Posts” after post update or publish in Block Editor
  39. Redirecting old site links to new site
  40. Temporary redirect prevents getting $_POST array
  41. Creating intro page in wordpress
  42. URL Redirect and Bulk Actions in wp_list_table
  43. How to redirect only if page doesn’t exists
  44. Headers already sent – WordPress core
  45. How to redirect action=register link on the lostpassword page to a different link?
  46. You do not have sufficient permissions to access this page
  47. Redirect user after logout
  48. how to do logout redirect to current url
  49. Perform a redirect after user action
  50. How To Only Allow Users To View Their Own Buddypress Profiles? [closed]
  51. How to redirect user to specific page based on the input password
  52. Custom Registration page – prevent redirect to wp-login.php?action=register
  53. Firing a function AFTER redirect
  54. header and wp_redirect not working. cannot modify header information warning
  55. Code to create a redirection after login?
  56. How do I redirect /search/ to ?s=
  57. Changing “Lost Password Email Link” to custom password reset page
  58. WordPress Global Redirect
  59. Why does wp_redirect strip out %0A (url encoded new line character) and how do I make it stop?
  60. How to redirect user to a specific page based on username?
  61. Redirect if $_Server[‘REQUEST_URI’] is /wp-login.php?checkemail=registered
  62. Redirect after password update
  63. Redirect the non-www version of the site to the www
  64. How to redirect user after login to a specific page?
  65. How to use Regex to add to a url?
  66. Redirect first category archive page to normal page
  67. Stuck In a Redirect Loop
  68. Redirect when accessing /login when logged in
  69. How to redirect after login getting a variable from url (for example with the language)
  70. How to set 301 redirection after moving WordPress blog?
  71. If User is NOT Logged in and Page ID is not 6 – Redirect to Login
  72. Redirect to Current Page after Login
  73. How to include error message on login
  74. Login redirect problem
  75. Can I change default registration link (without htaccess)?
  76. Front end user creation form! How do you keep the user on the same page in event of error?
  77. How to redirect on login to a specific page if a specific meta user is empty
  78. Adding rewrite rule dynamically for search results redirecting to 404 URL
  79. Force WordPress to load from site’s hostname
  80. How to redirect if a background request is still being executed
  81. redirect Login problems
  82. Redirect custom post type from one domain to another domain
  83. How To Redirect /url.html to /url?
  84. I get redirected too many times only in wp-admin?
  85. How can I force the user to log in, even if they’re already authenticated?
  86. User is logged in after Stripe payment, then redirected, but is then logged out again
  87. Get query parameters from external url and make a redirection
  88. Redirect users after register (first login) and force submit post
  89. Login Redirect – Multiple Scenarios
  90. wordpress_redirect returns different headers for HEAD and GET requests
  91. Remove #wpcf7-f2450-o1 with Contact form 7 redirect [closed]
  92. WooCommerce – Redirect to a product after login
  93. How to pass a message using template_redirect
  94. Usage of the login_redirect filter
  95. Show different page for first time user
  96. Redirect user to original url after login?
  97. Redirect outside WP after login
  98. Prevent users from going to wordpress profile after login
  99. HTML Redirect to WP pages
  100. Why does the $_GET parameter ?search forward the front-page to the archive/blog page
Categories redirect Tags login, redirect, user-registration, wp-redirect
i’m trying to get all my media query attachments via ajax in wordpress
How to test if my server is vulnerable to the ShellShock bug?

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