Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 21, 2017 17:45
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 wpmudev-sls/bb06c4e4d266c9b260e324775158448c to your computer and use it in GitHub Desktop.
Save wpmudev-sls/bb06c4e4d266c9b260e324775158448c to your computer and use it in GitHub Desktop.
A custom function that returns an array with instructor and student role per user id for CoursePress
<?php
/*
Plugin Name: [CoursePress 2] - User role in CoursePress
Plugin URI: https://premium.wpmudev.org/
Description: A custom function that returns an array with instructor and student role per user id for CoursePress
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
function cp_user_roles( $user_id = null ){
if( is_null( $user_id ) ){
return array( 'is_student' => false, 'is_instructor' => false );
}
$user_cp_role = array( 'is_student' => false, 'is_instructor' => CoursePress_Data_Capabilities::is_instructor( $user_id ) );
$post_args = array(
'post_type' => CoursePress_Data_Course::get_post_type_name(),
'posts_per_page' => -1
);
$courses = new WP_Query( $post_args );
foreach( $courses->posts as $course ){
if( ! $user_cp_role['is_student'] && CoursePress_Data_Course::student_enrolled( $user_id, $course->ID ) ){
$user_cp_role['is_student'] = true;
}
}
return $user_cp_role;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment