Filter uncanny-automator

automator_elementor_add_page_id_before_form_id

Filters the page ID before the form ID is used in Elementor integrations.

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

Description

This filter hook allows developers to modify or prevent the addition of a page ID before a form ID when processing Elementor forms. It fires during Elementor form processing, offering a chance to alter the context or disable the inclusion of the page ID for specific scenarios. The `false` default prevents modification unless explicitly overridden.


Usage

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

Parameters

$form_id (mixed)
This parameter is a placeholder that is not used by the hook.

Return Value

The filtered value.


Examples

<?php

/**
 * Example function to filter the 'automator_elementor_add_page_id_before_form_id' hook.
 *
 * This example demonstrates how to prepend the post ID to the form ID
 * if a specific condition is met (e.g., if the form is part of a draft post).
 *
 * @param mixed $value The default value passed to the filter (usually false).
 * @param mixed $form_id The original form ID.
 * @return mixed The modified form ID or the original value.
 */
function my_automator_elementor_prepend_post_id_to_form_id( $value, $form_id ) {
    // Assuming $form_id is the original form ID.
    // To prepend the post ID, we need access to the post ID.
    // Since the hook is applied within a loop that has $post_meta->post_id,
    // we'll need to pass the post ID to the filter if we want to use it directly.
    // For this example, let's simulate a scenario where we only want to prepend
    // the post ID for forms within posts that are in 'draft' status.

    // NOTE: In a real-world scenario, you might need to adjust how you get
    // the $post_id depending on the exact context where the filter is applied.
    // If $post_meta is available in the scope of the filter callback,
    // you could use it. For this example, we'll simulate this.

    // To make this example work directly with the provided source context,
    // the filter needs to be able to access $post_meta->post_id.
    // Since the filter is called inside the foreach loop over $post_metas,
    // and $post_meta is available there, we can make this work.

    // Let's assume we are interested in modifying the form ID only if the
    // original $value passed to the filter is not already indicating modification.
    // The default $value is false. If it's true, it means the form ID
    // has already been modified or a condition for modification has been met.

    // The logic provided in the source example `if ( true === apply_filters(...) )`
    // implies that returning `true` from the filter will trigger the modification.

    // Let's simulate a condition:
    // If the $form_id is 'my-special-form' (a hypothetical identifier)
    // OR if we wanted to check post status (which we don't have direct access to here,
    // but the principle applies).
    // For demonstration, let's say we want to prepend the post ID if the form ID
    // contains a specific string, or if the default $value was actually intended
    // to carry information about the post ID.

    // The source code shows:
    // if ( true === apply_filters( 'automator_elementor_add_page_id_before_form_id', false, $form_id ) ) {
    //     $form_id = "{$post_meta->post_id}___{$form_id}";
    // }
    // This means returning `true` from the filter will enable the modification.

    // Let's create a condition where we decide to return `true`.
    // For a realistic example, imagine you want to prefix the form ID with the post ID
    // for all forms that are associated with posts of a certain post type, or
    // if the form has a specific setting.

    // Since we don't have the post_id directly in this callback without it being passed,
    // and the source code uses $post_meta->post_id, let's assume the intention
    // is to return true if modification is desired, and the actual modification
    // happens *after* the filter returns true, using the available $post_meta->post_id.

    // So, our filter should decide *whether* to modify.
    // Let's make a simple rule: if the form ID is "123" (a placeholder),
    // we want to prepend the post ID.
    if ( $form_id === '123' ) {
        // Returning true tells the calling code to proceed with prepending the post ID.
        return true;
    }

    // If the condition is not met, we return the original value or false,
    // indicating no modification is needed based on our filter's logic.
    return $value; // Or return false; if you want to be explicit about no action.
}

// Add the filter to WordPress.
// The second parameter '2' specifies that our callback function accepts 2 arguments:
// $value (the default value passed to the filter, which is 'false' in the source)
// and $form_id (the form ID).
add_filter( 'automator_elementor_add_page_id_before_form_id', 'my_automator_elementor_prepend_post_id_to_form_id', 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/elementor/helpers/elementor-helpers.php:101
src/integrations/elementor/tokens/elem-tokens.php:51

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