ID == (int) $role; } else if ($role == 'post_author' || $role == 'page_author') { $user = isset($user) ? $user : wp_get_current_user(); $is_role = $user->ID == $post->post_author; } else if ($role == 'user_ids') { $user = isset($user) ? $user : wp_get_current_user(); $user_ids = get_post_meta( $post->ID, "restrict_user_ids", true ); $user_ids = array_map('trim', explode(',', $user_ids)); $is_role = in_array($user->ID, $user_ids); } else if ($role == 'public') { $is_role = !is_user_logged_in(); } else { $user = isset($user) ? $user : wp_get_current_user(); $is_role = $user->roles[0] == $role; } return $is_role; } public static function redirect_if_necessary() { global $post; $has_access = false; $page_roles = get_post_meta( $post->ID, "restrict_roles", true ); foreach ($page_roles as $role) { if (SimpleRestrict::check_role_for_post($post, $role)) { return; } } if (!$has_access) { $redirect = get_post_meta( $post->ID, "restrict_roles_redirect", true ); if (!$redirect || $redirect == "") { $redirect = home_url("/"); } wp_redirect( $redirect ); exit; } } public static function restrict_shortcode( $attrs, $content = null ) { global $post; $has_access = false; if (isset($attrs["only"])) { $roles = array_map('trim', explode(',', $attrs["only"])); foreach ($roles as $role) { if (SimpleRestrict::check_role_for_post($post, $role)) { $has_access = true; break; } } } if (isset($attrs["except"])) { $roles = array_map('trim', explode(',', $attrs["except"])); foreach ($roles as $role) { if (!SimpleRestrict::check_role_for_post($post, $role)) { $has_access = false; break; } } } return $has_access ? $content : ""; } public static function add_meta_box() { $screens = get_post_types( array( "public" => true ), "names"); foreach ($screens as $screen) { add_meta_box( "simplerestrict_metabox", "SimpleRestrict", array("SimpleRestrict", "meta_box"), $screen, "side", "low" ); } } public static function save_meta_box( $post_id ) { if (isset($_POST["restrict_roles"])) { update_post_meta( $post_id, "restrict_roles", $_POST["restrict_roles"] ); update_post_meta( $post_id, "restrict_roles_redirect", esc_url( $_POST["restrict_roles_redirect"] ) ); update_post_meta( $post_id, "restrict_user_ids", preg_replace('/\s+/', '', esc_attr( $_POST["restrict_user_ids"] )) ); } } public static function meta_box( $post ) { global $wp_roles; $roles = $wp_roles->get_names(); $redirect = get_post_meta( $post->ID, "restrict_roles_redirect", true ); $page_roles = get_post_meta( $post->ID, "restrict_roles", true ); $restrict_user_ids = get_post_meta( $post->ID, "restrict_user_ids", true ); if (!$page_roles) { $page_roles = array_keys($roles); array_push($page_roles, "public"); } if (!$redirect) { $redirect = home_url("/"); } ?>

Who is allowed see this page?

$value) { ?> checked="checked" name="restrict_roles[]" id="restrict_roles_" value="">
checked="checked" name="restrict_roles[]" id="restrict_roles_public" value="public">
checked="checked" name="restrict_roles[]" id="restrict_roles_post_author" value="post_author">
checked="checked" name="restrict_roles[]" id="restrict_roles_user_ids" value="user_ids">

Where should other visitors be redirected?

Hint: Restrict page content with this shortcode:
[restrict only="editor,post_author,31" except="public"]Restricted content![/restrict]

roles; } else { $user_roles = array("public"); } if (isset($attrs["only"])) { $roles = array_map('trim', explode(',', $attrs["only"])); if (array_intersect($roles, $user_roles)) { $allowed = true; } else { $allowed = false; } } if (isset($attrs["except"])) { $roles = array_map('trim', explode(',', $attrs["except"])); if (!array_intersect($roles, $user_roles)) { $allowed = true; } else { $allowed = false; } } return $allowed ? apply_filters('the_content', $content) : ""; } } SimpleRestrict::init(); ?>