-
Create a subfolder in plugins folder, name it ‘MyRestict’. In This folder put the following php:
<?php /** * Plugin Name: My Restict * Plugin URI: http://wordpress.stackexchange.com/questions/112566/how-to-set-different-users-for-different-pages * Author: G.M. */ function my_restict_template_filter( $template ) { if ( is_page() ) { $post = get_queried_object(); $allowed = (string) get_post_meta($post->ID, 'allowed_users', true); if ( $allowed ) $allowed = array_map('trim', explode(',', $allowed) ); $user = wp_get_current_user(); if ( ! empty($allowed) && ( ! is_object($user) || ! in_array($user->ID, $allowed) ) ) return plugin_dir_path(__FILE__) . 'not-allowed.php'; } return $template; } add_filter('template_include', 'my_restict_template_filter');
-
In the same ‘MyRestict’ folder create a file named
not-allowed.php
and there put something like:<div>Sorry, you are not allowed to view this page.</div>
-
In your pages you have to create a custom field with the key
allowed_users
where you have to write comma separed list of users ids of users that you want to allow viewing that page:
-
Activate the ‘My Restict’ plugin in the backend
Remember that pages that do not have any custom field will be accessible to all visitors, even not logged ones.