How to Process Smart Tags in Checkbox Labels

Overview

Would you like to process Smart Tags in Checkbox field labels? If you’d like to process these Smart Tags as field label choices, you can easily accomplish this with some custom PHP. In this tutorial, we’ll show you the PHP to use so that you can use Smart Tags in Checkbox field labels.

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

Additionally, custom Smart Tags can be created to extend this functionality much further.

By default, however, Smart Tags will not be processed if placed within the label field.

However, with the code snippet below you can easily enable this option.

Setup

To process any Smart Tags included within Checkboxes field choices, you can copy the code below into your site.

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

/**
 * Using Smart Tags in Checkboxes.
 *
 * @link   https://wpforms.com/developers/process-smart-tags-in-checkbox-labels/
 *
 * @param  array $field
 * @param  array $deprecated
 * @param  array $form_data
 * @return array
 */
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 );

With the code snippet added, you can now place Smart Tags in the checkbox labels

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.