Filter
uncanny-automator
automator_wpforms_non_dynamic_choice_value
Filters the value of non-dynamic choice fields in WPForms before it's used in automations.
add_filter( 'automator_wpforms_non_dynamic_choice_value', $callback, 10, 2 );
Description
Filters the selected value for non-dynamic choice fields in WPForms. Developers can modify the default value, which is often the index of the selected choice, before it's used in automations. Useful for custom handling of checkbox, radio, or dropdown selections.
Usage
add_filter( 'automator_wpforms_non_dynamic_choice_value', 'your_function_name', 10, 2 );
Parameters
-
$value(mixed) - This parameter contains the non-dynamic choice value being processed by the filter.
-
$field(mixed) - This parameter contains the raw value of the non-dynamic choice from the WPForms field.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the automator_wpforms_non_dynamic_choice_value filter.
*
* This example demonstrates how to modify the value of a non-dynamic choice field
* from a WPForms submission before it's processed by Automator.
*
* For instance, if you have a 'Country' dropdown field and you want to append
* '(USA)' to the selected country if it's 'United States', this filter can be used.
*
* @param mixed $value The original value of the WPForms field.
* @param array $field The configuration array of the WPForms field.
* @return string|mixed The modified or original value.
*/
function my_automator_modify_wpforms_choice_value( $value, $field ) {
// Check if the field is a dropdown or radio button and not a payment field.
if ( ( 'select' === $field['type'] || 'radio' === $field['type'] ) && strpos( $field['type'], 'payment' ) !== 0 ) {
// Check if the selected value is 'United States'.
if ( 'United States' === $value ) {
// Append '(USA)' to the value.
return $value . ' (USA)';
}
// If you wanted to modify based on the 'value' of the choice instead of the 'label':
// $choices = $field['choices'];
// foreach ( $choices as $choice_id => $choice_data ) {
// if ( $choice_data['value'] === $value && 'country_us' === $choice_data['value'] ) {
// return $value . ' (USA)';
// }
// }
}
// Return the original value if no modifications are needed.
return $value;
}
add_filter( 'automator_wpforms_non_dynamic_choice_value', 'my_automator_modify_wpforms_choice_value', 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/wpforms/tokens/wpf-tokens.php:684
src/integrations/wpforms/tokens/wpf-tokens.php:709
private function get_non_dynamic_choice_index( $value, $field ) {
// Skip if payment field.
if ( strpos( $field['type'], 'payment' ) === 0 ) {
return apply_filters( 'automator_wpforms_non_dynamic_choice_value', $value, $field );
}
$label_key = isset( $field['show_values'] ) && absint( $field['show_values'] ) > 0 ? 'value' : 'label';
$labels = wp_list_pluck( $field['choices'], $label_key );
$multiple = ( isset( $field['multiple'] ) && 0 < absint( $field['multiple'] ) ) || 'checkbox' === $field['type'];
$value_has_commas = strpos( $value, ',' ) !== false;
// Multiple Choice and value contains commas.
if ( $multiple && $value_has_commas ) {
$selected = $this->get_non_dynamic_multiple_choice_index( $value, $labels );
} else {
// Single choice.
$value = $this->normalize_whitespace( $value );
$selected = array_search( $value, $labels, true );
// Fallback check: if the value is not found, try the other key. Ticket #64096
if ( false === $selected ) {
$fallback_key = 'value' === $label_key ? 'label' : 'value';
$fallback_labels = wp_list_pluck( $field['choices'], $fallback_key );
$selected = array_search( $value, $fallback_labels, true );
$selected = false !== $selected ? $selected : '';
}
}
return apply_filters( 'automator_wpforms_non_dynamic_choice_value', $selected, $field );
}