Action uncanny-learndash-groups

ulgm_user_redeems_group_key

Fires after a user redeems a code, just before redirection. Fires after a user successfully redeems a group key, before the redirection occurs.

add_action( 'ulgm_user_redeems_group_key', $callback, 10, 2 );

Description

Fires after a user successfully redeems a group key, prior to any redirection. This hook provides the user's ID and the details of the redeemed code. Developers can use this to perform custom actions, like logging the redemption, sending notifications, or modifying post-redemption data before the user is redirected.


Usage

add_action( 'ulgm_user_redeems_group_key', 'your_function_name', 10, 2 );

Parameters

$user_id (int)
User ID.
$coode_details (array|null)
Redemption code details, if available.

Examples

// Example usage of the 'ulgm_user_redeems_group_key' action hook.
// This function will be executed after a user successfully redeems a group key.
// It will log the user ID and the redeemed code details to the WordPress debug log.
add_action( 'ulgm_user_redeems_group_key', 'my_custom_group_redemption_handler', 10, 2 );

/**
 * Handles the redemption of a group key by a user.
 *
 * @param int $user_id         The ID of the user who redeemed the key.
 * @param array|null $code_details Details of the redeemed code, if available.
 */
function my_custom_group_redemption_handler( $user_id, $code_details ) {

	// Check if we have user ID and code details to process.
	if ( ! $user_id || empty( $code_details ) ) {
		return; // Exit if essential data is missing.
	}

	// For demonstration purposes, let's log this event.
	// In a real scenario, you might update user meta, trigger an email,
	// or perform other actions based on the redemption.
	if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
		// Get the username for better logging.
		$user = get_user_by( 'id', $user_id );
		$username = $user ? $user->user_login : 'Unknown User';

		$message = sprintf(
			'User "%s" (ID: %d) successfully redeemed group key. Code Details: %s',
			$username,
			$user_id,
			print_r( $code_details, true ) // Print array details for debugging.
		);

		error_log( $message );
	}

	// Example: Add a custom meta field to the user indicating group redemption.
	// You would likely want to check if this meta already exists to avoid duplicates
	// or manage updates based on your plugin's logic.
	$group_key_name = isset( $code_details['name'] ) ? sanitize_text_field( $code_details['name'] ) : 'default_group';
	update_user_meta( $user_id, 'last_group_redeemed', $group_key_name );

}

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:383

}
		/**
		 * Fires after a user redeems a code, just before redirection.
		 *
		 * @param int $user_id User ID.
		 * @param array|null $coode_details Redemption code details, if available.
		 */
		do_action( 'ulgm_user_redeems_group_key', $user_id, self::$code_details );

		if ( ! empty( $redirect_to ) ) {
			wp_safe_redirect( $redirect_to );
		} else {
			wp_safe_redirect( get_permalink() . '?' . $_REQUEST['key'] . '&registered&nonce=' . wp_create_nonce( time() ) );
		}
		exit;


Scroll to Top