Home / Admin / Sample Email Template Tag (copy)
Duplicate Snippet

Embed Snippet on Your Site

Sample Email Template Tag (copy)

This is an example of how to register a new email template tag for EDD as of version 3.3.0. Intended to be modified.

Code Preview
php
<?php
/**
 * Register a custom email tag for the file name.
 *
 * @return void
 */
function prefix_add_file_name_email_tag() {
	edd_add_email_tag(
		'file_name', // Tag
		'Displays the file name of the purchased download file.', // Description
		'prefix_render_file_name_email_tag', // Callback function
		'File Name', // Label
		array( 'order' ), // Context(s): using "order" here
		array( 'customer' ) // Recipients: for customer emails
	);
}
add_action( 'edd_add_email_tags', 'prefix_add_file_name_email_tag' );
/**
 * Render the custom email tag that outputs the file name.
 *
 * @param int    $email_object_id The email object ID (typically the payment ID).
 * @param mixed  $email_object    The email object (e.g., order/payment object).
 * @param object $email           The email instance, which contains the context.
 *
 * @return string
 */
function prefix_render_file_name_email_tag( $email_object_id, $email_object = null, $email = null ) {
	if ( 'order' === $email->context ) {
		// Retrieve the downloads for the order/payment.
		$downloads = edd_get_payment_meta( $email_object_id, '_edd_payment_downloads', true );
		if ( ! empty( $downloads ) && is_array( $downloads ) ) {
			// Assuming there's only one download; adjust if multiple files exist.
			$download = reset( $downloads );
			if ( isset( $download['file'] ) && ! empty( $download['file'] ) ) {
				// Return the file name extracted from the file URL.
				return basename( $download['file'] );
			}
		}
		return 'No file found.';
	}
	return 'This is not an order email.';
}

Comments

Add a Comment