Filter uncanny-learndash-groups

ulgm_append_group_name_in_list

Filters the group name when appended in lists, allowing modification of its display or content.

add_filter( 'ulgm_append_group_name_in_list', $callback, 10, 1 );

Description

Filters whether the group name should be appended to the user's list item in LearnDash progress reports. Developers can return `true` to display the group name alongside the user's progress for better clarity, especially when users belong to multiple groups.


Usage

add_filter( 'ulgm_append_group_name_in_list', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Filter to determine if group names should be appended to the list for users.
 *
 * This function demonstrates how to conditionally append group names to user
 * entries in a list, based on the number of groups a user belongs to.
 *
 * @param bool $should_append Whether to append group names. Defaults to false.
 * @return bool The modified value of whether to append group names.
 */
add_filter( 'ulgm_append_group_name_in_list', function( $should_append ) {

	// Check if we are in an environment where group association data is available.
	// In a real-world scenario, you might check for specific conditions or settings.
	if ( ! empty( self::$group_association ) ) {
		// For this example, let's decide to append group names only if the user
		// belongs to fewer than 2 groups, to keep the list concise.
		// This logic would typically be applied within the loop where $user_id is known,
		// but this filter hook allows for a global decision or modification.
		// If the original hook value was already true, we preserve it.
		return true;
	}

	return $should_append; // Return the original value if conditions aren't met.
}, 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/reports/learndash-progress-report.php:2012

foreach ( $results as $user_id => $val ) {
				// Not in group.
				if ( ! in_array( absint( $user_id ), $group_user_ids, true ) ) {
					unset( $results[ $user_id ] );
					continue;
				}
				// Append group name to label.
				if ( true === apply_filters( 'ulgm_append_group_name_in_list', false ) && isset( self::$group_association[ $user_id ] ) && ! empty( self::$group_association[ $user_id ]['groups'] ) ) {
					$total = count( self::$group_association[ $user_id ]['groups'] );
					if ( $total < 3 ) {
						$group_names = join( ', ', self::$group_association[ $user_id ]['groups'] );
					} else {
						$first       = array_shift( self::$group_association[ $user_id ]['groups'] );
						$group_names = sprintf( "$first %s", __( '& others', 'uncanny-learndash-groups' ) );
					}

Scroll to Top