ld_group_email_users_personalize_message
Filters the arguments for personalizing group email messages sent to users.
add_filter( 'ld_group_email_users_personalize_message', $callback, 10, 3 );
Description
Filters the message body for emails sent to group users. Allows modification of the email content, user data, and group email details before sending. Useful for tailoring messages to individual users or adding dynamic content based on group context.
Usage
add_filter( 'ld_group_email_users_personalize_message', 'your_function_name', 10, 3 );
Parameters
-
$mail_args(mixed) - This parameter contains an array of arguments used for sending the email, including the subject and message body.
-
$user_data(mixed) - This parameter contains an array of arguments related to the email being sent, including subject and message content.
-
$group_email_data(mixed) - This parameter contains data about the specific user who will receive the email.
Return Value
The filtered value.
Examples
add_filter(
'ld_group_email_users_personalize_message',
'my_custom_group_email_message',
10,
3
);
/**
* Customizes the email message sent to users within a LearnDash group.
*
* This function allows developers to dynamically alter the message content based on
* user data and group email details. For example, it could add a personalized
* greeting or a specific call to action.
*
* @param string $message The original email message.
* @param array $user_data An array containing the data of the recipient user.
* Expected keys include 'email', 'display_name', etc.
* @param array $group_email_data An array containing the data of the group email being sent.
* Expected keys include 'group_id', 'subject', 'message', etc.
* @return string The modified email message.
*/
function my_custom_group_email_message( $message, $user_data, $group_email_data ) {
// Ensure we have valid user and group data before proceeding.
if ( ! empty( $user_data['display_name'] ) && ! empty( $group_email_data['group_id'] ) ) {
// Construct a more personalized greeting for the user.
$greeting = sprintf(
__( 'Hi %s,', 'my-text-domain' ),
esc_html( $user_data['display_name'] )
);
// Append the custom greeting to the beginning of the original message.
$modified_message = $greeting . "nn" . $message;
// Example: Add a specific message if the user is part of a particular group.
if ( (int) $group_email_data['group_id'] === 123 ) {
$specific_message = sprintf(
__( 'As a member of our special %s, we have an exclusive update for you!', 'my-text-domain' ),
esc_html__( 'VIP Group', 'my-text-domain' )
);
$modified_message .= "nn" . $specific_message;
}
// Return the modified message.
return $modified_message;
}
// If data is insufficient or invalid, return the original message.
return $message;
}
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:854
/**
* New filters added for email subject and body.
*
* @since 3.5.1
*/
$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 ) {