Filter
uncanny-automator
automator_trigger_token_filters
Filters triggers and their metadata, allowing for customization before they are used by Automator.
add_filter( 'automator_trigger_token_filters', $callback, 10, 2 );
Description
Filters the available tokens for triggers. Developers can use this hook to add, remove, or modify token options for triggers, allowing for more dynamic and customized automation. It fires after initial token options are gathered, enabling late-stage adjustments.
Usage
add_filter( 'automator_trigger_token_filters', 'your_function_name', 10, 2 );
Parameters
-
$filters(mixed) - This parameter contains an array of filters that can be applied to the trigger tokens.
-
$triggers_meta(mixed) - This parameter contains an array of filters that can be applied to the trigger tokens.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'automator_trigger_token_filters' hook to modify available tokens for a trigger.
*
* This function adds a custom token to the 'user_registered' trigger,
* allowing users to insert the user's IP address into recipe actions.
*/
function my_custom_trigger_tokens( $filters, $triggers_meta ) {
// Check if the current trigger is 'user_registered' and if we have the necessary meta data.
// $triggers_meta is an array containing information about the current trigger,
// including its ID and other relevant details. The exact structure depends on the plugin.
// Assuming $triggers_meta['trigger_id'] holds the trigger's unique identifier.
if ( isset( $triggers_meta['trigger_id'] ) && $triggers_meta['trigger_id'] === 'user_registered' ) {
// Define a new filter for our custom token.
// The key of this array will be used as the hook name later in the source code.
$filters['automator_user_ip_token'] = array(
'title' => __( 'User IP Address', 'my-text-domain' ),
'description' => __( 'Inserts the IP address of the newly registered user.', 'my-text-domain' ),
'callback' => 'my_get_user_ip_token_value', // This is the function that will be called to get the token's value.
'args' => array( // Arguments that will be passed to the callback.
'user_id', // Assuming user_id is available in $args.
),
);
}
return $filters;
}
add_filter( 'automator_trigger_token_filters', 'my_custom_trigger_tokens', 10, 2 );
/**
* Callback function to retrieve the user's IP address.
*
* This function will be executed when the custom token is used in a recipe action.
*
* @param int $user_id The ID of the user.
* @return string The user's IP address.
*/
function my_get_user_ip_token_value( $user_id ) {
// In a real-world scenario, you'd want to get the IP address reliably.
// This is a simplified example. You might need to access WordPress transients,
// user meta, or $_SERVER variables depending on how the IP is captured.
// For demonstration, we'll simulate getting the IP.
$user_ip = get_user_meta( $user_id, 'user_ip_address', true ); // Assuming IP is stored in user meta.
if ( ! empty( $user_ip ) ) {
return $user_ip;
}
// Fallback or logic to determine IP if not found in meta.
// This might involve looking at $_SERVER variables, but be cautious about proxies.
// For this example, we'll return a placeholder if not found.
return 'N/A';
}
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:208
array(
$filters,
$triggers_meta,
),
'3.0',
'automator_trigger_token_filters'
);
$filters = apply_filters( 'automator_trigger_token_filters', $filters, $triggers_meta );
if ( $filters ) {
foreach ( $filters as $filter => $args ) {
try {
$tokens = apply_filters( $filter, $tokens, $args );
} catch ( Error $e ) {
automator_log( $e->getMessage(), '$e->getMessage()', AUTOMATOR_DEBUG_MODE, 'trigger_tokens_errors', true );