Action uncanny-learndash-groups

ulgm_user_redeems_group_key_before

Fires before a user redeems a group key, providing user, code, and POST data for modification.

add_action( 'ulgm_user_redeems_group_key_before', $callback, 10, 3 );

Description

Fires before a user successfully redeems a group key. Developers can use this hook to perform actions like validating the redemption, modifying redemption details, or initiating custom logic based on the user and redemption code before the group is assigned. Access the current user object, code details, and POST data for context.


Usage

add_action( 'ulgm_user_redeems_group_key_before', 'your_function_name', 10, 3 );

Parameters

$user (mixed)
This parameter contains the WordPress `WP_User` object for the currently logged-in user who is redeeming the group key.
$code_details (mixed)
The current WordPress user object.
$_POST (mixed)
This parameter contains an array of details about the redeemed group code.

Examples

/**
 * Example function hooked to ulgm_user_redeems_group_key_before.
 *
 * This function demonstrates how to intercept user group key redemption before it happens.
 * It checks if the user is already a member of the target group and can optionally
 * prevent the redemption if the user is already a member or if a specific role
 * is not set.
 *
 * @param WP_User $user The current WP_User object.
 * @param array   $code_details An array containing details about the redeemed code.
 *                              Expected keys include 'ld_group_id' and potentially others.
 * @param array   $_POST The $_POST superglobal array containing data submitted with the form.
 */
function my_ulgm_prevent_duplicate_group_membership( WP_User $user, array $code_details, array $_POST ) {

	// Prevent redemption if the user is already in the target group.
	if ( isset( $code_details['ld_group_id'] ) ) {
		$ld_group_id = (int) $code_details['ld_group_id'];

		// Check if the user is already a member of the LearnDash group.
		if ( function_exists( 'learndash_is_user_in_group' ) && learndash_is_user_in_group( $user->ID, $ld_group_id ) ) {
			// Optionally, you could display an error message to the user here.
			// For this example, we'll just silently prevent the redemption.
			// You might also want to redirect the user to a different page.
			wp_die( __( 'You are already a member of this group.', 'your-text-domain' ) );
		}

		// Example: Prevent redemption if a specific role is required but not set.
		$required_role = sanitize_text_field( $_POST['_ulgm_code_default_role'] ?? '' );
		if ( ! empty( $required_role ) && ! current_user_can( $required_role, $user->ID ) ) {
			// If the user doesn't have the required role and it's specified, stop.
			// This assumes your logic might involve checking for a prerequisite role
			// before allowing group membership.
			wp_die( __( 'You do not meet the requirements for this group.', 'your-text-domain' ) );
		}
	}
}
add_action( 'ulgm_user_redeems_group_key_before', 'my_ulgm_prevent_duplicate_group_membership', 10, 3 );

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-registration.php:366

return;
		}

		$redirect_to = esc_url_raw( ulgm_filter_input( '_ulgm_code_redirect_to', INPUT_POST ) );
		$set_role    = trim( sanitize_text_field( ulgm_filter_input( '_ulgm_code_default_role', INPUT_POST ) ) );
		$user        = wp_get_current_user();
		$user_id     = $user->ID;
		do_action( 'ulgm_user_redeems_group_key_before', $user, self::$code_details, $_POST );
		update_user_meta( $user_id, '_ulgm_code_used', $code_redeem );
		$ld_group_id = (int) self::$code_details['ld_group_id'] ?? 0;
		$result      = ulgm()->group_management->set_user_to_code( $user_id, $code_redeem, SharedFunctions::$not_started_status, $ld_group_id );
		if ( $result ) {
			SharedFunctions::set_user_to_group( $user_id, self::$code_details['ld_group_id'] );
			if ( ! empty( $set_role ) ) {
				// check if this role really exists.


Scroll to Top