1. Home
  2. Snippets
  3. Create a Custom Smart Tag

Create a Custom Smart Tag

Heads up!

This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Dismiss

Smart Tags offer a quick and easy way to pull specific information into a form field or notification email. WPForms comes with many Smart Tags, and these can pull information like the current page’s URL or a logged in user’s email address.

With a little custom code, you can design a custom Smart Tag to pull even more useful information.

In the example below, a Smart Tag is created to display a URL for a download link.

<?php
/**
 * Register the Smart Tag so it will be available to select in the form builder.
 *
 * @param array $tags
 * @return array
 */
function wpf_dev_register_smarttag( $tags ) {

	// Key is the tag, item is the tag name.
	$tags['download_link'] = 'Download Link';

	return $tags;
}
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );

/**
 * Process the Smart Tag.
 *
 * @param string $content
 * @param string $tag
 * @return string
 */
function wpf_dev_process_smarttag( $content, $tag ) {

	// Only run if it is our desired tag.
	if ( 'download_link' === $tag ) {
		$link    = 'https://yoursite.com/file.pdf';
		// Replace the tag with our link.
		$content = str_replace( '{download_link}', $link, $content );
	}

	return $content;
}
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );

It’s important to include both of the PHP functions shown above.

The first function creates the Smart Tag. This will require only basic name changes to be customized for any other Smart Tag.

The second function is where the bulk of the code will go. Inside of the if, you would need to add any code needed to pull the value(s) you’d like this Smart Tag to display.

After saving the code and refreshing the form builder, your custom Smart Tag(s) will be added to the bottom of the existing Smart Tag options list.

Add custom Smart Tag to a form notification

Updated on November 20, 2017

Was this article helpful?