Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogist/603de87322d1e2f1f6f7bcd9401d1b80 to your computer and use it in GitHub Desktop.
Save woogist/603de87322d1e2f1f6f7bcd9401d1b80 to your computer and use it in GitHub Desktop.
This code prevents Sensei learners from starting a course unless they've been specifically added to that course by a site administrator or a teacher.
<?php
function sensei_display_start_course_form_when_admin_or_teacher( $should_display_start, $course_id ) {
global $current_user;
if ( empty( $current_user ) ) {
$current_user = wp_get_current_user();
}
if ( !( $current_user instanceof WP_User ) || 0 === $current_user->ID ) {
return false;
}
$allowed_roles = array( 'administrator', 'teacher' );
$user_roles = $current_user->roles;
$is_administrator_or_teacher = count( array_intersect( $allowed_roles, $user_roles ) ) > 0;
return $is_administrator_or_teacher ? true : false;
};
add_filter( 'sensei_display_start_course_form', 'sensei_display_start_course_form_when_admin_or_teacher', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment