Filter uncanny-learndash-groups

ulgm_maybe_send_group_email

Filters whether to send a group email, allowing modification of the email address and subject before sending.

add_filter( 'ulgm_maybe_send_group_email', $callback, 10, 2 );

Description

Filters whether to send a group email. Developers can use this to conditionally prevent emails based on user capabilities, GDPR compliance, or other custom logic. Defaults to true, allowing emails to be sent.


Usage

add_filter( 'ulgm_maybe_send_group_email', 'your_function_name', 10, 2 );

Parameters

$email_address (mixed)
This parameter likely indicates whether the email sending process should proceed, potentially based on some condition or permission.
$mail_args_subject (mixed)
This parameter represents the email address of the recipient.

Return Value

The filtered value.


Examples

/**
 * Prevent sending group emails to users who have opted out via GDPR settings.
 *
 * The filter 'ulgm_maybe_send_group_email' is intended to conditionally control
 * whether a group email should be sent to a specific user. This implementation
 * checks if the user's email address is present in a predefined list of
 * unsubscribed email addresses. If the user has unsubscribed, the email
 * should not be sent.
 *
 * @param bool $send_mail       The default decision to send the email (true).
 * @param string $email_address The email address of the recipient user.
 * @param string $mail_args_subject The subject of the email.
 * @return bool                 Returns false if the user has unsubscribed, true otherwise.
 */
add_filter( 'ulgm_maybe_send_group_email', function( $send_mail, $email_address, $mail_args_subject ) {
	// In a real scenario, this list would likely come from user meta,
	// a custom database table, or a plugin's settings.
	// For demonstration purposes, we'll use a hardcoded array.
	$unsubscribed_emails = array(
		'[email protected]',
		'[email protected]',
	);

	// Check if the current user's email address is in the unsubscribe list.
	if ( in_array( $email_address, $unsubscribed_emails, true ) ) {
		// If the email address is found in the unsubscribe list, prevent sending.
		return false;
	}

	// Otherwise, allow sending the email (as per the original default).
	return $send_mail;
}, 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/helpers/rest-api-end-points.php:860

$mail_args_subject    = apply_filters( 'ld_group_email_users_personalize_subject', $mail_args['subject'], $user_data, $group_email_data );
			$mail_args_message    = apply_filters( 'ld_group_email_users_personalize_message', $mail_args['message'], $user_data, $group_email_data );
			$ulgm_gdpr_compliance = apply_filters( 'ulgm_gdpr_is_group_leader_allowed', true, wp_get_current_user(), (int) $group_email_data['group_id'], $user_data, 'email-users' );

			if ( ! $ulgm_gdpr_compliance ) {
				$mail_errors[] = $email_address;
			} else {
				$send_mail = apply_filters( 'ulgm_maybe_send_group_email', true, $email_address, $mail_args_subject );
				if ( $send_mail ) {
					$mail_ret        = SharedFunctions::wp_mail(
						$email_address,
						$mail_args_subject,
						$mail_args_message,
						Group_Management_Helpers::get_headers( true, $group_leader_details, $group_id )
					);


Scroll to Top