Filter uncanny-automator

uap_option_all_elementor_forms

Filters the options for all Elementor forms, allowing modification before they are applied to Elementor forms.

add_filter( 'uap_option_all_elementor_forms', $callback, 10, 1 );

Description

Filters the list of available Elementor form fields that Uncanny Automator can use as tokens. Developers can add, remove, or modify these options to customize which Elementor form data is accessible within Uncanny Automator recipes. This hook fires when Uncanny Automator is retrieving Elementor form options.


Usage

add_filter( 'uap_option_all_elementor_forms', 'your_function_name', 10, 1 );

Parameters

$option (mixed)
This parameter holds the current option value being processed by the filter.

Return Value

The filtered value.


Examples

/**
 * Filters the list of available Elementor form options to potentially add custom ones or modify existing ones.
 *
 * This example demonstrates how to add a new option to the list of Elementor forms,
 * perhaps for a custom form widget provided by another plugin.
 *
 * @param array $option The current array of Elementor form options. Expected to be an array of arrays,
 *                      where each inner array contains form title and ID keys.
 * @return array The modified array of Elementor form options.
 */
add_filter( 'uap_option_all_elementor_forms', 'my_custom_elementor_form_options', 10, 1 );

function my_custom_elementor_form_options( $option ) {
	// Check if the option array is structured as expected.
	if ( ! is_array( $option ) || empty( $option ) || ! is_array( reset( $option ) ) ) {
		// If the structure is unexpected, return the original option to avoid errors.
		return $option;
	}

	// Define a new custom form. This is a hypothetical example.
	// In a real scenario, you would dynamically fetch or generate this information.
	$custom_form_title = 'My Custom Form';
	$custom_form_id    = 'custom_form_123'; // A unique identifier for your custom form.

	// Add the custom form to the beginning of the options array.
	// The format matches the expected structure: an array where keys are
	// option codes and values are labels.
	$new_options = array(
		$custom_form_title => $custom_form_title, // Using title as a simple key for this example.
		$custom_form_id    => $custom_form_id,
	);

	// Merge the new options with the existing ones.
	// We'll prepend the new options.
	$option = array_merge( $new_options, $option );

	return $option;
}

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/elementor/helpers/elementor-helpers.php:129

public function all_elementor_forms( $label = null, $option_code = 'ELEMFORMS', $args = array() ) {

		if ( ! $label ) {
			$label = esc_attr__( 'Form', 'uncanny-automator' );
		}

		$args = wp_parse_args(
			$args,
			array(
				'uo_include_any' => true,
				'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'];
			}
			global $wpdb;
			$post_metas = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT pm.meta_value, pm.post_id
FROM $wpdb->postmeta pm
    LEFT JOIN $wpdb->posts p
        ON p.ID = pm.post_id
WHERE p.post_type IS NOT NULL
  AND p.post_type NOT LIKE %s
  AND p.post_status NOT IN('trash', 'inherit', 'auto-draft')
  AND pm.meta_key = %s
  AND pm.`meta_value` LIKE %s",
					'revision',
					'_elementor_data',
					'%%form_fields%%'
				)
			);

			if ( ! empty( $post_metas ) ) {
				foreach ( $post_metas as $post_meta ) {
					$inner_forms = self::get_all_inner_forms( json_decode( $post_meta->meta_value ) );
					if ( ! empty( $inner_forms ) ) {
						foreach ( $inner_forms as $form ) {
							$form_id = $form->id;
							if ( true === apply_filters( 'automator_elementor_add_page_id_before_form_id', false, $form_id ) ) {
								$form_id = "{$post_meta->post_id}___{$form_id}";
							}
							$options[ $form_id ] = $form->settings->form_name;
						}
					}
				}
			}
		}//end if

		$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_html__( 'Form title', 'uncanny-automator' ),
				$option_code . '_ID' => esc_html__( 'Form ID', 'uncanny-automator' ),
			),
		);

		//      Automator()->cache->set( 'uap_option_all_elementor_forms', $option );

		return apply_filters( 'uap_option_all_elementor_forms', $option );
	}


Scroll to Top