Filter
uncanny-automator
uap_option_all_happyforms_forms
Filters the data for all HappyForms forms before it is saved or retrieved, allowing modification of form options.
add_filter( 'uap_option_all_happyforms_forms', $callback, 10, 1 );
Description
Filters the options array for all HappyForms forms. Developers can modify the form title and form ID tokens, or completely replace the options array to customize form selection in Uncanny Automator. Fires before HappyForms form options are returned.
Usage
add_filter( 'uap_option_all_happyforms_forms', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter contains the value of the option being filtered, which could be a string, array, or other mixed type.
Return Value
The filtered value.
Examples
/**
* Filters the options for HappyForms forms to potentially add additional data or modify existing data.
*
* For example, this could be used to add a custom label or to conditionally include/exclude certain forms
* based on specific criteria.
*
* @param array $option The original array of options for HappyForms forms.
* Expected structure:
* [
* 'options' => [ // Array of HappyForms forms, typically keyed by ID
* form_id => 'Form Title',
* ...
* ],
* 'relevant_tokens' => [ // Array of relevant tokens for tokens generation
* option_code => 'Form title',
* option_code_ID => 'Form ID',
* ...
* ]
* ]
* @return array The modified array of options.
*/
add_filter( 'uap_option_all_happyforms_forms', function( $option ) {
// Example: Add a custom token for the current date if a specific form is present.
// This is a hypothetical scenario to demonstrate modifying the 'relevant_tokens'.
if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
$form_title_code = 'happyforms_form'; // Assuming this is the base code for form title
$form_id_code = 'happyforms_form_ID'; // Assuming this is the base code for form ID
// Check if a form with a specific title exists.
$specific_form_title = 'Contact Us';
$specific_form_id = array_search( $specific_form_title, $option['options'] );
if ( $specific_form_id !== false ) {
// Add a new relevant token if the 'Contact Us' form is found.
$option['relevant_tokens'][ $form_title_code . '_date_submitted' ] = esc_attr__( 'Date Submitted for Contact Us Form', 'your-text-domain' );
}
// Example: Conditionally remove a form from the options if its ID matches a certain value.
// Let's say we want to exclude a form with ID '123'.
$exclude_form_id = 123;
if ( isset( $option['options'][$exclude_form_id] ) ) {
unset( $option['options'][$exclude_form_id] );
// If we remove a form, we might also want to remove its corresponding tokens.
// This depends on how the tokens are generated based on the options array.
// For this example, we'll assume tokens are generated based on keys.
// In a real scenario, you'd need to know the exact token keys.
// For instance, if $option_code for this form was 'my_custom_form_code':
// unset( $option['relevant_tokens']['my_custom_form_code'] );
// unset( $option['relevant_tokens']['my_custom_form_code_ID'] );
}
}
return $option;
}, 10, 1 );
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/happyforms/helpers/happyforms-helpers.php:108
public function all_happyforms_forms( $label = null, $option_code = 'HFFORMS', $args = array() ) {
if ( ! $label ) {
$label = esc_attr__( 'Form', 'uncanny-automator' );
}
$args = wp_parse_args(
$args,
array(
'uo_include_any' => false,
'uo_any_label' => esc_attr__( 'Any form', 'uncanny-automator' ),
)
);
$token = key_exists( 'token', $args ) ? $args['token'] : false;
$is_ajax = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
$end_point = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
$options = array();
if ( Automator()->helpers->recipe->load_helpers ) {
if ( $args['uo_include_any'] ) {
$options[- 1] = $args['uo_any_label'];
}
$form_controller = happyforms_get_form_controller();
$forms = $form_controller->do_get();
if ( ! empty( $forms ) ) {
foreach ( $forms as $form ) {
$options[ $form['ID'] ] = $form['post_title'];
}
}
}
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'supports_tokens' => $token,
'is_ajax' => $is_ajax,
'fill_values_in' => $target_field,
'endpoint' => $end_point,
'options' => $options,
'relevant_tokens' => array(
$option_code => esc_attr__( 'Form title', 'uncanny-automator' ),
$option_code . '_ID' => esc_attr__( 'Form ID', 'uncanny-automator' ),
),
);
return apply_filters( 'uap_option_all_happyforms_forms', $option );
}