Page restriction in WordPress allows you to restrict access to certain pages on your website, ensuring that only authorized users can view them. This can be useful for creating private pages, member-only content, or restricting access to certain sections of your site.
To achieve page restriction in WordPress, you can use the is_user_logged_in()
function to check if a user is logged in, and the wp_redirect()
function to redirect users to a different page if they don’t have the necessary access.
Here’s an example code snippet that restricts access to a specific page for non-logged-in users:
function wpsnippets_restrict_page_access() {
if ( ! is_user_logged_in() && is_page( 'restricted-page' ) ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_restrict_page_access' );
In this example, the wpsnippets_restrict_page_access()
function is hooked to the template_redirect
action, which is triggered before the template is loaded. Inside the function, we first check if the user is not logged in using the is_user_logged_in()
function. Then, we use the is_page()
function to check if the current page is the restricted page (replace 'restricted-page'
with the slug or ID of your restricted page).
If the user is not logged in and is trying to access the restricted page, we redirect them to the home page using the wp_redirect()
function. The exit
statement is used to stop further execution of the code.
You can modify this code snippet to suit your specific needs. For example, you can redirect users to a custom login page instead of the home page by changing the URL in the wp_redirect()
function.
Remember to prefix the function name with wpsnippets_
or your own custom prefix to follow WordPress coding standards.
Examples
Example 1: Restricting a Page to Logged-in Users Only
This use case demonstrates how to restrict a specific page on a WordPress website so that only logged-in users can access it. The code example uses the template_redirect
action hook to check if the current page is the restricted page and if the user is not logged in. If the conditions are met, the user is redirected to the login page.
function wpsnippets_restrict_page_to_logged_in_users() {
if ( is_page( 'restricted-page' ) && ! is_user_logged_in() ) {
wp_redirect( wp_login_url() );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_restrict_page_to_logged_in_users' );
Explanation: The wpsnippets_restrict_page_to_logged_in_users
function checks if the current page is the “restricted-page” using the is_page
function. It then verifies if the user is not logged in using the is_user_logged_in
function. If both conditions are true, the user is redirected to the login page using wp_redirect
and the script execution is terminated with exit
.
Example 2: Restricting a Page to a Specific User Role
In this use case, we restrict a page to a specific user role, allowing only users with that role to access the page. The code example checks if the current page is the restricted page and if the logged-in user has the required role. If not, the user is redirected to a custom error page.
function wpsnippets_restrict_page_to_specific_user_role() {
if ( is_page( 'restricted-page' ) && ! current_user_can( 'editor' ) ) {
wp_redirect( home_url( '/error-page' ) );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_restrict_page_to_specific_user_role' );
Explanation: The wpsnippets_restrict_page_to_specific_user_role
function checks if the current page is the “restricted-page” using the is_page
function. It then uses the current_user_can
function to check if the logged-in user has the “editor” role. If the user does not have the required role, they are redirected to the custom error page using wp_redirect
and the script execution is terminated with exit
.
Example 3: Restricting a Page to Multiple User Roles
This use case demonstrates how to restrict a page to multiple user roles, allowing only users with any of those roles to access the page. The code example checks if the current page is the restricted page and if the logged-in user has any of the specified roles. If not, the user is redirected to a custom error page.
function wpsnippets_restrict_page_to_multiple_user_roles() {
$allowed_roles = array( 'editor', 'author' );
if ( is_page( 'restricted-page' ) && ! array_intersect( $allowed_roles, wp_get_current_user()->roles ) ) {
wp_redirect( home_url( '/error-page' ) );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_restrict_page_to_multiple_user_roles' );
Explanation: The wpsnippets_restrict_page_to_multiple_user_roles
function checks if the current page is the “restricted-page” using the is_page
function. It then uses the array_intersect
function to check if the logged-in user has any of the specified roles (editor
or author
). If the user does not have any of the allowed roles, they are redirected to the custom error page using wp_redirect
and the script execution is terminated with exit
.