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

Hide front-end from every logged out user and redirect them to the default login page

One way of doing this would be to add the code below to the top of your template file(s):

<?php // redirect to login page if user is not logged in 
if ( !is_user_logged_in() ) :
    header('Location: /wp-login.php');
    exit();
endif;
?>

There are some subtleties around redirecting in PHP that you may want to consider, but for me the above code works.

Related Posts:

  1. User restricted only show posts assigned to current user
  2. How to restrict an admin page, if the user is not superadmin?
  3. wp_update_user not updating
  4. Is WordPress’ is_user_logged_in() secure?
  5. Temporarily disable user role login and replace with message
  6. how to add custom user capabilities using add_user_meta or something else?
  7. Is there a way to set the user Role based on email domain
  8. pre_get_posts Remove posts based on meta value with ‘post__not_in’
  9. What is the difference between “create_users” and “add_users” capabilities?
  10. Access on specific pages in wordpress for a specific user
  11. How to ‘unpublish’ or ‘hide’ posts when user role changes?
  12. Hide Specific User Page
  13. Let new user role to ‘edit_others_posts’ of other user role, not of its own type
  14. How can I have different groups of editors only allowed to edit certain parent+subpages?
  15. Hide everything on site for visitors except specific page IDs
  16. How do I remove the Other Roles field (from User Role Editor plugin) in wp-admin/user-new.php
  17. How to redirect specific post type with user role
  18. wordpress editor role remove all but ‘menus’ in appearance menu
  19. Parent User and Child User – relate users
  20. How to create user specific pages (not user role!)?
  21. Assigning certain authors to specific editors
  22. Restrict Access in Admin Panel
  23. Limit a user to have access to only specified pages?
  24. Plugin creation – how to add user rights?
  25. Restrict custom post content to specific user
  26. Need to block user role from accessing bbPress all together
  27. How to create a front facing user sign up, log in and profile pages like FoodGawker.Com [closed]
  28. Infinite redirects at front end if logged in user is not an Admin (Toolset Access)
  29. Is there a way to allow users with “Subscriber” user role to access media library in backend and frontend
  30. How to make WP page accessile only to specific user roles
  31. Custom user roles are unable to login
  32. grant multiple roles access to specific admin menu item
  33. How to show private pages based on a user’s role?
  34. Check what capabilitie(s) an action requires
  35. Redirect non-admin after login, and in url – /admin
  36. Is it possible to restrict a specific user to edit a specific custom post.
  37. Assign specific editor with custom user meta “A” to all authors with custom user meta “A” and exclude all other author access
  38. New folder and file permissions are not correct
  39. How to change user role setting in members plugin so that user can only edit his own post?
  40. How to restrict content on a subdomain to users that registers on main site?
  41. What do unfiltered_html and unfiltered_upload actually filter?
  42. Temporary capability for current_user_can()
  43. Code needed to only show users own posts in a multi-user account
  44. Restrict Author to pick from media library, but not upload media
  45. Remove Ability for Administrators to Delete Administrators
  46. Order users by custom user meta
  47. Auto Delete Users (auto_delete_users)
  48. New Roles and Capabilities in WordPress
  49. Issue with front-end ajax, getting a 302 redirect when accessing wp-admin/admin-ajax.php
  50. WordPress Super Admin
  51. Can’t manage to make translate_user_role() work
  52. how to assign more permission to wordpress author
  53. Limit users by custom taxonomy and user roles
  54. Restricting frontend acess based on user role otherwise redirect to login form
  55. Managing Users and Creating Groups [closed]
  56. Customizer – Allow user editor to change website title and tagline
  57. Query users which have same dynamically generated roles as the current user
  58. change role of wordpress users when they edit profile
  59. How to restrict CPT post’s fronted view only for specific user roles?
  60. Change post status based on user role
  61. Display User Role Next To Comment
  62. Allow an editor to change the author
  63. Protect sequence of pages with same password for each of two groups of users
  64. How to give remove role editing permission from user
  65. How can I allow an User to publish only 5 posts per month?
  66. How to have kind of a technical writer role in wp website
  67. Where are $current_user->allcaps set?
  68. How to make the Newsletter plugin visible to users with author privileges?
  69. WP welcome email depending on user role
  70. How can I promote a user to a network administrator?
  71. How to set where user is redirected to after logging in at wp-login?
  72. read_private_pages capability not working for new role
  73. How to change footer or for different kinds of users in wordpress?
  74. Grant a person permission to moderate all comments on a blog without giving them the ability to edit other peoples post
  75. How can I programattically hide all admin notices for everyone except admin users
  76. What Capability is required to let a role RUN code in Edit Theme?
  77. Restrict the list of parent pages to only those which are created by current user
  78. Can I Create a Second Admin Level User Role?
  79. Retrieve user roles but exclude default roles
  80. Disabled delete_others_posts if post is from admin
  81. How to hide Add new Term area for taxonomy terms for specific role (Client)
  82. show the author’s role along with the name in the single.php
  83. How to create different views for different user roles?
  84. How to Enable registration email for a custom user role?
  85. How can I add the ability for a user to add/edit administrators?
  86. add user role selection to registration form [duplicate]
  87. Front end login and page restriction
  88. Grant access to admin menu?
  89. I want this code to work only for Authors AND Contributors
  90. You do not have permission to access this document on form submit
  91. Disable Woocommerce checkout based on user role [closed]
  92. Remove_role not working, gives “Call to undefined method stdClass::remove_role()”
  93. Adding a new role with administrator’s capabilities
  94. Front-end ajax problem all users and guests getting a 302 redirect when accessing wp-admin/admin-ajax.php
  95. How to assign a custom gender to a user role?
  96. assign roles to users in custom drop down in signup form
  97. Hide block of HTML if the users role is a certain role
  98. How might I enable a user to view Draft pages from a different Author, without the ability to edit?
  99. Display users by role
  100. disable adding new tags on the edit and add post
Categories user-roles Tags content-restriction, user-access, user-roles, wp-login-form
Search in 1) Post, 2) Products (woocommerce) and 3) Custom post type or Taxonomy type on a single page
Add function after the_content

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