ulgm_user_added_to_group
Fires when a user is successfully added to a LearnDash group, providing the user and group IDs.
add_action( 'ulgm_user_added_to_group', $callback, 10, 2 );
Description
Fires when a user is successfully added to a LearnDash group. Developers can use this hook to perform custom actions, such as sending notifications, updating user meta, or triggering other integrations based on group membership changes. The hook passes the user's ID and the group's ID as parameters.
Usage
add_action( 'ulgm_user_added_to_group', 'your_function_name', 10, 2 );
Parameters
-
$user_id(mixed) - The user ID of the user who has been added to a group.
-
$ld_group_id(mixed) - The user ID of the user who has been added to a group.
Examples
add_action( 'ulgm_user_added_to_group', 'my_custom_group_addition_logic', 10, 2 );
/**
* Example callback function for the 'ulgm_user_added_to_group' action hook.
* This function demonstrates how to perform custom actions when a user is added to a LearnDash group.
* For instance, it might send a welcome email or grant access to specific content.
*
* @param int $user_id The ID of the user who was added to the group.
* @param int $ld_group_id The ID of the LearnDash group the user was added to.
*/
function my_custom_group_addition_logic( $user_id, $ld_group_id ) {
// Ensure we have valid user and group IDs.
if ( ! is_numeric( $user_id ) || ! is_numeric( $ld_group_id ) ) {
// Log an error or handle the invalid input appropriately.
error_log( 'Invalid user ID or group ID passed to my_custom_group_addition_logic.' );
return;
}
// Retrieve user and group details for potential use.
$user_info = get_userdata( $user_id );
$group_title = get_the_title( $ld_group_id ); // Assuming it's a post type group.
if ( ! $user_info || ! $group_title ) {
error_log( "Could not retrieve user or group details for user ID {$user_id} and group ID {$ld_group_id}." );
return;
}
// --- Example 1: Send a custom welcome email ---
// You would typically have a more robust email sending mechanism.
$email_subject = sprintf( 'Welcome to the "%s" group!', $group_title );
$email_body = sprintf(
"Hello %s,nnYou have been successfully added to the '%s' group.nnWe hope you enjoy the resources and community within this group!nnBest regards,nYour Website Team",
$user_info->display_name,
$group_title
);
// wp_mail( $user_info->user_email, $email_subject, $email_body );
// Note: wp_mail is commented out to prevent actual emails from being sent in this example.
// Uncomment and configure if you want to send emails.
// --- Example 2: Grant access to a specific post or course ---
// This is a hypothetical example. In a real scenario, you'd have specific logic
// to determine which content to grant access to based on the group.
// Let's assume there's a specific post ID we want to grant access to.
$special_content_post_id = 123; // Replace with a real post ID.
if ( 'Your_Specific_Group_Slug' === get_post_meta( $ld_group_id, 'group_slug', true ) ) { // Example condition
// Grant access to the special content. This would depend on how your system handles content access.
// For example, you might add the user to another meta field, a custom table,
// or trigger another LearnDash function if applicable.
// For demonstration, let's just log it.
// update_user_meta( $user_id, 'granted_access_to_content_' . $special_content_post_id, true );
error_log( "User {$user_id} added to group {$group_title} ({$ld_group_id}). Special content access potentially granted." );
}
// --- Example 3: Log the event for auditing ---
error_log( sprintf( 'User "%s" (ID: %d) was successfully added to group "%s" (ID: %d).', $user_info->display_name, $user_id, $group_title, $ld_group_id ) );
}
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:481
public function set_user_to_group( $user_id, $ld_group_id ) {
if ( empty( $user_id ) || empty( $ld_group_id ) ) {
return false;
}
ld_update_group_access( $user_id, $ld_group_id );
$transient_key = "learndash_user_groups_{$user_id}";
delete_transient( $transient_key );
do_action( 'ulgm_user_added_to_group', $user_id, $ld_group_id );
}