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

Restricting frontend acess based on user role otherwise redirect to login form

WP can’t redirect after you have already output HTML – so move your if/else to the top, before you call for the header, and only output the header & footer if your condition is met.

Also, instead of using current_user_can('role'), use current_user_can('capability') – i.e.

<?php if(
    current_user_can('activate_plugins') ||
    current_user_can('edit_a_defined_custom_post_type')
) {
    get_header(); ?>
    <div>Restricted content here</div><?php
    get_footer();
} else {
    wp_redirect(wp_login_url());
} ?>

(According to the Codex, “While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.”)

Related Posts:

  1. Action wp_login_failed not working if only one field is filled out
  2. Redirect wp-login
  3. Problem in auto login after registration
  4. Custom user roles are unable to login
  5. Login form- no feedback
  6. How can I redirect user after entering wrong password?
  7. Separate registration and login for different roles
  8. Disallow user from editing their own profile information
  9. Custom login form
  10. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  11. How do I change the language of only the login page?
  12. Disable WordPress 3.6 idle logout / login modal window / session expiration
  13. Avoid to load default WP styles in login screen
  14. How to fake a WordPress login?
  15. Can not login with correct username and password
  16. How can I add a custom script to footer of login page?
  17. How to keep always logged in development environment
  18. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  19. custom login page redirect to logged in user profile page
  20. How to customise wp-login.php only for users who are setting a password for the first time?
  21. WordPress Login Footer URL
  22. Remove built in wordpress login and use only google auth
  23. Change Login Page for a Multisite Subsite
  24. Redirect user after login/registration globally
  25. Positioning the “Lost your password?” and “← Back to Site”
  26. send users logging in from wp-login.php directly to home page of site, rather than dashboard
  27. Redirect after login based on user role (custom login page)
  28. How to get login data (session) outside WordPress?
  29. Add class to input form in login form
  30. Allow access to a page for admins only
  31. Customizing login error messages
  32. Customizing the WordPress login form
  33. Deny a user role to log in after register
  34. how to restrict user login whenever if a user puts on hold by editing wp-login action?
  35. How do I force “users must be registered and logged in” on subsites?
  36. wp_get_referer not working properly after wp_redirect
  37. deny IPs from wp-login using .htaccess
  38. Login redirects and query strings
  39. Passing username to login screen
  40. Login error redirecting to wp-login page
  41. How can I prevent my custom form from redirecting to wp-login?
  42. Private page protected with username and password
  43. reset password link redirect to login page
  44. How do I replace “Username” in the WordPress login form?
  45. Custom Login iframe doesn’t work
  46. Password reset – Disabled for LDAP accounts
  47. How to integrate external user tables with WP?
  48. How to Get Logged-in to “Remote WP Site” from my local script (in Same Browser)?
  49. Replace dash with space in username on login
  50. Autologin only working the second time
  51. replace wp-login.php login forms via a hook & use custom forms with wp-login form validation
  52. Allow Users Only Edit Their Profile?
  53. How can I login as admin after redirect to custom login page
  54. Auto login between word press subdomain and a .net website
  55. Is possible to allow user to login with different role?
  56. Disabling standard registration login with username/email and password?
  57. How to change wordpress Log In text
  58. WordPress error on log out ‘Not Permitted’ and can’t log out
  59. Set Default User Role
  60. Is wp_login_form secure on a non secure page?
  61. Lock out all WordPress Administrators except two specific users
  62. Generate email on meta value update
  63. Is the login encrypted before it is sent? If so how to do I encrypt it the same way?
  64. Form Action submit over https
  65. Do more action after login successfully
  66. Is there anyway to get the inputted password string from the login form?
  67. Login and Forgot password in Lightbox
  68. Forcing frontend login with UI switch
  69. WordPress Login redirection according to user role
  70. Prevent display password on wp-login.php
  71. Prevent Subscriber Role to login
  72. Change default login auth
  73. wordpress login without password just email address (NO 2 factor authentication with email)
  74. WordPress SSL not working [closed]
  75. Where is the php file, that does the checks for login information?
  76. Hide Author page from others
  77. Can I protect a type of content site-wide with a single password?
  78. Cannot Get User id after login success in file wp_login.php
  79. Require re-login when logged-in user attempts to access restricted page
  80. This webpage has a redirect loop issue
  81. Alert Message through email or phone(Message)
  82. Moving from one host to another – cannot access the dashboard
  83. Temporally disable password to login with empty password?
  84. How are all users now set to inactive?
  85. woocommerce store login not working at first time
  86. Login Based on ip
  87. How do I change the language of the login page to Arabic?
  88. WordPress site login Redirect
  89. How do you implement a login feature on a WordPress site?
  90. URL Restrictions? Need only people who are logged in AND have a specific role (or roles) to access all pages for a site
  91. How to add custom authentication to wordpress login and register
  92. Login form does not store/remember/suggest users password
  93. Custom failed login error messages for users based on user role?
  94. Using is_user_logged_in() to lock down whole site
  95. Check for $ _POST fields in a POST method form
  96. Styling WordPress login page – Can I change the markup on the login page?
  97. Special link for no automatic login (no username and no password)
  98. Modify wp-login.php Labels Conditionally Based On Referring URL
  99. Can’t login with any account – No error message shown
  100. WordPress Login & Register works in localhost but don’t work on server
Categories login Tags login, user-roles, wp-login-form, wp-redirect
How to get customfield value by woocommerce product ID
How can I move a custom taxonomy and its data from one post type to another?

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