Filter uncanny-automator

automator_maybe_trigger_tokens

Filters the available tokens for an Automator recipe, allowing modification before they are displayed to the user.

add_filter( 'automator_maybe_trigger_tokens', $callback, 10, 2 );

Description

This filter hook, `automator_maybe_trigger_tokens`, allows developers to modify the list of available trigger tokens before they are presented to the user. It fires after initial trigger tokens are gathered but before they are finalized. Developers can use this hook to add, remove, or alter tokens based on the recipe or trigger, for example, to customize or filter token options.


Usage

add_filter( 'automator_maybe_trigger_tokens', 'your_function_name', 10, 2 );

Parameters

$tokens (mixed)
This parameter contains an array of available tokens for the current trigger.
$recipe_id (mixed)
This parameter is a mixed-type array that holds all available tokens for the current trigger.

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Add a custom token to be available in the trigger tokens for a specific recipe.
 *
 * This function hooks into the 'automator_maybe_trigger_tokens' filter to dynamically
 * add a new token to the list of available tokens for a given recipe's trigger.
 *
 * @param array $tokens    The current array of available trigger tokens.
 * @param int   $recipe_id The ID of the recipe being processed.
 *
 * @return array The modified array of trigger tokens.
 */
add_filter( 'automator_maybe_trigger_tokens', function( $tokens, $recipe_id ) {

	// Check if the current recipe is a specific one we want to modify (e.g., recipe ID 123).
	// In a real scenario, you might check for a specific trigger type, user role, or other conditions.
	if ( $recipe_id === 123 ) {

		// Define a new custom token.
		// The key is the token ID that will be used in the Automator plugin,
		// and the value is an array containing 'name' and 'value'.
		$custom_token = array(
			'my_custom_user_role' => array(
				'name'  => __( 'My Custom User Role', 'your-text-domain' ),
				'value' => 'administrator', // The actual value the token will represent.
			),
		);

		// Merge the custom token into the existing tokens array.
		// We use array_merge_recursive to ensure that if there are duplicate keys,
		// they are handled appropriately, though in this simple case, a direct + would also work.
		$tokens = array_merge_recursive( $tokens, $custom_token );
	}

	// Always return the tokens array, whether modified or not.
	return $tokens;
}, 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/core/lib/recipe-parts/tokens/class-automator-tokens.php:233

if ( isset( $triggers_meta['code'] ) ) {
			$tokens = Automator()->get->trigger_tokens_from_trigger_code( $triggers_meta['code'] ) + $tokens;
		}
		// Adds the opportunity to modify final tokens list
		// (i.e., remove middle name from GF tokens list)
		//$tokens = $this->remove_duplicate_token_ids( $tokens );

		return apply_filters( 'automator_maybe_trigger_tokens', $tokens, $recipe_id );
	}


	/**
	 * @param $action
	 * @param $action_id
	 * @param $recipe_id


Scroll to Top