Filter uncanny-toolkit-pro

learndash_notifications_email_subject

Filters the email subject line for LearnDash notifications before it is sent.

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

Description

Filters the email subject for LearnDash notifications. Developers can modify the default subject, which is derived from the notification's post title, before it's sent. This allows for dynamic or customized subject lines based on notification content or recipient data.


Usage

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

Parameters

$notification (mixed)
This parameter contains the notification object itself, providing access to its properties and data.
$notification (mixed)
This parameter represents the notification object itself, containing details about the notification being processed.

Return Value

The filtered value.


Examples

/**
 * Add a prefix to the LearnDash notification email subject.
 *
 * This function demonstrates how to hook into the 'learndash_notifications_email_subject'
 * filter to modify the subject line of emails sent by LearnDash notifications.
 * In this example, we'll prepend a custom string to the default subject.
 *
 * @param string     $subject      The original email subject line, potentially with shortcodes processed.
 * @param int        $notification_id The ID of the notification post.
 * @return string The modified email subject line.
 */
function my_learndash_custom_email_subject( $subject, $notification_id ) {
    // You can conditionally modify the subject based on the notification ID or other factors.
    // For demonstration, we'll always add a prefix.
    $custom_prefix = '[My Course Update] ';

    // Ensure the prefix is not already present to avoid duplicates if the filter is applied multiple times.
    if ( strpos( $subject, $custom_prefix ) === false ) {
        $subject = $custom_prefix . $subject;
    }

    return $subject;
}
add_filter( 'learndash_notifications_email_subject', 'my_learndash_custom_email_subject', 10, 2 );

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:1651
src/classes/uncanny-drip-lessons-by-group.php:1660

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' ] );
			}
		}
	}


Scroll to Top