Posts in the developer's cookbook category describe topics that require technical knowledge. Be warned and use the techniques at your own risk. Unless you are an experienced developer, you are strongly advised to ask a professional for support.
A mail-tag is a formatted text you use in an email template. In addition to ordinary mail-tags (which are connected to a specific form field and are replaced with user input through that field), you can also use “special” mail-tags. A special mail-tag (SMT) is independent of any user input; rather, an SMT is often used to retrieve a value from the environment or context in which the submission belongs.
The current Contact Form 7 supports 26 types of SMT by default. On top of them, you can also implement your own custom SMTs with simple coding like the following:
add_filter(
'wpcf7_special_mail_tags',
function ( $output, $name, $html, $mail_tag = null ) {
if ( '_my_first_smt' === $name ) {
$output = "Hello, world!";
}
return $output;
},
10, 4
);
Once you have added the above code, put a mail-tag [_my_first_smt] into the message body of an email template. You’ll see “Hello, world!” in email messages where that mail-tag was.
The important points are:
- The tag name of an SMT must start with an underscore (
_). - For adding an SMT, use the
wpcf7_special_mail_tagsfilter hook. - The callback function will receive four parameters; the second one (
$name) contains the tag name.
For more details and tips on how to retrieve data from the environment or context, see includes/special-mail-tags.php to learn how Contact Form 7 defines the native SMTs.