Home / Admin / Sample Email Template Tag
Duplicate Snippet

Embed Snippet on Your Site

Sample Email Template Tag

This is an example of how to register a new email template tag for EDD.

Code Preview
php
<?php
/**
 * Register a custom email tag.
 *
 * Uses edd_add_email_tag(). See includes/emails/tags.php for full parameter documentation.
 *
 * @return void
 */
function eddwp_add_sample_email_tag() {
	edd_add_email_tag(
		'custom_email_tag',
		__( 'This tag will display the currency of the order.', 'your-text-domain' ),
		'eddwp_render_sample_email_tag',
		__( 'Custom Email Tag', 'your-text-domain' ),
		array( 'order' ),
		array( 'customer' )
	);
}
add_action( 'edd_add_email_tags', 'eddwp_add_sample_email_tag' );
/**
 * Render the custom email tag.
 *
 * Callback signature matches EDD core: ( object_id, email_object, context_or_email ).
 * The third parameter is either \EDD\Emails\Types\Email or the context string; use get_context() to normalize.
 *
 * @param int                            $object_id        The object ID (e.g. order ID).
 * @param mixed|null                     $email_object     The email object (order, user, etc.). May be null.
 * @param string|\EDD\Emails\Types\Email $context_or_email The context string ( order, license, user, etc.) or the email type instance.
 * @return string
 */
function eddwp_render_sample_email_tag( $object_id, $email_object = null, $context_or_email = 'order' ) {
	$context = EDD()->email_tags->get_context( $context_or_email );
	if ( 'order' !== $context ) {
		return __( 'This is not an order email.', 'your-text-domain' );
	}
	if ( ! $email_object instanceof \EDD\Orders\Order ) {
		$email_object = edd_get_order( $object_id );
	}
	if ( ! $email_object instanceof \EDD\Orders\Order ) {
		return '';
	}
	return $email_object->currency;
}

Comments

Add a Comment