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.