Action uncanny-learndash-groups

ulgm_group_leader_added

Fires after a new group leader is successfully added to a LearnDash group, passing user, group, and order data.

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

Description

Fires after a group leader is successfully added. Developers can use this hook to perform custom actions such as sending notifications, updating related user meta, or logging the addition. It provides the added user's data, the group ID, and the associated order ID.


Usage

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

Parameters

$user_data (mixed)
This parameter contains the user data for the leader being added to the group.
$ld_group_id (mixed)
This parameter contains an array of user data for the user being added as a group leader.
$order_id (mixed)
This parameter contains the ID of the LearnDash group to which the leader is being added.

Examples

This example demonstrates how to hook into the `ulgm_group_leader_added` action to send a custom notification email to the newly added group leader.

add_action( 'ulgm_group_leader_added', 'my_custom_group_leader_notification', 10, 3 );

/**
 * Sends a custom welcome email to a newly added group leader.
 *
 * @param array $user_data   An array containing the new group leader's data (e.g., user_email, user_nicename, user_login).
 * @param int   $ld_group_id The ID of the LearnDash group to which the leader was added.
 * @param int   $order_id    The ID of the order associated with the group, if applicable.
 */
function my_custom_group_leader_notification( $user_data, $ld_group_id, $order_id ) {

    // Ensure we have valid data before proceeding.
    if ( ! empty( $user_data['user_email'] ) && is_numeric( $ld_group_id ) ) {

        $user_email = $user_data['user_email'];
        $group      = learndash_get_group( $ld_group_id ); // Assuming LearnDash groups are used.

        if ( $group instanceof WP_Post && 'groups' === $group->post_type ) {

            $group_name = $group->post_title;

            // Construct the email subject and body.
            $subject = sprintf(
                __( 'Welcome as Group Leader for "%s"', 'your-text-domain' ),
                $group_name
            );

            $message = sprintf(
                __( 'Dear %s,

You have been assigned as a Group Leader for the group "%s".

You can now manage members and access relevant information for this group.

Thank you,
Your Website Team', 'your-text-domain' ),
                $user_data['user_nicename'] ?? $user_data['user_login'], // Fallback to login if nicename is not available
                $group_name
            );

            // Set email headers.
            $headers = array( 'Content-Type: text/html; charset=UTF-8' );

            // Send the email.
            $sent = wp_mail( $user_email, $subject, $message, $headers );

            if ( ! $sent ) {
                error_log( sprintf( 'Failed to send custom welcome email to group leader %s for group ID %d.', $user_email, $ld_group_id ) );
            }
        } else {
            error_log( sprintf( 'Could not retrieve group data for group ID %d when sending group leader notification.', $ld_group_id ) );
        }
    } else {
        error_log( 'Invalid data received for ulgm_group_leader_added hook.' );
    }
}

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/helpers/rest-api-end-points.php:1372

public static function add_group_leader( WP_REST_Request $request ) {

		// Actions permitted by the pi call (collected from input element with name action )
		$permitted_actions = array( 'add-leader' );

		// Was an action received, and is the actions allowed
		if ( $request->has_param( 'action' ) && in_array( $request->get_param( 'action' ), $permitted_actions ) ) {

			$action = (string) $request->get_param( 'action' );

		} else {
			$action          = '';
			$data['message'] = __( 'Select an action.', 'uncanny-learndash-groups' );
			wp_send_json_error( $data );
		}

		// Does the current user have permission
		$permission = apply_filters( 'group_management_add_group_leader_permission', 'group_leader' );
		if ( ! current_user_can( $permission ) && ! current_user_can( 'manage_options' ) && ! current_user_can( 'ulgm_group_management' ) ) {
			$data['message'] = __( 'You do not have permission to add group leaders.', 'uncanny-learndash-groups' );
			wp_send_json_error( $data );
		}

		// If a single part of data doesn't validate, it dies and sends back the validation error
		$user_data   = self::validate_new_user_data( $action, $request );
		$ld_group_id = (int) $request->get_param( 'group-id' );
		$order_id    = SharedFunctions::get_order_id_from_group_id( $ld_group_id );
		//returns true or false if group leader allowed to add user
		//wp_get_current_user() = (object) of current logged in user details
		//(int) $request->get_param( 'group-id' ) = (int) LD Group ID
		//$user_data = (object) of user being added to group as group leader
		$ulgm_gdpr_compliance = apply_filters( 'ulgm_gdpr_is_group_leader_allowed', true, wp_get_current_user(), $ld_group_id, (object) $user_data, $request->get_param( 'action' ) );
		// Add group leader and send out welcome email
		if ( $ulgm_gdpr_compliance && 'add-leader' === $action ) {
			// Add the user and send out a welcome email NOTE group id has already been validate and the script would return error so its safe :)
			$data = Group_Management_Helpers::create_group_leader( $user_data, $ld_group_id, false );
			if ( ! key_exists( 'error', $data ) ) {
				if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {
					$user = get_user_by( 'email', $user_data['user_email'] );
					if ( 'no' === ulgm()->group_management->is_user_already_member_of_group( $user->ID, $ld_group_id ) ) {
						$user_data['user_id'] = $user->ID;

						//if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {
						Group_Management_Helpers::add_existing_user( $user_data, true, $ld_group_id, $order_id, SharedFunctions::$redeem_status, false, false, false, true );
						//}

						SharedFunctions::delete_transient( null, $ld_group_id );
					}
				}

				do_action( 'ulgm_group_leader_added', $user_data, $ld_group_id, $order_id );

				wp_send_json_success( $data );
			} else {
				wp_send_json_error( $data );
			}
		}
	}


Scroll to Top