How to Create a Smart Tag Site URL

Introduction

Would you like to create a Smart Tag Site URL? Perhaps you’d like to re-use this Smart Tag in your email notifications or confirmation messages? In this tutorial, we’ll show you how you can create a Smart Tag for your site URL but also how and where to place it inside your notifications.

Adding the Snippet

To begin, we’re going to add our snippet to the site first. If you need any help with how and where to add snippets, please be sure to review this helpful documentation.

/**
 * Process the Smart Tag.
 *
 * @link   https://wpforms.com/developers/how-to-create-a-smart-tag-for-site-url/
 */

function wpf_dev_register_site_smarttag( $tags ) {

    // Key is the tag, item is the tag name.
    $tags[ 'site_url' ] = 'Site URL';

    return $tags;
}
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_site_smarttag', 10, 1 );

/**
 * Process the Smart Tag.
 *
 * @link   https://wpforms.com/developers/how-to-create-a-smart-tag-for-site-url/
 */

function wpf_dev_process_site_url_smarttag( $content, $tag ) {

    // Only run if it is our desired tag.
    if ( 'site_url' === $tag ) {

        // Assign the site URL to the $url variable
        $url = get_site_url();

        // Replace the tag with our link.
        $content = str_replace( '{site_url}', $url, $content );

    }

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

This snippet has two functions. The first is just registering that we’re going to create a Smart Tag called site_url however, the text we’ll see inside the form builder for this particular Smart Tag is Site URL.

The second function will look for the current site URL with the WordPress function get_site_url(); and then we’ll pass that inside the function and assign the $url variable inside the Smart Tag.

Using the Smart Tag

For the purpose of this documentation, we’re placing a thank you image inside each email notification that our visitors receive after completing our form. To do this, we’ll head to the Notifications tab and add this after the {all_fields} Smart tag in our notification message.

Just add your HTML link and image source to your Email Message on your Notifications tab.

<a href="{site_url}" class="form_footer_logo"><img src="{site_url}/my-image.jpg" /></a>

use the new smart tag to add an image

And that’s it! There is so much you can do with Smart Tags in WPForms. Check out our tutorial on How to Create a Custom Smart Tag to start creating your own custom Smart Tags.

FAQ

Q: What if I wanted to use this inside an HTML form field or Label?

A: Smart Tags can be used in many areas by default such as confirmation messages, email notifications, Default Value for certain form fields. However, if you wanted to use Smart Tags inside field labels or in an HTML form field, there will be an extra snippet you may need to add.

<a href="{site_url}" class="form_footer_logo"><img src="{site_url}/my-image.jpg" /></a>

create a smart tag site URL with this snippet

For further information on the snippet needed to process Smart Tags inside an HTML form field, please review this tutorial.

If you’d like to process Smart Tags inside a field label, be sure to check out this tutorial.

Did you know that you can also use Smart Tags as options for your Checkbox field? You can see that tutorial by reading this documentation.

Filter References:

FAQ

Q: Is it possible to truncate the URL?

A: Absolutely! If you want to use the permalink of the current post or page to populate a field, you can utilize this snippet instead.

/**
 * Process the Smart Tag.
 *
 * @link   https://wpforms.com/developers/how-to-create-a-smart-tag-for-site-url/
 */
 
function wpf_dev_register_site_smarttag( $tags ) {
 
    // Key is the tag, item is the tag name.
    $tags[ 'permalink' ] = 'Permalink';
 
    return $tags;
}
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_site_smarttag', 10, 1 );
 
/**
 * Process the Smart Tag.
 *
 * @link   https://wpforms.com/developers/how-to-create-a-smart-tag-for-site-url/
 */
 
function wpf_dev_process_site_url_smarttag( $content, $tag ) {
 
    // Only run if it is our desired tag.
    if ( 'permalink' === $tag ) {
 
        // Assign the site URL to the $url variable 
	$url = substr( get_permalink(), strlen( home_url('/') ) );
		
        // Replace the tag with our link.
        $content = str_replace( '{permalink}', $url, $content );
 
    }
 
    return $content;
}
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_site_url_smarttag', 10, 2 );