Introduction
Would you like to process Smart Tags in field labels on your forms? With a small PHP snippet, this can be easily achieved. In this tutorial, we’ll show you how to process Smart Tags in your form field labels.
Using Smart Tags is a very 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.
For the purpose of this tutorial, we’re going to create a form that only our logged-in users can see, therefore we want to make it more personal by using their first name inside the field label of a Paragraph form field.
Adding the snippet
We’re going to start by adding our snippet to our site. If you need any assistance with how to add snippets to your site, please check out this tutorial.
Below we’re going to show you two methods, one that targets a specific form ID and another that would be applied for all forms. Just select which snippet you would want to use.
Targeting a specific form ID
In this snippet, we’re only targeting the form ID 365. If you need help in finding your form ID, please review this tutorial.
/** * Run Smart Tags on all field labels. * * @link https://wpforms.com/developers/how-to-process-smart-tags-in-field-labels/ */ function wpf_dev_textarea_field_display( $field, $field_atts, $form_data ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. if ( absint( $form_data[ 'id' ] ) !== 365 ) { return $field; } $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 );
Using 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/ */ 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 );
Creating your form
Next, we’re going to create our form and add our fields. The first form field we’re going to add is a Paragraph Text form field, we’re going to use our new Smart Tag inside the field label to pull the user’s first name. So we’re going to change the label for this field to Tell us a little about yourself {user_first_name}.
If you need any help in creating your form, please review this documentation.
When your form loads, you’ll see the name that is pulled from your new Smart Tag.
Using the Smart Tags in the email notifications
You can use Smart Tags in your notifications as well.
To do this, navigate to Settings from inside the form builder and select Notifications.
While configuring your email notification message, you can select the Show Smart Tags to include what Smart Tags you wish to include in your message
Using Smart Tags in other form field labels
Below, you will find the snippet needed for each of the various form fields which you can process Smart Tags in the label for each of these specific fields.
As mentioned above, these snippets are specific to a single form ID. If you’d like to make it available to all forms, simply remove the block in the snippet which references the form ID.
/** * Run Smart Tags on field labels in WPForms. * * @link https://wpforms.com/developers/how-to-process-smart-tags-in-field-labels/ */ // Run smart tags on text field label. function wpf_dev_text_field_display( $field, $field_atts, $form_data ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. if( $form_data[ 'id' ] != 365 ) return $field; $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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 single payment field label. function wpf_dev_payment_single_field_display( $field, $field_atts, $form_data ) { // Optional, you can limit to specific forms. Below, we restrict output to // form #365. 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 functionality will not support user input data (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.
Related
Filter Reference: wpforms_textarea_field_display