Filter
uncanny-automator
automator_universal_loopable_token_{$this->get_id()}
Filters the list of child tokens available for a specific automator item when it's being processed.
add_filter( 'automator_universal_loopable_token_{$this->get_id()}', $callback, 10, 2 );
Description
Fires after child tokens are determined for a loopable token. Developers can filter the $child_tokens array to modify or add child tokens for specific loopable token IDs before they are set, allowing for dynamic token generation.
Usage
add_filter( 'automator_universal_loopable_token_{$this->get_id()}', 'your_function_name', 10, 2 );
Parameters
-
$child_tokens(mixed) - This parameter contains an array of child tokens that are intended to be filtered before being assigned to the instance.
-
$args(mixed) - This parameter holds an array of child tokens that are intended to be processed or modified by the filter.
Return Value
The filtered value.
Examples
add_filter( 'automator_universal_loopable_token_my_custom_token_id', 'my_custom_automator_loopable_token_logic', 10, 2 );
/**
* Example function to modify child tokens for a specific loopable token.
*
* This function demonstrates how to add, remove, or modify child tokens
* associated with a custom loopable token before they are used by the
* Automator plugin.
*
* @param array $child_tokens The original array of child tokens.
* @param array $args An array of arguments, including the 'instance' of the loopable token.
* @return array The modified array of child tokens.
*/
function my_custom_automator_loopable_token_logic( array $child_tokens, array $args ): array {
// Ensure we have the necessary arguments.
if ( ! isset( $args['instance'] ) || ! ( $args['instance'] instanceof YourLoopableTokenClass ) ) {
return $child_tokens; // Return original if arguments are not as expected.
}
/** @var YourLoopableTokenClass $loopable_token_instance */
$loopable_token_instance = $args['instance'];
// Example: Add a new child token if it doesn't exist.
$new_child_token_key = 'my_custom_data_point';
if ( ! array_key_exists( $new_child_token_key, $child_tokens ) ) {
$child_tokens[ $new_child_token_key ] = array(
'name' => 'My Custom Data Point',
'description' => 'This is a custom data point for my loopable token.',
'value' => $loopable_token_instance->get_some_custom_value(), // Assume this method exists on YourLoopableTokenClass
);
}
// Example: Remove an existing child token.
$token_to_remove = 'unwanted_default_token';
if ( array_key_exists( $token_to_remove, $child_tokens ) ) {
unset( $child_tokens[ $token_to_remove ] );
}
// Example: Modify an existing child token's value.
$token_to_modify = 'another_existing_token';
if ( array_key_exists( $token_to_modify, $child_tokens ) ) {
$child_tokens[ $token_to_modify ]['value'] .= ' - Modified!';
}
return $child_tokens;
}
// Note: You would need to define 'YourLoopableTokenClass' and
// 'get_some_custom_value()' method in your actual plugin or theme.
// Replace 'my_custom_token_id' with the actual ID returned by $this->get_id()
// in your Automator loopable token class.
// The number 10 is the priority, and 2 indicates that the callback function accepts 2 arguments.
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/services/loopable/loopable-token.php:119
public function set_child_tokens( array $child_tokens = array() ) {
$args = array(
'instance' => $this,
);
$this->child_tokens = apply_filters( "automator_universal_loopable_token_{$this->get_id()}", $child_tokens, $args );
}