How to Process Smart Tags in Checkbox Labels

Introduction

Would you like to process Smart Tags in the Checkbox field labels? By default, Smart Tags will not be processed if placed within the label field. However, using a small PHP code snippet you can easily allow this. In this tutorial, we’ll walk you through each step of the way.

Smart Tags is a great way to dynamically pull and display data within your form.

Alternatively, custom Smart Tags can be created to extend this functionality even further.

Creating the form

For the purpose of this tutorial, we’re going to add some fields to our membership form that will confirm the full name or display name of the person completing the form, which will be pulled from the WordPress profile as well as a form field for How did you hear about this offer? which will also be pulled from a Smart tag.

If you need help with creating a form, please check out this helpful guide.

begin by creating your form and adding your fields

Using Smart Tags in checkbox labels

We can use the Smart Tags so we’ll enter {user_full_name} in the label for the first option and {user_display} for the second option.

Add smart tags you choose for the checkbox label for display name and full name

We’re also going to add a third option that when selected will conditionally show a Single Text Line form field for the visitor to enter the correct name if the profile records are incorrect.

Next, we’re going to add another Checkbox field to ask the visitor to complete the How did you hear about this offer? which will include the {url_referer} Smart Tag to try and pull the URL the visitor was just referred from.

The next checkbox label is going to use the URL Referrer smart tag so we can see where the visitor has come from to get to this form and display that as an option on the form.

Adding the code snippet

To process any Smart Tags included for the Checkbox labels, you’ll need to copy this code snippet to your site.

If you need help adding codes snippets, please review this tutorial.

This snippet won’t pull user input values (for example, {field_id=”3″}) to populate other form fields on the form.

/**
 * Using Smart Tags in Checkboxes.
 *
 * @link   https://wpforms.com/developers/process-smart-tags-in-checkbox-labels/
 */

function wpf_dev_checkbox_choices_process_smarttags( $field, $deprecated, $form_data ) {

	foreach ( $field[ 'choices' ] as $key => $choice ) {

		if ( ! empty( $choice[ 'label' ] ) ) {

			$field[ 'choices' ][ $key ][ 'label' ] = apply_filters( 'wpforms_process_smart_tags', $choice[ 'label' ], $form_data );

		}

	}

	return $field;
}
add_filter( 'wpforms_checkbox_field_display', 'wpf_dev_checkbox_choices_process_smarttags', 10, 3 );

The code above tells the checkbox options to process the filter for Smart Tags with apply_filters( ‘wpforms_process_smart_tags’,. This is what allows you to use Smart Tags as labels.

Now on the frontend, instead of seeing the coded Smart Tags as the checkbox labels you'll see the dynamically populated options.

And that’s all you need in order to use Smart Tags in the Checkbox field labels. Would you like to also use Smart Tags in the HTML / Code form field? Take a look at our tutorial on How to Process Smart Tags in HTML Fields.

Filter References:

FAQ

Q: Can I use this for Dropdown and Multiple Choice fields too?

A: Absolutely! Use this snippet to process Smart Tags inside the field labels of the Dropdown and Multiple Choice form fields.

/**
 * Using Smart Tags in Multiple Choice and Dropdown choices.
 *
 * @link   https://wpforms.com/developers/process-smart-tags-in-checkbox-labels/
 */

function wpf_dev_select_radio_choices_process_smarttags( $field, $deprecated, $form_data ) {
	
    foreach ( $field[ 'choices' ] as $key => $choice ) {
		
        if ( ! empty( $choice[ 'label' ] ) ) {
			
            $label = apply_filters( 'wpforms_process_smart_tags', $choice[ 'label' ], $form_data );
			
            if ( ! empty( $label ) ) {
				
                $field['choices'][ $key ][ 'label' ] = $label;
            
			} else {
               
                // Remove empty option.
                unset( $field['choices'][ $key ] );
            }
        }
    }
 
    return $field;
}
 
add_filter( 'wpforms_radio_field_display', 'wpf_dev_select_radio_choices_process_smarttags', 10, 3 );
add_filter( 'wpforms_select_field_display', 'wpf_dev_select_radio_choices_process_smarttags', 10, 3 );