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 not working to redirect only logged out users

You need to wrap the first three || conditions in additional parentheses so they are evaluated first BEFORE the last && condition:

Psuedocode example:

    here                               here 
     ↓                                  ↓  
if ( (condition || condtion || condition) && ! condition ) {
    //do business logic...
}

This now means that:

If any ONE of is_paged() OR is_author() OR is_single() IS true THEN evaluate the second condition ! is_user_logged_in()

In your original code:

With no additional parentheses wrapping the first three conditions, IF any of those four conditions return true then you would redirect your user whether they are logged in or not.

Final code with additional parentheses:

add_action( 'template_redirect', 'redirect_to_specific_page' );

function redirect_to_specific_page() {   
    if ( (is_paged() || is_author() || is_single()) && ! is_user_logged_in() ) {
        wp_redirect( 'http://www.exampleblog.net/members-only/', 301 ); exit; 
    } 
}

Related Posts:

  1. wp_enqueue_script was called incorrectly
  2. Masking logout URL
  3. How can I tell if I’m on a login page? [duplicate]
  4. Is it possible to use a forgot password url filter?
  5. redirect wp-login.php to another page
  6. Login using the password from protected pages
  7. How do I redirect upon login a specific user based on role?
  8. Logging in redirects to correct page but shows logged out content until forced refresh
  9. Redirect if not logged in?
  10. Remove default user registration, login and subscriber profiles
  11. Redirect a list of URLs to another URL, using functions.php
  12. Members only site – still need the lost password page accessible
  13. Php function 301 redirect
  14. How to change login labels
  15. How can I get my Script to work on the Login page?
  16. Login Redirect if Logged in from Specific Page
  17. Unable to login using username
  18. When a user logs in, how can they view the website instead of the admin menu?
  19. WordPress PHP Conflicting User Sessions
  20. Get User Login Data (date, time… )
  21. Using wp_redirect and .htaccess to re-route searches (and pass along the remaining GET vars)
  22. Registration Hooks don’t appear to be working
  23. Cannot modify header information – headers already sent (wp_redirect in functions.php)
  24. If user is logged-in display/hide something
  25. Secondary Menu and Logged In Users
  26. Redirect website domain (including all pages) to external URL after Popup message (few seconds)
  27. Prevent WordPress Automatic Logout
  28. error at login page in wordpress
  29. Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
  30. Logout Redirect and also WP-login.php Redirect
  31. How to add array [closed]
  32. Remove attachment page for audio media type only
  33. loginout function customization
  34. Add Login and logout buttons to top menu bar
  35. Show errormessages on wrong username/password on custom loginform?
  36. WordPress login set cookie that survive browser exit (wp_signon function)
  37. This code is supposed to only allow user to be authenticated if accountVerified is equal to 1, but it still allows user to be authenticated otherwise
  38. Prevent wp_signon redirect on failed login for ajax login
  39. How to redirect Subscribers on login to specific page, when logging in from a Page
  40. How to replace ACTION url from original wordpress login form?
  41. Unable to login after registration
  42. Allow logged in user to view a Page, else send to login screen and then redirect back to Page
  43. Redirect after login to current URL
  44. Login functions
  45. WordPress permanently logging users out
  46. A blank page is shown after I add a Function
  47. How to request login for user but not for bots
  48. is_user_logged_in() isn’t working
  49. update_user_option not working as expected
  50. “Headers already sent” while trying to add a CSS file to my login page?
  51. Making an under maintenance page (without using plugins)
  52. Simple way to make most of my site private
  53. wp_login_form display no styled form
  54. Redirecting after login except for a specific page
  55. Missing feature image link function
  56. What’s the difference between home_url() and site_url()
  57. Remove “Category:”, “Tag:”, “Author:” from the_archive_title
  58. get_template_directory_uri pointing to parent theme not child theme
  59. How to customize the_archive_title()?
  60. remove empty paragraphs from the_content?
  61. What is the “with_front” rewrite key?
  62. Why use if function_exists?
  63. How to override parent functions in child themes?
  64. Add multiple custom fields to the general settings page
  65. Ajax call always returns 0
  66. 400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
  67. How long does a deprecated function live in core?
  68. Solution to render Shortcodes in Admin Editor
  69. How to add a data attribute to a WordPress menu item
  70. What’s the difference between esc_html, esc_attr, esc_html_e, and so on?
  71. remove_action on after_setup_theme not working from child theme
  72. plugins_url vs plugin_dir_url
  73. Remove type attribute from script and style tags added by WordPress
  74. How to run a function every 5 minutes?
  75. Best way of passing PHP variable between partials?
  76. Upload Multiple Files With media_handle_upload
  77. How to display custom field in woocommerce orders in admin panel?
  78. Adding fields to the “Add New User” screen in the dashboard
  79. Issues with title-tag and document_title_parts
  80. How do I get the current edit page ID in the admin?
  81. How to check if a user exists by a given id
  82. Why isn’t is_page working when I put it in the functions.php file?
  83. Add tags to the section via functions.php
  84. Add image size if page template
  85. How to create a custom order status in woocommerce!
  86. Remove Actions/Filters added via Anonymous Functions
  87. Adding a second email address to a completed order in WooCommerce [closed]
  88. How to load parent_theme functions.php before child_theme?
  89. How to load scripts/styles specific for a page
  90. Programatically add options to “add new” custom field dropdown
  91. Is there any global functions.php file which works for any theme?
  92. Excluding iPad from wp_is_mobile
  93. Is there a is_user_logged_in() for multisite?
  94. When should you, and when should you not, use wp_list_pluck()?
  95. Get the ID of the page a menu item links to?
  96. Add container to nav_menu sub menu
  97. Difference between the_permalink() and get_permalink() function
  98. What’s the difference between WordPress random_int() and PHP built-in function random_int()?
  99. Child theme – Overriding ‘require_once’ in functions.php
  100. Link to user’s profile settings page?
Categories functions Tags functions, login, wp-redirect
Empty tax_query array returns an empty array
Plugin not saving values when placed in a folder within /wp-content/plugins

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