Filter uncanny-learndash-groups

ulgm_user_group_ids

Filters the user group IDs associated with a group leader ID.

add_filter( 'ulgm_user_group_ids', $callback, 10, 2 );

Description

Filters the group IDs associated with a specific group leader. Developers can modify this array to include or exclude groups before they are used for user group assignments. The hook passes the original group IDs and the group leader's ID.


Usage

add_filter( 'ulgm_user_group_ids', 'your_function_name', 10, 2 );

Parameters

$user_group_ids (mixed)
This parameter contains an array of group IDs that the user belongs to.
$group_leader_id (mixed)
This parameter contains an array of group IDs that the current user is a member of.

Return Value

The filtered value.


Examples

/**
 * Example: Modify the user group IDs returned by the ulgm_user_group_ids filter.
 * This example adds a condition to exclude any group IDs that are also
 * the current user's ID, assuming the current user might be a group leader
 * and we don't want to list their own ID as a group they belong to.
 */
add_filter( 'ulgm_user_group_ids', function( $user_group_ids, $group_leader_id ) {
	// Ensure $user_group_ids is an array before attempting to modify it.
	if ( ! is_array( $user_group_ids ) ) {
		return $user_group_ids;
	}

	// Get the current logged-in user's ID.
	$current_user_id = get_current_user_id();

	// If the current user is a group leader and their ID is present in the group IDs, remove it.
	// This prevents a group leader from seeing their own ID as a group they are a member of.
	if ( $group_leader_id == $current_user_id ) {
		$user_group_ids = array_diff( $user_group_ids, array( $current_user_id ) );
	}

	// Return the modified array of group IDs.
	return $user_group_ids;
}, 10, 2 );

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/helpers/shared-functions.php:1419

public static function get_group_leader_groups( $group_leader_id = null, $exclude = array() ) {

		if ( empty( $group_leader_id ) ) {
			$group_leader_id = wp_get_current_user()->ID;
		}

		$user_group_ids = learndash_get_administrators_group_ids( $group_leader_id );
		$user_group_ids = apply_filters( 'ulgm_user_group_ids', array_map( 'absint', $user_group_ids ), $group_leader_id );

		if ( empty( $user_group_ids ) ) {
			return $user_group_ids;
		}

		$user_group_ids = array_map( 'absint', $user_group_ids );

		if ( ! empty( $exclude ) ) {
			$user_group_ids = array_diff( $user_group_ids, $exclude );
		}

		$args = array(
			'posts_per_page' => 9999,
			'include'        => array_map( 'intval', $user_group_ids ),
			'post_type'      => 'groups',
			'orderby'        => 'title',
			'order'          => 'ASC',
		);

		return get_posts( $args );
	}

Scroll to Top