Action uncanny-learndash-groups

ld_group_email_users_before

Fires before sending emails to users in a LearnDash group, allowing modification of email arguments.

add_action( 'ld_group_email_users_before', $callback, 10, 1 );

Description

Fires before an email is sent to group users, allowing modification of email arguments like subject, body, and recipient data. Developers can intercept and alter these parameters to customize email content or recipient lists before the sending process begins. This hook provides an opportunity for advanced integration and personalized communication within Learndash groups.


Usage

add_action( 'ld_group_email_users_before', 'your_function_name', 10, 1 );

Parameters

$mail_args (mixed)
This parameter contains an array of arguments intended for sending emails to group users.

Examples

<?php
/**
 * Example of using the 'ld_group_email_users_before' action hook.
 *
 * This example demonstrates how to intercept the email arguments before users
 * are emailed for a LearnDash group. It might be used to log the email attempt
 * or modify the email arguments (though modifying arguments is better done
 * with a filter if available and more appropriate).
 *
 * @param array $mail_args An array containing arguments for the email being sent.
 *                         The exact structure depends on the LearnDash Groups plugin
 *                         but might include keys like 'subject', 'message', 'to', etc.
 */
function my_learndash_group_email_logging( $mail_args ) {
	// Check if $mail_args is actually an array and has expected keys.
	if ( ! is_array( $mail_args ) || ! isset( $mail_args['subject'] ) || ! isset( $mail_args['message'] ) ) {
		// Log an error or handle unexpected mail_args structure if necessary.
		error_log( 'Unexpected structure for $mail_args in ld_group_email_users_before hook.' );
		return;
	}

	// Get the current user and group information if available in $mail_args or context.
	// This is a hypothetical example, the actual data available in $mail_args will depend
	// on how the LearnDash Groups plugin populates it.
	$current_user_id = get_current_user_id();
	$group_id        = isset( $mail_args['group_id'] ) ? $mail_args['group_id'] : 0; // Assuming group_id might be passed.
	$email_subject   = sanitize_text_field( $mail_args['subject'] );
	$email_message   = wp_strip_all_tags( $mail_args['message'] ); // Strip tags for logging.

	// Construct a log message.
	$log_message = sprintf(
		'User ID %d is about to send an email for Group ID %d. Subject: "%s".',
		$current_user_id,
		$group_id,
		$email_subject
	);

	// You could also log the message content if it's not too sensitive.
	// $log_message .= ' Message: "' . substr( $email_message, 0, 100 ) . '..."';

	// Log to the WordPress debug log.
	error_log( $log_message );

	// You could also save this information to a custom database table
	// or send a notification to an administrator.
}

// Hook into the action, specifying the function to call and the number of arguments it accepts.
// The 'ld_group_email_users_before' hook passes one argument: $mail_args.
add_action( 'ld_group_email_users_before', 'my_learndash_group_email_logging', 10, 1 );

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

wp_send_json_error(
				array(
					'message' => __( 'Mail args empty. Unexpected condition from filter: ld_group_email_users_args.', 'uncanny-learndash-groups' ),
				)
			);
		}

		do_action( 'ld_group_email_users_before', $mail_args );

		foreach ( $email_addresses as $k => $email_address ) {

			if ( isset( $not_enrolled_users[ $email_address ] ) ) {
				$user_data = $not_enrolled_users[ $email_address ];
			} else {
				$user_data = get_user_by( 'email', $email_address );


Scroll to Top