// Function for appending items to the header based on their role function custom_role_menu_items($items, $args) { // Only affect Astra Primary Menu if (!isset($args->theme_location) || $args->theme_location !== 'primary') { return $items; } if (!is_user_logged_in()) { return $items; } $user = wp_get_current_user(); // STUDENT if (in_array('student', $user->roles)) { $items .= ''; } // PENDING STUDENT elseif (in_array('pending_student', $user->roles)) { $items .= ''; $items .= ''; } // INSTRUCTOR elseif (in_array('instructor', $user->roles)) { $items .= ''; } return $items; } add_filter('wp_nav_menu_items', 'custom_role_menu_items', 10, 2); //Helper function that returns a boolean based on if a user can access a page or not function sbp_user_can_access($area) { // Must be logged in if (!is_user_logged_in()) { return false; } $user = wp_get_current_user(); // Admins can access everything if (in_array('administrator', $user->roles, true)) { return true; } switch ($area) { case 'student': return in_array('student', $user->roles, true); case 'instructor': return in_array('instructor', $user->roles, true); case 'pending': return in_array('pending_student', $user->roles, true); default: return false; } } function sbp_portal_guard() { $protected_pages = [ 'student-portal' => 'student', 'instructor-portal' => 'instructor', 'enroll' => 'pending', ]; foreach ($protected_pages as $page => $required_role) { if (is_page($page)) { if (!sbp_user_can_access($required_role)) { // Guests go to login if (!is_user_logged_in()) { wp_redirect(home_url('/login/')); } else { // Logged-in users without permission go home wp_redirect(home_url('/')); } exit; } // Stop checking once we've matched a protected page break; } } } add_action('template_redirect', 'sbp_portal_guard');

Coming Soon