Filter uncanny-automator

uap_option_list_contact_form7_forms

Filters the list of available Contact Form 7 forms for integration options.

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

Description

Filters the list of options for Contact Form 7 forms within Uncanny Automator. Developers can use this hook to add, remove, or modify tokens related to Contact Form 7 forms, such as form titles, IDs, or URLs, influencing how these forms are represented and utilized in Automator recipes.


Usage

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

Parameters

$option (mixed)
This parameter is used to specify the initial value for the Contact Form 7 form dropdown.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering the 'uap_option_list_contact_form7_forms' hook.
 *
 * This example adds a new token option for the Contact Form 7 form,
 * specifically a 'form_slug' token, if the form has a slug available.
 *
 * @param array $option The array of options to be filtered.
 * @return array The modified array of options.
 */
function my_custom_contact_form7_tokens( $option ) {
    // Assuming $option['value'] contains the form ID and $option['label'] contains the form title.
    // We'll try to get the form object to extract more information if available.
    if ( ! empty( $option['value'] ) && class_exists( 'WPCF7_ContactForm' ) ) {
        $form_id = $option['value'];
        $contact_form = WPCF7_ContactForm::get_instance( $form_id );

        if ( $contact_form && $contact_form->exists() ) {
            // Get the form slug if it exists.
            // Note: Contact Form 7 doesn't have a direct "slug" property in the same way posts do.
            // We might need to infer it or check for custom meta if available.
            // For this example, let's assume a custom meta key might store a slug if the user added it.
            // In a real scenario, you'd know the exact way to retrieve the slug.
            // Let's simulate retrieving a slug, perhaps from post_name if the form is a CPT.
            // However, CF7 forms are typically not standard CPTs.
            // A more realistic scenario might be to add a custom field in the CF7 UI itself,
            // which isn't directly supported by CF7's core.

            // For demonstration, let's assume we have a function or method to get a unique identifier that could act as a slug.
            // If not, this part might be omitted or adjusted based on actual CF7 integration logic.
            $form_slug = sanitize_title( $contact_form->title() ); // Using title as a fallback for slug

            if ( ! empty( $form_slug ) ) {
                $option_code = $option['value']; // This is likely the form ID, used to build token keys.

                $option['relevant_tokens'][ $option_code . '_SLUG' ] = esc_attr__( 'Form Slug', 'uncanny-automator' );
            }
        }
    }

    return $option;
}
add_filter( 'uap_option_list_contact_form7_forms', 'my_custom_contact_form7_tokens', 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/contact-form7/helpers/contact-form7-helpers.php:95

public function list_contact_form7_forms( $label = null, $option_code = 'CF7FORMS', $args = array() ) {

		if ( ! $label ) {
			$label = esc_attr__( '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();

		$args = array(
			'post_type'      => 'wpcf7_contact_form',
			'posts_per_page' => 999,
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		);

		$options = Automator()->helpers->recipe->options->wp_query( $args );
		$type    = 'select';

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => $type,
			'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' ),
				$option_code . '_URL' => esc_attr__( 'Form URL', 'uncanny-automator' ),
			),
		);

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

Scroll to Top