Action uncanny-learndash-groups

ulgm_user_registered

Fires after a user is registered in the frontend, just before redirection. Fires after a user is registered frontend, before redirection, providing the new user ID.

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

Description

Fires after a user successfully registers via the frontend, right before redirection. Developers can use this hook to perform custom actions with the new user ID and any associated redemption code details, such as sending custom welcome emails or assigning default roles.


Usage

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

Parameters

$new_user_id (int)
Newly registered user ID.
$coode_details (array|null)
Redemption code details, if available.

Examples

/**
 * Example function to hook into 'ulgm_user_registered' action.
 *
 * This function demonstrates how to access the newly registered user's ID
 * and any redemption code details that might have been used during registration.
 * It could be used for various purposes like sending a welcome email,
 * assigning default user roles, or logging the registration event.
 *
 * @param int $new_user_id The ID of the newly registered user.
 * @param array|null $coode_details An array of redemption code details, or null if none were used.
 */
function my_custom_user_registration_handler( $new_user_id, $coode_details ) {
	// Ensure we have a valid user ID.
	if ( ! $new_user_id ) {
		return;
	}

	// You can retrieve user data using get_userdata().
	$user_info = get_userdata( $new_user_id );

	if ( $user_info ) {
		$user_login = $user_info->user_login;
		$user_email = $user_info->user_email;

		// Example: Log the user registration to a custom log file or WordPress debug log.
		error_log( "User registered: ID {$new_user_id}, Username: {$user_login}, Email: {$user_email}." );

		// Example: If redemption code details are available, process them.
		if ( ! empty( $coode_details ) && is_array( $coode_details ) ) {
			$redemption_code = isset( $coode_details['code'] ) ? sanitize_text_field( $coode_details['code'] ) : 'N/A';
			$discount_applied = isset( $coode_details['discount'] ) ? floatval( $coode_details['discount'] ) : 0;

			error_log( "User {$user_login} registered using redemption code: {$redemption_code} with a discount of {$discount_applied}%." );

			// Example: Update user meta with redemption code information.
			update_user_meta( $new_user_id, 'used_redemption_code', $redemption_code );
			update_user_meta( $new_user_id, 'redemption_discount', $discount_applied );
		}

		// Example: Assign a default role to the new user.
		// Make sure the role 'subscriber' actually exists in your WordPress installation.
		if ( ! in_array( 'subscriber', $user_info->roles ) ) {
			$user_obj = new WP_User( $new_user_id );
			$user_obj->add_role( 'subscriber' );
			error_log( "Assigned 'subscriber' role to user ID {$new_user_id}." );
		}

		// Example: Send a custom welcome email.
		// You could use wp_mail() here. For more complex emails, consider a templating system.
		// $to = $user_email;
		// $subject = 'Welcome to Our Website!';
		// $message = 'Thank you for registering. We are excited to have you onboard!';
		// $headers = array('Content-Type: text/html; charset=UTF-8');
		// wp_mail( $to, $subject, $message, $headers );

	} else {
		error_log( "Error: Could not retrieve user data for new user ID: {$new_user_id} after registration." );
	}
}

// Add the action hook. The '3' indicates that this function accepts 3 arguments (though we only use 2 here, the hook provides 2).
// The '10' is the default priority, meaning it will run after most other functions hooked to this action.
add_action( 'ulgm_user_registered', 'my_custom_user_registration_handler', 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-registration.php:287

/**
		 * Fires after a user is registered in the frontend, just before redirection.
		 *
		 * @param int $new_user_id Newly registered user ID.
		 * @param array|null $coode_details Redemption code details, if available.
		 */
		do_action( 'ulgm_user_registered', $new_user_id, self::$code_details );

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


Scroll to Top