learndash_notifications_email_content
Filters the content of an email notification before it is sent. This hook allows modification of the email's body.
add_filter( 'learndash_notifications_email_content', $callback, 10, 2 );
Description
Fires when the email content for a LearnDash notification is being generated. Developers can use this filter to modify or customize the email body before it's sent. This hook provides access to the notification object and the current email content.
Usage
add_filter( 'learndash_notifications_email_content', 'your_function_name', 10, 2 );
Parameters
-
$content(mixed) - This parameter contains the email content that will be sent, and it can be modified by the filter.
-
$notification(mixed) - This parameter contains the email content that will be filtered, allowing for modifications to the message's body before it's sent.
Return Value
The filtered value.
Examples
/**
* Modifies the email content for LearnDash notifications.
*
* This example adds a custom footer to all LearnDash notification emails.
*
* @param string $content The current email content.
* @param WP_Post $notification The notification post object.
*
* @return string The modified email content.
*/
add_filter(
'learndash_notifications_email_content',
function( $content, $notification ) {
// Ensure we're dealing with a string content, otherwise return as is.
if ( ! is_string( $content ) ) {
return $content;
}
// Append a custom footer with the notification title.
$custom_footer = '<p><strong>Note:</strong> This is an automated notification related to the "' . esc_html( $notification->post_title ) . '" course content.</p>';
$content .= $custom_footer;
return $content;
},
10, // Priority
2 // Number of accepted arguments
);
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/uncanny-drip-topics-by-group.php:1658
src/classes/uncanny-drip-lessons-by-group.php:1667
public static function send( $emails, $notification, $args ) {
global $ld_notifications_shortcode_data;
$args['notification_id'] = $notification->ID;
$ld_notifications_shortcode_data = $args;
$subject = apply_filters( 'learndash_notifications_email_subject', do_shortcode( $notification->post_title ), $notification->ID );
$content = do_shortcode( $notification->post_content );
$content = wpautop( $content );
if ( ! strstr( $content, '<!DOCTYPE' ) && ! strstr( $content, '<p' ) && ! strstr( $content, '<div' ) ) {
$content = wpautop( $content );
}
$content = trim( $content );
$content = apply_filters( 'learndash_notifications_email_content', $content, $notification->ID );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
self::notification_log( sprintf( __( 'About to send the email to %s, data: %s', 'uncanny-pro-toolkit' ), join( PHP_EOL, $emails ), self::array_to_text( $args ) ) );
foreach ( $emails as $email ) {
$user = get_user_by( 'email', $email );
$is_send = true;
if ( is_object( $user ) ) {
//check the subscription
$list = get_user_meta( $user->ID, 'learndash_notifications_subscription', true );
if ( isset( $list[ self::$trigger_name ] ) && absint( $list[ self::$trigger_name ] ) === 0 ) {
self::notification_log( sprintf( __( 'Email %s excluded', 'uncanny-pro-toolkit' ), $email ) );
$is_send = false;
}
}
if ( $is_send ) {
add_action( 'wp_mail_failed', [ __CLASS__, 'debug_email_fail' ] );
$ret = wp_mail( $email, $subject, $content, $headers );
self::notification_log( sprintf( __( 'Send to %s. Status: %s', 'uncanny-pro-toolkit' ), $email, $ret === true ? __( 'sent', 'uncanny-pro-toolkit' ) : __( 'fail', 'uncanny-pro-toolkit' ) ) );
remove_action( 'wp_mail_failed', [ __CLASS__, 'debug_email_fail' ] );
}
}
}