How to Process Smart Tags in Field Labels

Overview

Would you like to process Smart Tags on field labels in your WPForms? With a PHP snippet, this can be easily achieved. This tutorial will show you how to process Smart Tags in your form field labels.

Smart Tags are a useful tool to dynamically pull and display specific data within your form.

By default, Smart Tags will not be processed if placed within any field labels on your form.

However, this functionality may be useful if, for example, you’d like to show a logged-in user name, your page URL or even to display other details using custom Smart Tags.

Implementation options

There are a couple of different implementation options for the code snippet we’ll share.

For both options, you’ll need to open your form builder and in the Label field, enter the Smart Tag you wish to use.

For the purpose of this tutorial, we’ll add the {user_first_name} Smart Tag to our form as this is a member’s only form.

Add your Smart Tag to the field label

Apply Code to a Specific Form ID

Next, copy the code shown below into your site.

/**
 * Run smart tags on all field labels.
 *
 * @link   https://wpforms.com/developers/how-to-process-smart-tags-in-field-labels/
 *
 * @param  array $properties
 * @param  array $field
 * @param  array $form_data
 */
 
function wpf_dev_textarea_field_display( $field, $field_atts, $form_data ) {
    if( $form_data[ 'id' ] != 365 ) 
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_textarea_field_display', 'wpf_dev_textarea_field_display', 10, 3 );

Please note, in the code shown above, we’re only targeting the form ID 365. You’ll need to update that form ID number to reflect the form ID you’re using.

You can now set up some Smart Tags in the field label and it will also be included in your notification emails.

The Smart Tag now shows inside the field label

Apply Code for All WPForms

The code shown below would allow this on all your WPForms.

/**
 * Run Smart Tags on field labels in WPForms.
 *
 * @link   https://wpforms.com/developers/how-to-process-smart-tags-in-field-labels/
 *
 * @param  array $properties
 * @param  array $field
 * @param  array $form_data
 */
 
function wpf_dev_textarea_field_display( $field, $field_atts, $form_data ) {
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_textarea_field_display', 'wpf_dev_textarea_field_display', 10, 3 );

Apply Code to Specific Field Types

There are additional fields you can add this code to as well.

/**
 * Run Smart Tags on field labels in WPForms.
 *
 * @link   https://wpforms.com/developers/xxx
 *
 * @param  array $properties
 * @param  array $field
 * @param  array $form_data
 */

// Run smart tags on text field label.
function wpf_dev_text_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_text_field_display', 'wpf_dev_text_field_display', 10, 3 );

// Run smart tags on textarea field label.
function wpf_dev_textarea_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 ) 
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_textarea_field_display', 'wpf_dev_textarea_field_display', 10, 3 );

// Run smart tags on checkbox field label.
function wpf_dev_checkbox_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_checkbox_field_display', 'wpf_dev_checkbox_field_display', 10, 3 );

// Run smart tags on email field label.
function wpf_dev_email_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 ) 
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_email_field_display', 'wpf_dev_email_field_display', 10, 3 );

// Run smart tags on select field label.
function wpf_dev_select_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 ) 
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_select_field_display', 'wpf_dev_select_field_display', 10, 3 );

// Run smart tags on radio field label.
function wpf_dev_radio_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_radio_field_display', 'wpf_dev_radio_field_display', 10, 3 );

// Run smart tags on number field label.
function wpf_dev_number_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_number_field_display', 'wpf_dev_number_field_display', 10, 3 );

// Run smart tags on name field label.
function wpf_dev_name_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_name_field_display', 'wpf_dev_name_field_display', 10, 3 );

// Run smart tags on phone field label.
function wpf_dev_phone_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_phone_field_display', 'wpf_dev_phone_field_display', 10, 3 );

// Run smart tags on address field label.
function wpf_dev_address_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_address_field_display', 'wpf_dev_address_field_display', 10, 3 );

// Run smart tags on URL field label.
function wpf_dev_url_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 ) 
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_url_field_display', 'wpf_dev_url_field_display', 10, 3 );

// Run smart tags on password field label.
function wpf_dev_password_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_password_field_display', 'wpf_dev_password_field_display', 10, 3 );

// Run smart tags on payment checkbox field label.
function wpf_dev_payment_checkbox_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_payment_checkbox_field_display', 'wpf_dev_payment_checkbox_field_display', 10, 3 );

// Run smart tags on payment multiple select field label.
function wpf_dev_payment_multiple_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_payment_multiple_field_display', 'wpf_dev_payment_multiple_field_display', 10, 3 );

// Run smart tags on cfredit card field label.
function wpf_dev_creditcard_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_creditcard_field_display', 'wpf_dev_creditcard_field_display', 10, 3 );

// Run smart tags on single payment field label.
function wpf_dev_payment_single_field_display( $field, $field_atts, $form_data ) {
	if( $form_data[ 'id' ] != 365 )
    return;
    $field['label'] = wpforms()->smart_tags->process( $field['label'], $form_data );
    return $field;
}
add_filter( 'wpforms_payment_single_field_display', 'wpf_dev_payment_single_field_display', 10, 3 );

This will not work for Smart Tags that pull user input values (for example, {field_id="3"}).

And that’s it! You can now process your Smart Tags inside field Labels. Would you like to process Smart Tags in field descriptions? Have a look at our article on How to Process Smart Tags in Field Descriptions.