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 protect content from public users and limit access to certain roles in WordPress 2023?
  41. How to restrict content on a subdomain to users that registers on main site?
  42. allow editors to edit menus?
  43. How to change a user’s role?
  44. Allow member to have access to custom post type only. Permission to only edit their own posts
  45. Is there way to rename user role name without plugin?
  46. How to create a clone role in wordpress
  47. How to add a Capability to a User Role?
  48. How can I get a list of users by their role?
  49. How to get role of user
  50. How to allow editor to edit privacy page / settings only?
  51. How do I make a draft post accessible to everyone?
  52. Reset default roles and capabilities
  53. How to restrict attachment download to a specific user?
  54. How do I programmatically set default role for new users?
  55. add_role() run only once?
  56. What do unfiltered_html and unfiltered_upload actually filter?
  57. Temporary capability for current_user_can()
  58. Allowing users to edit only their page and nobody else’s
  59. Create custom user role (client) that can create another custom user role (employee) of that client
  60. Is it possible to add new user Roles?
  61. Allow roles below admin to add subscribers only
  62. Troubleshooting a “You do not have sufficient permissions to access this page” error
  63. Restrict admin access to certain pages for certain users
  64. How can I hide a category from Contributors in the edit/add new post screen?
  65. Disallowing Users of a Custom Role from Deleting or Adding Administrators?
  66. How to assign specific users the capability to edit specific pages / posts / custom post types
  67. Hide Admin Menu for Specific User ID who has administrator Role
  68. How to get all capabilities of an existing user role
  69. Make A WordPress Page Accessible To Admins Only, Redirect Other User Roles
  70. Limit access to posts/pages by user roles
  71. Temporarily give ‘manage_options’ capability
  72. REST API, get user role?
  73. How to force listing contributors in authors dropdown menu
  74. Only allow administrators and editors to access wp-admin
  75. Hide specific admin users’ posts
  76. Restrict users on multisite WordPress install
  77. Allow authors to edit only certain users
  78. How do I allow a specific Role in WordPress 3+ to only see certain plugins?
  79. How to update role capabilities
  80. Allow editors access to added plugins
  81. How to filter the role selector?
  82. How to loop through each user id?
  83. Code needed to only show users own posts in a multi-user account
  84. How can I change a user role upon visiting a page?
  85. How to let the Contributor role preview unpublished posts?
  86. How to restrict a page [without plugin]
  87. Change role after the user has read all the posts in a category
  88. Do custom user roles have any default capabilities?
  89. Filter available WooCommerce payment gateways by role [closed]
  90. Ordering users of a specific role by last name
  91. How to programmatically add a user to a role?
  92. Restrict Author to pick from media library, but not upload media
  93. Can i copy administrator role to another user?
  94. Assign Subscriber capabilities to a custom user role
  95. How to remove “Super Admin” from All Users for those that are not a “Super Admin”?
  96. How to change role of all users with a specific role to another role?
  97. Allow user to Publish, but not Edit or Delete
  98. Roles at registration (classipress)
  99. How to allow registered users to change their user role through frontend?
  100. how to make author to write comment on only his own posts?
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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