Filter Dynamic uncanny-automator

automator_option_{dynamic}_select_field

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the available token options for dynamic select fields used in Automator integrations.

add_filter( 'automator_option_{dynamic}_select_field', $callback, 10, 1 );

Description

This filter allows developers to modify the data for dynamic select fields within Uncanny Automator. It's triggered when a select field's options are being prepared, letting you alter the `$support_token` data passed to the field. This is useful for customizing available options or adding token support.


Usage

add_filter( 'automator_option_{dynamic}_select_field', 'your_function_name', 10, 1 );

Parameters

$support_token (mixed)
This parameter contains a mixed value that supports tokens for dynamic selection in the automator.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_option_{dynamic}_select_field' filter.
 * This filter allows you to modify the options displayed in a select field for a specific option code.
 *
 * In this example, we'll assume '{dynamic}' is 'user_roles' and we want to add an extra option
 * to select "All Roles" if the current user has administrator privileges.
 *
 * @param array $support_token An array containing information about token support.
 *                             The first element is typically a boolean indicating if tokens are supported.
 *                             Subsequent elements might be passed as arguments to the filter for versioning or context.
 * @return array The modified $support_token array.
 */
add_filter( 'automator_option_user_roles_select_field', function( $support_token ) {

	// Check if the current user can manage options (a common check for administrative functions).
	if ( current_user_can( 'manage_options' ) ) {
		// Assuming the first element of $support_token controls token support.
		// If we want to ensure tokens are supported for this modified field,
		// we might check and potentially set it to true.
		// For this example, we'll focus on adding a custom option, so we'll just ensure
		// it's passed through correctly.

		// In a real-world scenario, you would likely be modifying a list of options
		// provided by the function that generates the select field.
		// This filter hook, based on the source, seems to be designed to influence
		// token support or related metadata for the *options themselves*, not to directly
		// inject new options into a select list.

		// However, if we *were* to interpret this as a way to influence what tokens are available
		// or how they're treated for a specific option's select field, we could do something like this:
		// Let's assume $support_token might contain a key 'options' that we can append to.
		// This is a hypothetical interpretation, as the source code snippet doesn't directly show
		// this filter being used to add options to a select dropdown.

		// If the intention was to add a new option to the dropdown itself, a different filter
		// or a different hook would likely be used. Given the name 'support_token',
		// it's more likely about how tokens relate to the available options.

		// For demonstration, let's assume $support_token is an array and we want to add a
		// specific token-related flag.
		if ( is_array( $support_token ) ) {
			// This is a speculative addition based on the hook name.
			// A real implementation would depend on the exact structure of $support_token
			// and how it's used by the calling function.
			$support_token['custom_admin_role_token'] = true;
		}

		// If the goal was to ensure that tokens *are* supported for this select field,
		// and $support_token is an array where the first element indicates support:
		if ( is_array( $support_token ) && count( $support_token ) > 0 ) {
			$support_token[0] = true; // Ensure token support is enabled.
		} elseif ( ! is_array( $support_token ) ) {
			// If $support_token is not an array, and token support is expected,
			// we might need to initialize it. This is highly dependent on context.
			// For safety, we'll return a default that enables token support.
			return array( true );
		}
	}

	// Always return the (potentially modified) $support_token.
	return $support_token;

}, 10, 1 ); // Priority 10, 1 accepted argument.

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/core/lib/helpers/class-automator-recipe-helpers-field.php:522
src/core/lib/helpers/class-automator-recipe-helpers-field.php:600

public function select_field( $option_code = 'SELECT', $label = null, $options = array(), $default = null, $is_ajax = false, $fill_values_in = '', $relevant_tokens = array(), $args = array() ) {
		if ( defined( 'AUTOMATOR_DEBUG_MODE' ) && true === AUTOMATOR_DEBUG_MODE ) {
			_doing_it_wrong( 'Automator()->helpers->recipe->field->select_field()', 'Use Automator()->helpers->recipe->field->select() instead.', '3.0' );
		}

		// TODO this function should be the main way to create select fields
		// TODO chained values should be introduced using the format in function "list_gravity_forms"
		// TODO the following function should use this function to create selections
		// -- less_or_greater_than
		// -- all_posts
		// -- all_pages
		// -- all_ld_courses
		// -- all_ld_lessons
		// -- all_ld_topics
		// -- all_ld_groups
		// -- all_ld_quiz
		// -- all_buddypress_groups
		// -- all_wc_products
		// -- list_contact_form7_forms
		// -- list_bbpress_forums
		// -- wc_order_statuses
		// -- wp_user_roles
		// -- list_gravity_forms
		// -- all_ec_events
		// -- all_lp_courses
		// -- all_lp_lessons
		// -- all_lf_courses
		// -- all_lf_lessons

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

		$custom_value_description = key_exists( 'custom_value_description', $args ) ? $args['custom_value_description'] : null;
		$supports_custom_value    = $this->supports_custom_value( $args );
		$supports_tokens          = key_exists( 'supports_tokens', $args ) ? $args['supports_tokens'] : null;
		$support_token            = apply_filters( 'uap_option_' . $option_code . '_select_field', array( false ), '3.0', 'automator_option_' . $option_code . '_select_field' );
		$support_token            = apply_filters( 'automator_option_' . $option_code . '_select_field', $support_token );
		$options_show_id          = empty( $args['options_show_id'] ) ? true : $args['options_show_id'];
		$options_show_id          = apply_filters( 'automator_options_show_id', $options_show_id, $this );

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'input_type'               => 'select',
			'supports_tokens'          => $support_token,
			'required'                 => true,
			'default_value'            => $default,
			'options'                  => $options,
			'custom_value_description' => $custom_value_description,
			'supports_custom_value'    => $supports_custom_value,
			'options_show_id'          => $options_show_id,
		);

		if ( ! empty( $relevant_tokens ) ) {
			$option['relevant_tokens'] = $relevant_tokens;
		}

		//$option = $this->select( $option );
		$option = apply_filters_deprecated( 'uap_option_select_field', array( $option ), '3.0', 'automator_option_select_field' );

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


Scroll to Top