ulgm_user_redeemed_key
Fires when a user successfully redeems a license key, passing the user ID, code, and status.
add_action( 'ulgm_user_redeemed_key', $callback, 10, 3 );
Description
Fires after a user successfully redeems a license key. Developers can use this hook to perform custom actions, such as granting access to courses, updating user meta, or triggering custom notifications, based on the redeemed code and its status.
Usage
add_action( 'ulgm_user_redeemed_key', 'your_function_name', 10, 3 );
Parameters
-
$user_id(mixed) - The ID of the user who redeemed the code.
-
$code(mixed) - The ID of the user who redeemed a code.
-
$code_status(mixed) - The redemption code that was used.
Examples
/**
* Example function to handle the ulgm_user_redeemed_key action.
* This function logs the user ID, redeemed code, and its status to the WordPress debug log.
*
* @param int $user_id The ID of the user who redeemed the key.
* @param string $code The redeemed key code.
* @param string $code_status The status of the redeemed code.
*/
function my_ulgm_log_redeemed_key( $user_id, $code, $code_status ) {
if ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG ) {
return; // Do nothing if debug logging is not enabled.
}
// Ensure we have a valid user ID to log.
if ( empty( $user_id ) || ! is_numeric( $user_id ) ) {
error_log( 'ULGM Redeemed Key Log: Invalid User ID provided.' );
return;
}
// Log the redemption details.
$user_info = get_userdata( $user_id );
$username = $user_info ? $user_info->user_login : 'Unknown User';
$log_message = sprintf(
'ULGM Redeemed Key: User "%s" (ID: %d) redeemed code "%s" with status "%s".',
$username,
absint( $user_id ),
esc_html( $code ),
esc_html( $code_status )
);
error_log( $log_message );
}
// Hook the function to the 'ulgm_user_redeemed_key' action.
// The third parameter '3' indicates that the hooked function accepts 3 arguments.
add_action( 'ulgm_user_redeemed_key', 'my_ulgm_log_redeemed_key', 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/handlers/class-group-management-db-handler.php:522
public function set_user_to_code( $user_id, $code, $code_status = '', $ld_group_id = 0 ) {
if ( empty( $code_status ) ) {
$code_status = SharedFunctions::$redeem_status;
}
if ( empty( $user_id ) || empty( $code ) ) {
return false;
}
if ( 0 === (int) $ld_group_id ) {
$ld_group_id = $this->get_ld_group_id_from_code_group( $code );
}
global $wpdb;
$success = false;
$code = trim( esc_attr( $code ) );
$user_id = absint( $user_id );
if ( null !== $code_status ) {
$success = $wpdb->update(
$wpdb->prefix . ulgm()->db->tbl_group_codes,
array(
'code_status' => $code_status,
'student_id' => $user_id,
'used_date' => current_time( 'mysql' ),
'ld_group_id' => $ld_group_id,
),
array( 'code' => $code ),
array( '%s', '%d', '%s', '%d' ),
array( '%s' )
);
}
do_action( 'ulgm_user_redeemed_key', $user_id, $code, $code_status );
return $success;
}