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 my login page if not logged in

This is how I managed a similar situation – to allow logged in authors access to a topics pitch form – using these steps (I’ll add my code examples below):

  1. I put the form in a Hidden Div that was visible only to logged in users, if a site visitor was not logged in, instead they see a “login or apply” message – in my case the “login” displayed the login form in an overlay (fancybox style) but you could also link it directly to the wp-login.php page and then add a bit more code to redirect them back to where they were….in my case the “apply” link leads to a Page where they can apply to register as an authorized user, but they’re not automatically approved so after applying they go to a “thanks we’ll be in touch” page.

  2. I created the shortcode to display only the login form, not the wp-login.php page and show it in an overlay so my site visitors didn’t leave the page they were on.

Here’s the code I used:

  1. Hiding a form from non-logged in users – 2 possible methods:

    a) the easy way is to use a forms plugin that has the option to hide/show a form base on logged in status, I use GravityForms and this is easy to accomplish and allows for an HTML notice (but not PHP) to show in place of the form if a user is not logged in, the “no PHP” rule is why I created a shortcode:

    b) IF you’re manually creating the review form OR using a plugin that doesn’t have the option to hide/show the form based on logged-in status, then simply wrap your form in some code like this:

     if ( is_user_logged_in() ) {
     display your form code or shortcode here;
     } else {
     echo 'You must be logged in to access our pitch form, please <a class="fancybox-inline" href="#fancyboxID-2">Login</a> or <a href="https://(mysite-redacted-for-privacy)/Apply">Apply</a>';
     echo '<div style="display:none;"><div id="fancyboxID-2">';
     echo do_shortcode('[displayLogin]');
     echo '</div></div>';
     }
    

Note the “echo do_shortcode” – that’s the shortcode below to display just the login form, not the login page. If someone is logged in, they’ll see your form….if not, they’ll see a link to “login” or “apply”, if they click on login, it will show the login form in an overlay, fancybox style (I use Easy FancyBox plugin).

  1. Shortcode to display login form in the overlay:

    add_shortcode( 'displayLogin', 'display_loginForm' );
    function display_loginForm() {
    $args = array(
    'echo'           => false,
    'value_remember'       => true,);
    $loginForm = wp_login_form($args);
    
    $html="";
    $html.= '<div id="popupLogin" class="bordered rounded">';
    $html.= $loginForm;
    $html.='</div>';
    $html.='<span class="popupLoginFPWD"><a href="https://(mysite-redacted-for-privacy)/wp-login.php?action=lostpassword" target="_self">Lost Your Password?</a></span>';
    
    return $html;
    }
    

NOTE: I default the “remember me” checkbox to true because all my authors complained about being logged out, so I added code to extend their login duration to a year if they check the box, you may not want to do that, just change the value for “value_remember” to false if you want the normal 2-day login duration.

I went with this method because redirecting users after logging in can be tricky, the default WP behavior is generally to take users to the WP Admin back end with whatever menu items their role allows, for the lowest role (subscriber) it usually only gives them their own profile page.

You certainly CAN redirect them elsewhere – such as back to your reviews page – but that’s not always the best user experience, especially if they didn’t want to go back to that page but had decided instead to maybe edit their profile. I would give thought to all considerations of handling this – for example, you might want to create a new class of User – say “reviewer” for example – and that class never gets to the back-end. For those who already have a higher role, you might offer an interstitial page or message offering them the option of going to the reviews page OR the back end.

Good luck with this! I’ll be interested in whatever final solutions you land on. 🙂

Related Posts:

  1. Replacing the WordPress password validation
  2. WordPress auto login after registration not working
  3. Allow up to 5 Concurrent Login Sessions
  4. Why does is_user_logged_in() return false after redirect from another site?
  5. Pre-populate Username Field
  6. How to keep track of user logins?
  7. Get user info outside WordPress
  8. How can I secure a WordPress blog using OpenID from a single provider?
  9. Is it possible to get a user with just the password field?
  10. Redirect after login based on user role (custom login page)
  11. Check for user meta data at Login
  12. Redirect User to Homepage if no other redirect is specified
  13. My custom page template with is_user_logged_in() does not detect that I’m logged in
  14. Use phpbb user database for WordPress
  15. WordPress to use Drupal users’ credentials
  16. Share user table from WP with Drupal
  17. How can I allow password reset based on logins containing the @ character?
  18. A way to count logged in users and display count?
  19. stop login if user_status equal zero
  20. How do i make my wordpress website private?
  21. Redirect user to login before viewing custom post
  22. Change the user_login at registration
  23. Max no of simultaneous active sessions for a single user
  24. When I try to login in wordpress it is showing “USER Doesn’t Exists”
  25. Should I encrypt the response that triggers an Ajax action? Is nonce sufficient?
  26. Redirect subscribers to last viewed page after log-in
  27. wordpress disable login for unverified user
  28. Reset Password policy
  29. Rewrite Rules and Login Issue
  30. Is possible to allow user to login with different role?
  31. Use WordPress Login for a non-wordpress site
  32. WordPress Login Customization for External Authentication
  33. Does wordpress support natively the concept of logging-in users? (not admins, but users of the website)
  34. Is there any action /filter hook I can use to disable login for some user role?
  35. How to check User Role and redirect to specific page according to Role when login in WordPress?
  36. Display video on homepage for users who have not logged in
  37. How do you manage your pages or functions that require logged-in users?
  38. How can i login with user’s password in WordPress being an admin?
  39. Delete a user from frontend
  40. Check if user is logged in via JS? [duplicate]
  41. Use members from 1 site on another one
  42. Can I edit the database to change a login?
  43. how redirect users to custom login page when “login to reply” is clicked? [duplicate]
  44. User(s) already exists show error please provide a valid username
  45. wp_generate_password sets password but can’t login using created password
  46. add class to element if user is not logged in [closed]
  47. Is there a way to call via javascript if a user is logged-in on a static html file?
  48. Restricting wordpress login sessions for a web app
  49. How to connect wordpress user with my own APP user?
  50. How to change default username field after login
  51. The same session information for peer users on two different WordPress servers
  52. WP users cant reset password
  53. Check for empty username or password on login
  54. $user_login is not working if author has only 1 post
  55. Specific Content on pages based on user
  56. user and usermeta table not found
  57. custom login form, guide me
  58. Changing user_login ends current session
  59. Current User Seeing Another Logged In User Info
  60. Redirect based on log-in status per JavaScript
  61. Securely log in a user without a password using a link?
  62. Integrating Facebook Registration (and Login) on a WordPress page
  63. WordPress – Security Question at Login from User’s Meta Data
  64. determine active user browser at the same time
  65. How to track all users logged into a site?
  66. Cant edit profile from frontend
  67. How to authenticate/verify login credentials & check for user meta without logging in?
  68. Will users still be able to log in if I change host?
  69. If the current user is an administrator or editor
  70. Editor can create any new user except administrator
  71. How do I add a field on the Users profile? For example, country, age etc
  72. How do I display logged-in username IF logged-in?
  73. How to allow an user role to create a new user under a role which lower than his level only?
  74. user_login vs. user_nicename
  75. How to programatically change username (user_login)?
  76. Change the Author Slug from Username to Nickname
  77. Remove Ability for Other Users to View Administrator in User List?
  78. Difference between update_user_meta and update_user_option
  79. Make display name unique
  80. Make WooCommerce pages accessible for logged in users only
  81. Find out if logged in user is not subscriber
  82. WordPress usermeta scaling for thousands of users
  83. How to get WordPress Username in Array format
  84. Display user registration date
  85. Is there a is_user_logged_in() for multisite?
  86. Get multiple roles with get_users
  87. get_user_meta() doesn’t include user email?
  88. Confirmation required on email change
  89. How to Merge Two Authors Into One?
  90. Whats the best way to share user data across multiple WordPress websites?
  91. get_current_user_id() returns 0?
  92. How to get userid at wp_logout action hook?
  93. Groups of capabilities: users with multiple roles?
  94. Is there a way to merge two users?
  95. User-edit role setting distinct from wp_capabilities? [closed]
  96. List users by last name in WP_User_Query
  97. What’s the difference between the capability remove_users and delete_users?
  98. How to restrict access to uploaded files?
  99. Automatically delete inactive users after 2 months
  100. How to change user_login with wp-cli?
Categories users Tags login, users
Options of select field in a custom divi module ignore sorting
How can I do automatic redirects to canonical URLs as defined by Yoast SEO?

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