Add a custom variable in a Email or PDF template.

Ever wondered how those {{custom variables}} in email or pdf templates get replaced by a dynamic variable value?

Just a quick run-down what a custom variable is. A custom variable is a placeholder where you want to add a username or email address dynamically to a template file or document. You might have noticed these predefined custom variables somewhere in a WordPress settings template page.

 

Example:

{{customer_username}}

{{customer_first_name}}

{{user_email}}

WooCommerce email example: https://docs.woocommerce.com/document/automated-follow-up-emails-docs/email-variables-and-merge-tags/

For this instance I assisted a customer who wanted to add the company name field to a Sensei certificate template. Going through the code of the Sensei certificate plugin which is a extension of the Sensei plugin I found this wonderful clear example and extended on it.:

The Sensei certificate plugin only provides the following custom variables.:

  • {{learner}} – The display name of the learner
  • {{course_title}} – The title of the course that has been completed
  • {{completion_date}} – The date the learner completed the course
  • {{course_place}} – The name of the site, taken from the WordPress Settings

Sensei certificate custom variables.: https://docs.woocommerce.com/document/sensei-certificates/#section-8

You will notice I added the {{Company_name}} custom variable to the array that replace it and populates it with the $company_name variable’s content.

As per the PHP manual.:

str_replaceReplace all occurrences of the search string with the replacement string. 

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. If you don’t need fancy replacing rules (like regular expressions).

What is does is to replace all occurrences of custom variables in a text string with the actual given value. In other words it will load the content string and scan the content string and replace all the marching {{custom variables}}, ‘markers’ with the dynamic value(s) of whatever it is at that stage. For example.:

{{name}} = John

{{company_name}} = CompanyX

$content = xxxxxxxxx {{name}} xxxxxxxxxx xxxxxx xxxxxxx {{company_name}} xxxxxxxxxxx

$content = xxxxxxxxx john xxxxxxxxxx xxxxxx xxxxxxx CompanyX xxxxxxxxxxx

The str_replace() functions takes on 3 parameters. 1st parameter(s), needle, (array) you define all the custom variable names it needs to look for like {{name}} for example. The 2nd parameter takes on the “replacement value” be that a static value or dynamic variable. The 3rd parameter is the haystack, the variable that needs to be search like the content string in this case.

That is how simple it is to extend on the custom variables of a email or pdf template. As the code is different from plugin-to-plugin I would suggest you first look for a hook that you can use or a template file (moved to the theme’s folder) to try and not modify the core plugin’s code directly as you do not want to lose these custom changes in the event of a update.