Filter uncanny-automator

automator_maybe_trigger_pre_tokens

Filters recipe triggers before tokens are processed, allowing modifications to trigger metadata and the recipe ID.

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

Description

Fires before tokens are processed for a trigger. Developers can use this filter to modify the token array or conditionally prevent token loading. It fires early in the trigger processing lifecycle, before any actual token replacement occurs.


Usage

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

Parameters

$triggers_meta (mixed)
This parameter contains the default array of tokens that will be filtered.
$recipe_id (mixed)
This parameter contains metadata associated with the trigger that is currently being processed.

Return Value

The filtered value.


Examples

add_filter( 'automator_maybe_trigger_pre_tokens', 'my_custom_automator_pre_tokens', 10, 3 );

/**
 * Custom function to modify the tokens array before they are potentially triggered.
 *
 * This example demonstrates how to add a custom token if a specific condition
 * related to the recipe or its metadata is met.
 *
 * @param array $tokens       The current array of tokens.
 * @param mixed $triggers_meta The metadata associated with the trigger.
 * @param int   $recipe_id    The ID of the current recipe.
 *
 * @return array The modified array of tokens.
 */
function my_custom_automator_pre_tokens( $tokens, $triggers_meta, $recipe_id ) {

    // Let's imagine we have a specific trigger scenario where we want to inject a custom token.
    // For example, if the trigger meta contains a specific key indicating a "special offer".
    if ( isset( $triggers_meta['trigger_type'] ) && 'special_offer_activated' === $triggers_meta['trigger_type'] ) {

        // We can add a custom token that will be available for use in this recipe's actions.
        // The key of the array will be the token's identifier, and the value can be
        // a callable that returns the actual token value when resolved.
        $tokens['special_offer_code'] = function() use ( $triggers_meta ) {
            return isset( $triggers_meta['offer_code'] ) ? $triggers_meta['offer_code'] : 'NO_CODE_FOUND';
        };

        // You could also conditionally add more tokens or modify existing ones.
        // For instance, if you wanted to add the user's name to tokens.
        if ( isset( $triggers_meta['user_id'] ) && $user_id = absint( $triggers_meta['user_id'] ) ) {
            $user = get_userdata( $user_id );
            if ( $user ) {
                $tokens['user_display_name'] = $user->display_name;
            }
        }
    }

    // Always return the tokens array, whether modified or not.
    return $tokens;
}

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:106

return null;
		}

		if ( defined( 'DOING_CRON' ) ) {
			return null;
		}

		$tokens = apply_filters( 'automator_maybe_trigger_pre_tokens', array(), $triggers_meta, $recipe_id );

		//Only load these when on edit recipe page or is automator ajax is happening!
		if ( ! automator_do_identify_tokens() ) {
			return $tokens;
		}

		if ( empty( $triggers_meta ) ) {


Scroll to Top