Solution:
This is a basic page template using conditional statements to restrict users from viewing specific contents pages. That’s probably somewhat what you’re looking for, at least you will be close, and you can do some research on your own for the rest. I’ve commented pretty much everything used. Modify it and have fun.
<?php
if ( ! defined( 'ABSPATH' ) ):
exit;
endif;
/**
* is_user_logged_in
* Determines whether the current visitor is a logged in user.
* @link https://developer.wordpress.org/reference/functions/is_user_logged_in/
*
* wp_get_current_user
* Retrieve the current user object.
* @link https://developer.wordpress.org/reference/functions/wp_get_current_user/
* @link https://wordpress.org/support/article/roles-and-capabilities/
*/
$users = wp_get_current_user();
$roles = array( 'administrator', 'editor', 'author', 'subscriber' );
if ( ! is_user_logged_in() || ! array_intersect( $roles, $users->roles ) ):
/**
* wp_logout
* Log the current user out.
* @link https://developer.wordpress.org/reference/functions/wp_logout/
*/
wp_logout();
/**
* wp_redirect
* Redirects to another page.
* @link https://developer.wordpress.org/reference/functions/wp_redirect/
*
* wp_registration_url
* Returns the URL that allows the user to register on the site.
* @link https://developer.wordpress.org/reference/functions/wp_registration_url/
*/
wp_redirect( esc_url( wp_registration_url() ) );
exit;
endif;
/**
* get_header
* Load header template.
* @link https://developer.wordpress.org/reference/functions/get_header/
*/
get_header();
/**
* The Loop
* @link https://developer.wordpress.org/themes/basics/the-loop/
*/
if( have_posts() ):
while( have_posts() ): the_post();
/**
* get_the_title
* Retrieve post title.
* @link https://developer.wordpress.org/reference/functions/get_the_title/
*
* the_title
* Display or retrieve the current post title with optional markup.
* @link https://developer.wordpress.org/reference/functions/the_title/
*/
if( get_the_title() !== '' ):
esc_attr( the_title() );
endif;
/**
* get_the_content
* Retrieve the post content.
* @link https://developer.wordpress.org/reference/functions/get_the_content/
*
* the_content
* Display the post content.
* @link https://developer.wordpress.org/reference/functions/the_content/
*/
if( get_the_content() !== '' ):
esc_attr( the_content() );
endif;
endwhile;
endif;
/**
* get_footer
* Load footer template.
* @link https://developer.wordpress.org/reference/functions/get_footer/
*/
get_footer(); ?>
Learn more
wp_logout
@ https://developer.wordpress.org/reference/functions/wp_logout/wp_redirect
@ https://developer.wordpress.org/reference/functions/wp_redirect/wp_registration_url
@ https://developer.wordpress.org/reference/functions/wp_registration_url/