Overview
Would you like to create a custom Smart Tag to use inside your forms? If you’d like to provide, for example, a download link via a Smart Tag inside your email notification or a confirmation message, this can be easily done with a custom code snippet. In this tutorial, we’ll show you how to use PHP to create a custom Smart Tag.
Smart Tags offer a quick and easy way to pull specific information into a form field or notification email. WPForms comes with many built-in Smart Tags for you to use, and these can pull information like the current page’s URL or a logged-in user’s email address, etc. For more information on the Smart Tags that are pre-defined within WPForms, please review this documentation.
With a small custom code added to your site you can design a custom Smart Tag to pull even more useful information.
Adding the snippet
In the example below, we’re going to create a new custom Smart Tag to display a URL for a download link when our form is completed. First, we’ll create the Smart Tag and second we’ll assign, in the snippet, what to do when that Smart Tag is used.
To begin, just copy and paste this snippet to your site. If you’re not sure where or how to add snippets to your site, please check out this tutorial.
/** * Register the Smart Tag so it will be available to select in the form builder. * * @link https://wpforms.com/developers/how-to-create-a-custom-smart-tag/ */ 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', 10, 1 ); /** * Process the Smart Tag. * * @link https://wpforms.com/developers/how-to-create-a-custom-smart-tag/ */ function wpf_dev_process_smarttag( $content, $tag ) { // Only run if it is our desired tag. if ( 'download_link' === $tag ) { // Replace our link in this demo with the URL you wish to provide $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 );
Smart Tag names can contain lowercase Latin letters, digits, and an underscore only.
Please remember to include both of the PHP functions as shown above.
The first function creates the Smart Tag and 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
statement, you would need to add any code needed to pull the value(s) you’d like this Smart Tag to display.
Creating the form
Next, we’re going to create a simple newsletter form. If they sign up for our newsletter, we want to include a free link to an eBook they can use. This is a great example of utilizing the power of WPForms Smart Tags. Once you create this Smart Tag, you can include this Smart Tag in any form you create going forward without further code changes.
For any assistance in how to create a form, please review this documentation.
Using the new custom Smart Tag
Now it’s time to use this new custom Smart Tag we created. For the purpose of this tutorial, we’re going to add this Smart Tag for our Download Link to our email Notifications.
From inside the form builder, click Settings and then select Notifications. Once there, scroll down to the Email Message box and click the Show Smart Tags link and just scroll through until we find our Download Link Smart Tag. Once you select this, it will be added to the notification.
And that’s it! You’ve successfully created a new custom Smart Tag. Would you like to be able to process that custom Smart Tag inside an HTML form field? If so, take a look at our article on How to Process Smart Tags in HTML Fields.
Related
Filter References: