ulgm_group_courses_list_courses
Filters the list of courses to be displayed within a group.
add_filter( 'ulgm_group_courses_list_courses', $callback, 10, 3 );
Description
Filters the list of courses associated with a specific LearnDash group before they are displayed. Developers can use this hook to modify, add, or remove courses from the list, or to alter the data passed to the template. This hook fires after courses are queried for a group.
Usage
add_filter( 'ulgm_group_courses_list_courses', 'your_function_name', 10, 3 );
Parameters
-
$courses(mixed) - This parameter contains an array of course objects or related data intended to be displayed in the group's course list.
-
$post_vars(mixed) - This parameter contains an array of course objects that are currently being displayed for the group.
-
$group_id(mixed) - This parameter contains an array of the current WordPress post variables.
Return Value
The filtered value.
Examples
/**
* Example: Filter to modify the list of courses displayed for a group.
* This example demonstrates how to remove a specific course from the list if it has not yet been started by any group members.
*
* @param array $courses The original array of course objects.
* @param array $post_vars An array containing various post-related variables.
* @param int $group_id The ID of the current group.
*
* @return array The modified array of course objects.
*/
add_filter(
'ulgm_group_courses_list_courses',
function ( $courses, $post_vars, $group_id ) {
// Ensure we are working with an array of courses
if ( ! is_array( $courses ) ) {
return $courses;
}
// If there are no courses, nothing to do
if ( empty( $courses ) ) {
return $courses;
}
// Get group members to check if courses have been started
$group_users = uncanny_learndash_groups()->get_group_users( $group_id );
// Check if group users were retrieved
if ( empty( $group_users ) || ! is_array( $group_users ) ) {
return $courses;
}
$courses_to_remove = array();
foreach ( $courses as $course_id => $course_data ) {
// Assume course_data is an object or array containing course information
// and that it has an ID accessible (e.g., $course_data->ID or $course_data['ID'])
$current_course_id = isset( $course_data->ID ) ? $course_data->ID : ( isset( $course_data['ID'] ) ? $course_data['ID'] : null );
if ( ! $current_course_id ) {
continue; // Skip if we can't determine the course ID
}
$course_has_been_started = false;
foreach ( $group_users as $user_id => $user_data ) {
// Check if the user has started this specific course
// This assumes a function or method to check user progress,
// you might need to adjust this based on your LearnDash integration.
// For example, using LearnDash's API if available.
if ( function_exists( 'ld_has_user_started_course' ) && ld_has_user_started_course( $user_id, $current_course_id ) ) {
$course_has_been_started = true;
break; // No need to check other users for this course
}
}
// If the course hasn't been started by any group member, mark it for removal
if ( ! $course_has_been_started ) {
$courses_to_remove[] = $course_id;
}
}
// Remove the courses that haven't been started
foreach ( $courses_to_remove as $course_id_to_remove ) {
unset( $courses[ $course_id_to_remove ] );
}
// Return the filtered courses
return $courses;
},
10, // Priority
3 // Accepted arguments: $courses, $post_vars, $group_id
);
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/templates/frontend-uo_groups/users-table-actions.php:81
}
}
// Reset Query
wp_reset_postdata();
}
$courses = apply_filters( 'ulgm_group_courses_list_courses', $courses, $post_vars, $group_id );
$number_of_courses = count( $courses );
?>
<div class="uo-row uo-header">
<h2 class="group-courses-heading uo-looks-like-h3">
<?php printf( _x( 'Group %s', '%s is the "courses" LearnDash label', 'uncanny-learndash-groups' ), $course_label_plural ); ?>
</h2>
<p class="uo-header-subtitle">