Filter uncanny-learndash-groups

ulgm_groups_management_selected_group_id

Filters the selected group ID when managing groups, providing access to the current ID and group objects.

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

Description

Filters the ID of the currently managed group. This hook fires after the group ID has been determined but before it's assigned. Developers can use this to dynamically change the selected group ID or perform custom validation on the group ID and the available group objects.


Usage

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

Parameters

$ulgm_current_managed_group_id (mixed)
This parameter contains the ID of the currently managed group that has been selected.
$ulgm_managed_group_objects (mixed)
This parameter contains the ID of the currently selected group that is being managed.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of using the 'ulgm_groups_management_selected_group_id' filter.
 *
 * This filter allows developers to programmatically change the selected group ID
 * within the "Ultimate Live Group Management" interface before it's displayed.
 *
 * In this example, we'll demonstrate how to pre-select a specific group
 * based on a query parameter in the URL, if it exists and is a valid group.
 *
 * @param mixed $ulgm_current_managed_group_id The current selected group ID.
 * @param mixed $ulgm_managed_group_objects    An array or object containing all available group objects.
 * @return mixed The modified or original group ID to be selected.
 */
function my_custom_ulgm_select_group( $ulgm_current_managed_group_id, $ulgm_managed_group_objects ) {

	// Check if a 'group_id' parameter exists in the URL query string.
	if ( isset( $_GET['group_id'] ) && ! empty( $_GET['group_id'] ) ) {
		$requested_group_id = sanitize_text_field( $_GET['group_id'] );

		// Ensure the requested group ID is valid and exists within the managed groups.
		// This check assumes $ulgm_managed_group_objects is an array of group objects or IDs.
		// The exact structure might vary depending on the plugin's implementation.
		// For this example, we'll assume it's an array of objects with an 'id' property.
		if ( is_array( $ulgm_managed_group_objects ) ) {
			foreach ( $ulgm_managed_group_objects as $group ) {
				// Assuming $group is an object with an 'id' property.
				// Adjust this condition if the structure is different (e.g., an array of IDs).
				if ( isset( $group->id ) && $group->id == $requested_group_id ) {
					// If the requested group ID is valid, use it as the selected group.
					return $requested_group_id;
				}
			}
		}
	}

	// If no valid group_id parameter is found, return the original selected group ID.
	return $ulgm_current_managed_group_id;
}
add_filter( 'ulgm_groups_management_selected_group_id', 'my_custom_ulgm_select_group', 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/group-management/group-management-interface.php:797

return $validation;
			}
			self::$ulgm_current_managed_group_id = $current_group_id;
		}

		// Filter selected group on page load
		self::$ulgm_current_managed_group_id = apply_filters( 'ulgm_groups_management_selected_group_id', self::$ulgm_current_managed_group_id, self::$ulgm_managed_group_objects );
		$validation['success']               = true;

		return $validation;
	}


	/**

Scroll to Top