uo_ld_expire_group_email_headers
Filters group email headers before expiring group content, allowing modification of headers.
add_filter( 'uo_ld_expire_group_email_headers', $callback, 10, 1 );
Description
Filters the email headers for LearnDash group expiration notifications. Developers can modify these headers, such as adding custom ones or changing existing ones, before the email is sent. This hook fires when the group expiration email is being prepared.
Usage
add_filter( 'uo_ld_expire_group_email_headers', 'your_function_name', 10, 1 );
Parameters
-
$headers(mixed) - This parameter contains an array of email headers, which can be filtered by other plugins or themes.
Return Value
The filtered value.
Examples
add_filter( 'uo_ld_expire_group_email_headers', 'my_custom_group_email_headers', 10, 1 );
/**
* Customizes the email headers for LearnDash group expiration notifications.
*
* This function allows developers to modify the email headers for notifications
* sent when a LearnDash group is about to expire. For example, you might want
* to add a custom 'X-Mailer' header or change the 'From' address.
*
* @param array $headers An array of email headers.
* @return array The modified array of email headers.
*/
function my_custom_group_email_headers( $headers ) {
// Add a custom header to identify the sender's application.
$headers[] = 'X-Mailer: My Awesome WordPress Site';
// Optionally, change the 'From' address for a specific group expiration email.
// This assumes you have a way to identify the specific group or context
// if you wanted to make this conditional. For this example, we'll keep it global.
// If you needed to, you could pass the group_id or other relevant data
// if the filter provided it or if you stored it globally/statically.
// For demonstration purposes, let's pretend we are sending from a different email.
// In a real scenario, ensure this email address is configured and valid.
// $headers[] = 'From: Group Notifications <[email protected]>';
// You can also remove existing headers if needed, though this is less common.
// For example, to remove the default 'Content-type' header:
// foreach ( $headers as $key => $header ) {
// if ( stripos( $header, 'Content-type' ) !== false ) {
// unset( $headers[$key] );
// }
// }
// $headers = array_values( $headers ); // Re-index array after removal.
return $headers;
}
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/learn-dash-group-expiration.php:792
public static function email_group( $group_id ) {
if ( 'yes' === self::$send_email && function_exists( 'learndash_group_enrolled_courses' ) ) {
if ( 'on' === self::$send_email_group_leader_only ) {
$user_ids = learndash_get_groups_administrator_ids( $group_id );
$user_ids = array_unique( $user_ids );
} else {
$user_ids = array_merge( learndash_get_groups_user_ids( $group_id ), learndash_get_groups_administrator_ids( $group_id ) );
$user_ids = array_unique( $user_ids );
}
if ( $user_ids ) {
$email_title = self::get_email_title( $group_id );
$email_body = self::get_email_body( $group_id );
$expiration_date = get_post_meta( $group_id, 'uo-expiration-date', true );
$group_name = get_the_title( $group_id );
$email_body = str_ireplace(
array(
'%LearnDash Group Name%',
'%expiration date%',
),
array(
$group_name,
$expiration_date,
),
$email_body
);
$headers = array();
$headers[] = 'Content-type: text/html; charset=UTF-8';
$headers[] = 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>';
$headers = apply_filters( 'uo_ld_expire_group_email_headers', $headers );
foreach ( $user_ids as $user_id ) {
$group_progress = learndash_get_user_group_progress( $group_id, $user_id );
if ( true === apply_filters( 'uo_ld_expire_group_do_not_send_email_to_user', false, $user_id, $group_id, $group_progress ) ) {
continue;
}
$user = get_userdata( $user_id );
$email = $user->user_email;
$user_name = $user->display_name;
$message = str_ireplace( '%display name%', $user_name, $email_body );
$message = nl2br( stripcslashes( $message ) );
$message = wpautop( $message );
$message = apply_filters( 'uo_ld_expire_group_email_message', $message, $user, $group_id );
$sub = apply_filters( 'uo_ld_expire_group_email_subject', $email_title, $user, $group_id );
wp_mail( $email, $sub, $message, $headers );
}
}
}
update_post_meta( $group_id, 'uo-email-group-sent', current_time( get_option( 'date_format' ) . ' H:i:s' ) );
wp_clear_scheduled_hook( 'uo-email-group', array( $group_id ) );
}