uo_ld_expire_group_do_not_send_email_to_user
Filters whether to send an email to a user when a group expires.
add_filter( 'uo_ld_expire_group_do_not_send_email_to_user', $callback, 10, 3 );
Description
Filters whether to prevent sending an email to a user when their LearnDash group expires. Return `true` to stop the email. Useful for custom logic based on user, group, or progress. Note: this filter is applied *before* the email is sent.
Usage
add_filter( 'uo_ld_expire_group_do_not_send_email_to_user', 'your_function_name', 10, 3 );
Parameters
-
$user_id(mixed) - This parameter is a boolean value that defaults to `false` and can be used to prevent the email from being sent to the user.
-
$group_id(mixed) - This parameter contains the ID of the user who is being affected by the group expiration process.
-
$group_progress(mixed) - This parameter contains the ID of the group for which the email notification is being considered.
Return Value
The filtered value.
Examples
/**
* Prevent sending an expiration email to a specific user for a group.
*
* This filter allows you to conditionally stop an email from being sent to a user
* if certain criteria are met. For example, you might want to skip sending
* the email if the user has already completed all courses within the group,
* or if they are an administrator who should receive a different notification.
*
* @param bool $do_not_send_email Whether to prevent sending the email to the user. Default is false.
* @param int $user_id The ID of the user for whom the email is being considered.
* @param int $group_id The ID of the LearnDash group.
* @param array $group_progress An array containing the user's progress information for the group.
*
* @return bool True to prevent sending the email, false to allow it.
*/
add_filter(
'uo_ld_expire_group_do_not_send_email_to_user',
function ( $do_not_send_email, $user_id, $group_id, $group_progress ) {
// Example: Prevent sending email if the user is an administrator of the group.
if ( learndash_is_group_leader_of_group( $user_id, $group_id ) ) {
return true;
}
// Example: Prevent sending email if the user has completed all courses in the group.
// Assuming $group_progress contains an array like ['completed' => 100] or similar.
if ( ! empty( $group_progress['overall'] ) && $group_progress['overall']['percentage'] >= 100 ) {
return true;
}
// If none of the conditions are met, allow the email to be sent.
return $do_not_send_email;
},
10,
4
);
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:795
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 ) );
}