How to Restrict Access to a Page by User Role in WordPress

Why Restrict Page Access by User Role?
In WordPress, user roles like Administrator, Editor, Author, Contributor, and Subscriber help define what users can do. But sometimes, you also need to control what they can see.
Use cases include:
- Creating member-only content
- Building a client dashboard
- Restricting access to staff resources or internal pages
Method 1: Restrict Access with Code (functions.php)
Add this code to your theme’s functions.php file (or custom plugin). It checks the user’s role when accessing a specific page.
function restrict_page_by_user_role() {
if ( is_page('private-dashboard') ) {
if ( ! current_user_can('editor') && ! current_user_can('administrator') ) {
wp_redirect(home_url());
exit;
}
}
}
add_action('template_redirect', 'restrict_page_by_user_role');
What it does:
- Checks if the current page is “private-dashboard” (use the page slug)
- Allows only Editors and Administrators
- Redirects others to the homepage
Method 2: Restrict by Page ID
Prefer using the page ID instead of slug? Just update the condition like this:
if ( is_page(42) ) { // 42 is the page ID
To find the page ID, go to Pages → Edit and check the URL — it ends in post=42.
Method 3: Display Message Instead of Redirect
Instead of redirecting, you can show a friendly message:
if ( ! current_user_can('editor') ) {
wp_die('Sorry, this page is only accessible to editors.');
}
Or replace wp_die() with a custom template or login form if desired.
Bonus: Restrict Access to a Custom Post Type
if ( is_singular('project') && ! current_user_can('administrator') ) {
wp_redirect(home_url());
exit;
}
This helps if you’re building private content types like Projects, Invoices, or Courses.
Alternative: Use a Lightweight Plugin
Not comfortable with code? Try:
- Members by MemberPress — lets you assign roles and restrict content in the editor
- User Access Manager
Conclusion
Restricting page access by user role in WordPress gives you control over who sees what. Whether you do it with code or plugins, it’s an essential feature for private content, memberships, and admin-only tools.
Use the method that best fits your site — and keep your content secure and organized.