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 based on log-in status per JavaScript

To test if a user is logged in use is_user_logged_in().

To redirect a visitor use wp_redirect($location, $status = 302).

You can combine both:

<?php

$location = 'http://localhost';

if ( is_user_logged_in() )
    $location = admin_url( "https://wordpress.stackexchange.com/" );

wp_redirect( $location );

In JavaScript (sort of):

add_action( 'wp_footer', 'wpse_73701_button' );

function wpse_73701_button()
{
    $location = is_user_logged_in() ? admin_url( "https://wordpress.stackexchange.com/" ) : 'http://localhost';

    print "<button onclick='window.location.href=\"$location\"'>Click!</button>";
}

You could also just create JavaScript variables and use them in your scripts:

<script>
var isLoggedIn = <?php print is_user_logged_in() ? 'true' : 'false'; ?>;
var redirectTo = '<?php print is_user_logged_in() ? admin_url( "https://wordpress.stackexchange.com/" ) : 'http://localhost'; ?>';
</script>

Related Posts:

  1. Redirect User to Homepage if no other redirect is specified
  2. Redirect user to login before viewing custom post
  3. Redirect subscribers to last viewed page after log-in
  4. How to check User Role and redirect to specific page according to Role when login in WordPress?
  5. how redirect users to custom login page when “login to reply” is clicked? [duplicate]
  6. Specific Content on pages based on user
  7. Replacing the WordPress password validation
  8. WordPress auto login after registration not working
  9. Allow up to 5 Concurrent Login Sessions
  10. Why does is_user_logged_in() return false after redirect from another site?
  11. Pre-populate Username Field
  12. Redirect after User Activation [closed]
  13. Redirecting user after updating profile?
  14. How to keep track of user logins?
  15. Get user info outside WordPress
  16. How can I secure a WordPress blog using OpenID from a single provider?
  17. Is it possible to get a user with just the password field?
  18. Redirect after login based on user role (custom login page)
  19. Check for user meta data at Login
  20. My custom page template with is_user_logged_in() does not detect that I’m logged in
  21. Use phpbb user database for WordPress
  22. WordPress to use Drupal users’ credentials
  23. Redirect after users complete profile form
  24. Share user table from WP with Drupal
  25. How can I allow password reset based on logins containing the @ character?
  26. A way to count logged in users and display count?
  27. stop login if user_status equal zero
  28. Rule to redirect non logged in User to Custom Registration/login Page in .htaccess file
  29. How do i make my wordpress website private?
  30. Change the user_login at registration
  31. Max no of simultaneous active sessions for a single user
  32. When I try to login in wordpress it is showing “USER Doesn’t Exists”
  33. Should I encrypt the response that triggers an Ajax action? Is nonce sufficient?
  34. Redirect user after successful email change
  35. wordpress disable login for unverified user
  36. WP_User_List -> How to open a new edit-page in WordPress admin backend!
  37. Reset Password policy
  38. Rewrite Rules and Login Issue
  39. Is possible to allow user to login with different role?
  40. How to stop redirect if user is already on correct page (Using Case Switch)
  41. Use WordPress Login for a non-wordpress site
  42. WP not logging in at first time
  43. WordPress Login Customization for External Authentication
  44. Does wordpress support natively the concept of logging-in users? (not admins, but users of the website)
  45. Is there any action /filter hook I can use to disable login for some user role?
  46. Display video on homepage for users who have not logged in
  47. How can I do a url redirect to include a wordpress username?
  48. How do you manage your pages or functions that require logged-in users?
  49. Redirect logged on user to a specific page based on wp user role, page id
  50. How can i login with user’s password in WordPress being an admin?
  51. Delete a user from frontend
  52. Redirect users after login
  53. Redirect user to login if not logged in, on specific pages
  54. Check if user is logged in via JS? [duplicate]
  55. redirect user to their profile after log in
  56. Use members from 1 site on another one
  57. WordPress Redirect Specific User. Tired of Peter’s redirect Plugin its not working
  58. Can I edit the database to change a login?
  59. Redirect current user to their most recent custom post
  60. User(s) already exists show error please provide a valid username
  61. wp_generate_password sets password but can’t login using created password
  62. Janrain/Simple Modal under Redirected Domain
  63. add class to element if user is not logged in [closed]
  64. Is there a way to call via javascript if a user is logged-in on a static html file?
  65. Restricting wordpress login sessions for a web app
  66. How to remember which page the user was on before logout?
  67. How to connect wordpress user with my own APP user?
  68. How to change default username field after login
  69. The same session information for peer users on two different WordPress servers
  70. WP users cant reset password
  71. Check for empty username or password on login
  72. Redirect /member/ to /member/user
  73. $user_login is not working if author has only 1 post
  74. user and usermeta table not found
  75. Redirect current user
  76. custom login form, guide me
  77. Changing user_login ends current session
  78. Current User Seeing Another Logged In User Info
  79. Securely log in a user without a password using a link?
  80. Integrating Facebook Registration (and Login) on a WordPress page
  81. WordPress – Security Question at Login from User’s Meta Data
  82. Redirect user based on role when they try access a particular page
  83. determine active user browser at the same time
  84. How to track all users logged into a site?
  85. Cant edit profile from frontend
  86. How to authenticate/verify login credentials & check for user meta without logging in?
  87. Will users still be able to log in if I change host?
  88. redirect to my login page if not logged in
  89. How can I prevent certain custom roles from seeing other custom roles on the user list page?
  90. Creating User Profiles using author.php
  91. WordPress Password security related questions
  92. WordPress REST Api: update user
  93. Hackers tried user name with wrong case [closed]
  94. Custom form redirect after login
  95. How to set custom avatar for users?
  96. Custom redirect user after login based on metadata
  97. Preventing user enumeration: which logic is better?
  98. “the_author_meta(‘url’)” not working inside “href”
  99. REST API: wp_get_current_user not working on second call
  100. How to make Users sort in reverse order by default?
Categories users Tags javascript, login, redirect, users
Plugin options page – save two related options as one entry
issue displaying variations in custom template using WPeC 3.8.9.2

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