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:
- Action wp_login_failed not working if only one field is filled out
- Redirect wp-login
- Problem in auto login after registration
- Custom user roles are unable to login
- Login form- no feedback
- How can I redirect user after entering wrong password?
- Separate registration and login for different roles
- Disallow user from editing their own profile information
- Custom login form
- Prevent wp_login_form() from redirecting to wp-admin when there are errors
- How do I change the language of only the login page?
- Disable WordPress 3.6 idle logout / login modal window / session expiration
- Avoid to load default WP styles in login screen
- How to fake a WordPress login?
- Can not login with correct username and password
- How can I add a custom script to footer of login page?
- How to keep always logged in development environment
- I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
- custom login page redirect to logged in user profile page
- How to customise wp-login.php only for users who are setting a password for the first time?
- WordPress Login Footer URL
- Remove built in wordpress login and use only google auth
- Change Login Page for a Multisite Subsite
- Redirect user after login/registration globally
- Positioning the “Lost your password?” and “← Back to Site”
- send users logging in from wp-login.php directly to home page of site, rather than dashboard
- Redirect after login based on user role (custom login page)
- How to get login data (session) outside WordPress?
- Add class to input form in login form
- Allow access to a page for admins only
- Customizing login error messages
- Customizing the WordPress login form
- Deny a user role to log in after register
- how to restrict user login whenever if a user puts on hold by editing wp-login action?
- How do I force “users must be registered and logged in” on subsites?
- wp_get_referer not working properly after wp_redirect
- deny IPs from wp-login using .htaccess
- Login redirects and query strings
- Passing username to login screen
- Login error redirecting to wp-login page
- How can I prevent my custom form from redirecting to wp-login?
- Private page protected with username and password
- reset password link redirect to login page
- How do I replace “Username” in the WordPress login form?
- Custom Login iframe doesn’t work
- Password reset – Disabled for LDAP accounts
- How to integrate external user tables with WP?
- How to Get Logged-in to “Remote WP Site” from my local script (in Same Browser)?
- Replace dash with space in username on login
- Autologin only working the second time
- replace wp-login.php login forms via a hook & use custom forms with wp-login form validation
- Allow Users Only Edit Their Profile?
- How can I login as admin after redirect to custom login page
- Auto login between word press subdomain and a .net website
- Is possible to allow user to login with different role?
- Disabling standard registration login with username/email and password?
- How to change wordpress Log In text
- WordPress error on log out ‘Not Permitted’ and can’t log out
- Set Default User Role
- Is wp_login_form secure on a non secure page?
- Lock out all WordPress Administrators except two specific users
- Generate email on meta value update
- Is the login encrypted before it is sent? If so how to do I encrypt it the same way?
- Form Action submit over https
- Do more action after login successfully
- Is there anyway to get the inputted password string from the login form?
- Login and Forgot password in Lightbox
- Forcing frontend login with UI switch
- WordPress Login redirection according to user role
- Prevent display password on wp-login.php
- Prevent Subscriber Role to login
- Change default login auth
- wordpress login without password just email address (NO 2 factor authentication with email)
- WordPress SSL not working [closed]
- Where is the php file, that does the checks for login information?
- Hide Author page from others
- Can I protect a type of content site-wide with a single password?
- Cannot Get User id after login success in file wp_login.php
- Require re-login when logged-in user attempts to access restricted page
- This webpage has a redirect loop issue
- Alert Message through email or phone(Message)
- Moving from one host to another – cannot access the dashboard
- Temporally disable password to login with empty password?
- How are all users now set to inactive?
- woocommerce store login not working at first time
- Login Based on ip
- How do I change the language of the login page to Arabic?
- WordPress site login Redirect
- How do you implement a login feature on a WordPress site?
- URL Restrictions? Need only people who are logged in AND have a specific role (or roles) to access all pages for a site
- How to add custom authentication to wordpress login and register
- Login form does not store/remember/suggest users password
- Custom failed login error messages for users based on user role?
- Using is_user_logged_in() to lock down whole site
- Check for $ _POST fields in a POST method form
- Styling WordPress login page – Can I change the markup on the login page?
- Special link for no automatic login (no username and no password)
- Modify wp-login.php Labels Conditionally Based On Referring URL
- Can’t login with any account – No error message shown
- WordPress Login & Register works in localhost but don’t work on server