Filter uncanny-learndash-groups

ulgm_mail_attachment

Filters the email attachment data before it is sent, allowing modification of the attachment content or settings.

add_filter( 'ulgm_mail_attachment', $callback, 10, 1 );

Description

This filter allows developers to modify the email attachments before they are sent by the `ulgm_mail_attachment` hook. Developers can add, remove, or alter attachment details such as file paths or content. This hook is part of the core email sending functionality.


Usage

add_filter( 'ulgm_mail_attachment', 'your_function_name', 10, 1 );

Parameters

$attachment (mixed)
This parameter contains the attachment(s) to be included in the email.

Return Value

The filtered value.


Examples

add_filter( 'ulgm_mail_attachment', 'my_custom_ulgm_email_attachment', 10, 1 );

/**
 * Example function to add or modify email attachments for the ULGM mail function.
 *
 * This function demonstrates how to conditionally add a new attachment or
 * modify existing attachments based on certain criteria. In this specific
 * example, we'll add a placeholder PDF file if the attachment array is empty.
 *
 * @param array|string $attachment The current attachment(s) being passed to wp_mail.
 *                                This can be a file path or an array of file paths.
 * @return array|string The modified or original attachment(s).
 */
function my_custom_ulgm_email_attachment( $attachment ) {
    // Check if the attachment is currently empty.
    if ( empty( $attachment ) ) {
        // Define a dummy attachment path for demonstration.
        // In a real-world scenario, this path would point to an actual file.
        $new_attachment_path = WP_CONTENT_DIR . '/uploads/my-custom-attachment.pdf';

        // Ensure the file exists before adding it (optional but good practice).
        // For this example, we'll assume it exists or create a dummy if needed for testing.
        if ( ! file_exists( $new_attachment_path ) ) {
            // Create a dummy file for demonstration if it doesn't exist.
            // In a live site, you'd likely be adding an existing file.
            $dummy_file_content = "%PDF-1.0n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Count 0>>endobjnxrefn0 3n0000000000 65535 fn0000000010 00000 nn0000000053 00000 nntrailer<</Size 3/Root 1 0 R>>nstartxrefn102n%%EOF";
            file_put_contents( $new_attachment_path, $dummy_file_content );
        }

        // If $attachment is an array, append the new path.
        if ( is_array( $attachment ) ) {
            $attachment[] = $new_attachment_path;
        } else {
            // If $attachment is a single string or empty, make it an array with the new path.
            $attachment = array( $new_attachment_path );
        }

        // You could also modify existing attachments, for example:
        // if ( is_array( $attachment ) && ! empty( $attachment ) ) {
        //     foreach ( $attachment as $key => $file_path ) {
        //         // Example: Add a prefix to all attachment filenames
        //         $attachment[ $key ] = 'prefix_' . basename( $file_path );
        //     }
        // }
    }

    return $attachment;
}

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/shared-functions.php:919

public static function wp_mail( $to, $subject, $body, $headers = array(), $attachment = array() ) {
		$body       = do_shortcode( $body );
		$subject    = do_shortcode( $subject );
		$to         = apply_filters( 'ulgm_mail_to', $to );
		$subject    = apply_filters( 'ulgm_mail_subject', html_entity_decode( $subject ) );
		$body       = apply_filters( 'ulgm_mail_body', html_entity_decode( $body ) );
		$headers    = apply_filters( 'ulgm_mail_headers', $headers );
		$attachment = apply_filters( 'ulgm_mail_attachment', $attachment );

		return wp_mail( $to, $subject, $body, $headers, $attachment );
	}


Scroll to Top