Filter
uncanny-automator
automator_pre_defined_tokens
Filters the array of pre-defined tokens available for use in recipes, allowing for modification before they are displayed.
add_filter( 'automator_pre_defined_tokens', $callback, 10, 1 );
Description
Allows developers to modify or add predefined tokens before they are parsed. This filter is ideal for injecting custom data or altering existing token representations used within the Automator core. Use it to extend token functionality for recipes.
Usage
add_filter( 'automator_pre_defined_tokens', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Adds custom tokens related to user meta to the automator_pre_defined_tokens filter.
*
* This function hooks into the 'automator_pre_defined_tokens' filter to add
* new predefined tokens that can be used within the Automator plugin.
* In this example, we're adding tokens that represent specific user meta keys
* to allow for dynamic content based on user profile information.
*
* @param array $tokens An array of predefined tokens.
* @return array The modified array of predefined tokens, including the new custom tokens.
*/
add_filter( 'automator_pre_defined_tokens', 'my_custom_automator_tokens', 10, 1 );
function my_custom_automator_tokens( $tokens ) {
// Get current user ID if a user is logged in.
$current_user_id = get_current_user_id();
// Example: Add a token for a specific user meta field, e.g., 'customer_since'.
// We only want to add this token if a user is actually logged in and has this meta.
if ( $current_user_id && get_user_meta( $current_user_id, 'customer_since', true ) ) {
$tokens['user_customer_since'] = array(
'name' => __( 'Customer Since Date', 'your-text-domain' ),
'description' => __( 'The date the user became a customer.', 'your-text-domain' ),
'type' => 'text', // Or 'date' if you have a specific date type
'callback' => function() use ( $current_user_id ) {
return get_user_meta( $current_user_id, 'customer_since', true );
},
);
}
// Example: Add a token for another user meta field, e.g., 'loyalty_level'.
if ( $current_user_id && get_user_meta( $current_user_id, 'loyalty_level', true ) ) {
$tokens['user_loyalty_level'] = array(
'name' => __( 'Loyalty Level', 'your-text-domain' ),
'description' => __( 'The current loyalty level of the user.', 'your-text-domain' ),
'type' => 'text',
'callback' => function() use ( $current_user_id ) {
return get_user_meta( $current_user_id, 'loyalty_level', true );
},
);
}
// You can add more custom tokens here based on your needs.
// For instance, if you have a custom post type and want to pull data from it
// based on the user, you would implement that logic here.
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/deprecated/legacy-token-parser.php:30
public function __construct() {
$this->defined_tokens = apply_filters(
'automator_pre_defined_tokens',
array(
// 'recipe_total_run',
// 'recipe_run',
)
);
// add_filter( 'automator_maybe_parse_token', array( $this, 'automator_maybe_parse_postmeta_token' ), 99999, 6 );
// Attach the new trigger tokens arch for actions that are scheduled.
add_filter( 'automator_pro_before_async_action_executed', array( $this, 'attach_trigger_tokens_hook' ), 10, 1 );
}