Elaborate plugins exist to restrict page access based on a user role or other settings. However, I guess the following code is as simple as it’s gets for checking a user role.
<?php
global $current_user;
if ( is_page( 'some-page' ) && in_array( 'some-role', $current_user->roles ) ) {
// Show the page
} else {
// Howdy, stranger! Nothing to see here.
}
?>
Note: this code could go into your theme’s template files such as index.php
or page.php
. Be aware that switching themes would remove the user role access restriction. Solution? Move this functionality to a plugin. Update: G. M. provided a plugin version in his answer.
Related Posts:
- Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
- Is there any way to rename or hide wp-login.php?
- I can’t access my site via wp-admin
- ‘Password field is empty’ error when using autofill in Chrome
- How to disable autocomplete on the wp-login.php page
- Website Visible only to Registered users
- How do I use add_action from a class method?
- I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
- How do I check if a post is private?
- Action wp_login_failed not working if only one field is filled out
- Display last login time
- Intentionally Force Failed Login WordPress
- What hooks should I use for pre-login and pre-registration actions?
- moving server can not login
- how to update current logged user username
- Custom Reset Password & Forgot Password Front End Forms
- Admin user getting message ‘You need a higher level of permission’
- How to implement Google reCaptcha without installing a plugin?
- Problem with logging in WP users automatically
- advance membership managment
- Logins through alias
- Remove WordPress Login Animation
- Redirect users to a front end wp login page
- Redirecting or displaying a message on first login
- Deny a user role to log in after register
- How to create a private login page for admin.?
- wp_get_referer not working properly after wp_redirect
- Login Button CSS
- Force Users to Login – loop problem
- Different homepage for logged in users
- Is there a better way than checking user is logged in to show or hide adminbar?
- How do I replace “Username” in the WordPress login form?
- How can I do a URL redirection when an user uses wrong login details?
- Show errors on custom login form [duplicate]
- Change login_message using title
- Changing WP login credential [closed]
- Index page and random posts needs a forced refresh in order to show new content
- How to change wordpress Log In text
- Why would the login page reload indefinitely?
- Can I (and should I) change the login-URL from functions.php?
- Lock out all WordPress Administrators except two specific users
- Removing wordpress cookie from non-wordpress site
- Why is wp-login trying to send an email?
- Secure login on wordpress [closed]
- Replacing default display name to login name
- Why does is_user_logged_in() return false after I change user password on the front end?
- How to generate “WP_Error” Object for user login?
- ?login=failed only attached to URL under certain circumstances
- How can I password protect a WordPress site without requiring users to log in?
- Can’t login to my own website
- Can I Get User ID at Login?
- WordPress not logged in locally with correct username and password
- my wp-login page doesn’t load [closed]
- Requiring login for specific pages
- login trouble WordPress can change IDs created by hoster site
- I want login using email not username wordpress front end
- login/logout for only one page
- WordPress registration page template
- Removing “public” user registration without completely turning it off?
- Custom login modal page action
- Require re-login when logged-in user attempts to access restricted page
- Disable registration on certain condition
- Temporally disable password to login with empty password?
- Reset Password Limit not working
- Login error when username as email different to primary email
- Unable to login my wordpress website
- How do you implement a login feature on a WordPress site?
- Help! ERROR: Cookies are blocked due to unexpected output on attempting to login to resolve an issue with my site
- Problem in auto login after registration
- How to invalidate `password reset key` after being used
- How to log into WordPress admin in MAMP
- Special link for no automatic login (no username and no password)
- Can’t login with any account – No error message shown
- wp login password reset
- Global login to password protected pages
- Disable / Remove Password for Login WordPress
- Cookies error during first time login attempt
- Making sure two different wordpress website has auto login
- Hide login page and use wp_login_form on ordinary pages
- Login screen keeps resetting?
- How do i Redirect specific user(WooCommerce Sign-in not wordpress admin page sign-in) to specific page
- How can I customize the content of the login page?
- WordPress giving error when I log in after trying emergency.php
- Some crawlers/bots attempting to login with very good guesses. How?
- Create custom field for users to check if they agreed to terms
- WordPress logs out on protocol switch
- Improvements to “limit login attempts” plugin
- Add a required field to Registration
- Can’t login through wp-login.php on fresh installation
- Showing A Menu When Only Users Are Logged In [duplicate]
- wp_logout logging everyone out instead of just the user that clicked the logout link!
- Using wp_login_form passowrd as undefined
- Redirect non-members to about/intro page
- Why doesn’t the “Remember Me” checkbox work for me on a live website? Only works on a local server environment
- Secure login without SSL? [closed]
- Cant login, Password MUST be reset error, after reset
- Trying to create a log in system but getting error “Parse error: syntax error, unexpected ‘else’ (T_ELSE) ” [closed]
- authenticate to another site using a login form on my site
- Entire WordPress content disappears
- Remove login link from Reset Password-screen