### [How to Create and Use the User-Agent Smart Tag](https://wpforms.com/developers/how-to-create-and-use-the-user-agent-smart-tag/)

**Published:** April 23, 2021
**Author:** Editorial Team

**Excerpt:** This tutorial will help you create a Smart Tag that can be used to capture user-agent data and attach it to your form entires. 

**Content:**

## Introduction

Would you like to create a user-agent Smart Tag to know where you’re visitors are coming from? This data can make future decisions for your website super easy. In this tutorial, we’re going to create a Smart Tag that will automatically fill in a **Hidden Field** on your form to capture where you’re submissions are coming from.

A [User-Agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent "User-Agent") is information that you can retrieve from the browser to see what browser, operating system, device used (desktop/mobile) etc.

## Creating the User-Agent Smart Tag

First, we’re going to add the code snippet that will create a new Smart Tag inside WPForms that we can use in the builder.

If you need any help adding code snippets to your site, [please see this tutorial](https://wpforms.com/developers/how-to-add-custom-php-or-javascript-for-wpforms/ "How to Add Custom PHP or JavaScript for WPForms").

```

/** 
 * Register the User-Agent Smart Tag so it will be available to select in the form builder.
 *
 * @link   https://wpforms.com/developers/how-to-create-and-use-the-user-agent-smart-tag/
 */
 
//Create the Smart Tag to be used
function wpf_dev_register_smarttag( $tags ) {
 
    $tags[ 'user_agent' ] = 'User Agent';
   
    return $tags;
}
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
 
//Define what the Smart Tag is
function wpf_dev_custom_smarttags( $content, $tag ) {
 
    $user_agent = ! empty( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? substr( $_SERVER[ 'HTTP_USER_AGENT' ], 0, 256 ) : '';
 
   if ( $tag === 'user_agent' ) {
      $content = str_replace( '{' . $tag . '}', $user_agent, $content );
   }
   return $content;
};
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_custom_smarttags', 10, 2 );

```

In the snippet, you’ll notice there are two different functions. Each has been commented on so that you can understand what they are. The first function will create the Smart Tag, this is only the creation, however, it’s only in the second function that we define what the Smart Tag is which in this case is to pull in the **HTTP\_USER\_AGENT**.

## Creating the form

Next, we’re going to create a new form so that we can add this new Smart Tag. If you need any assistance in creating a new form, [please review this documentation](https://wpforms.com/docs/creating-first-form/ "How to Create Your First Form").

Our form is just going to have a few fields on it for the **Name**, and **Email Address** and then we’ll add a **Hidden Field** that will hold the information we capture with the user-agent Smart Tag.

![begin by creating a new form and adding your fields to it which will include at least one hidden field to store the user-agent data in for the entry](https://wpforms.com/wp-content/uploads/2022/07/wpforms-user-agent-create-form.jpg)

## Using the User-Agent Smart Tag

Once you’ve added your **Hidden Field**, click the **Show Smart Tags** link to open up a list of Smart Tags you can add inside the **Default Value** field.

Find the **User Agent** Smart Tag and select it to insert it on the **Default Value**.

![Add the user-agent Smart Tag to the Default Value of the hidden field](https://wpforms.com/wp-content/uploads/2022/07/wpforms-insert-user-agent-smart-tag.jpg)

## Viewing the User-Agent

When you view your entries, you’ll see inside the **Hidden Field** all the user information that was captured.

![You can now view the user-agent data captured inside the hidden field when you view the entry.](https://wpforms.com/wp-content/uploads/2022/07/wpforms-user-agent-entry.jpg)

And that’s all you need! Would you like to create more custom Smart Tags? Take a look at our article on [How to Create a Custom Smart Tag](https://wpforms.com/developers/how-to-create-a-custom-smart-tag/ "How to Create a Custom Smart Tag").

## Related

Filter References:

- [wpforms\_smart\_tags](https://wpforms.com/developers/wpforms_smart_tags/ "Using the wpforms_smart_tags filter")
- [wpforms\_smart\_tag\_process](https://wpforms.com/developers/wpforms_smart_tag_process/ "Using the wpforms_smart_tag_process filter")

**Categories:** Tutorials

**Tags:** PHP, Smart Tag

---

