ucec_course_report_select2_users
Filters the user options for the course report select2 dropdown, allowing modification of the user list displayed.
add_filter( 'ucec_course_report_select2_users', $callback, 10, 1 );
Description
Filters the user options for the Select2 dropdown in the course report. Developers can modify the `$options` array to customize which users are displayed or to add custom user data. This hook fires when fetching users for the course report's user selection interface.
Usage
add_filter( 'ucec_course_report_select2_users', 'your_function_name', 10, 1 );
Parameters
-
$options(mixed) - This parameter contains an array of user options, which is passed to the filter and can be modified to customize the user list displayed in the Select2 dropdown for course reports.
Return Value
The filtered value.
Examples
/**
* Example: Filter the Select2 user options for the course report API
* to include only users who are instructors or have a specific user role.
*
* @param array $options The current array of user options.
* @return array The filtered array of user options.
*/
add_filter( 'ucec_course_report_select2_users', function( $options ) {
// Assuming $request object is available or can be accessed.
// In a real scenario, you might need to access global $wpdb or use WP_User_Query.
// For this example, let's simulate getting user search terms and filtering.
// Get current search term if available (this is context-dependent)
$user_search_term = isset( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : '';
$args = array(
'role__in' => array( 'instructor', 'administrator' ), // Example: filter by instructor or admin roles
'search' => '*' . $user_search_term . '*', // Wildcard search for flexibility
'fields' => 'all', // Get full user objects
);
$user_query = new WP_User_Query( $args );
$users = $user_query->get_results();
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
// Format the output for Select2
$options[] = array(
'id' => $user->ID,
'text' => $user->display_name . ' (' . $user->user_login . ')',
);
}
}
return $options;
}, 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/course-report-api.php:510
src/classes/course-report-api.php:580
$options = array();
// Sanitize user search
$user_search = $request->get_param( 'search' );
$user_search = ! empty( $user_search ) ? sanitize_text_field( $user_search ) : '';
if ( empty( $user_search ) ) {
$options = apply_filters( 'ucec_course_report_select2_users', $options );
return $options;
}
// Group leader users.
$filtered_ids = null;
if ( $request->has_param( 'group_leader_users' ) ) {
$filtered_ids = $request->get_param( 'group_leader_users' );