Filter uncanny-automator

automator_recipe_main_object_loop_tokens_items

Filters the list of available tokens for use within the main object loop of an Automator recipe.

add_filter( 'automator_recipe_main_object_loop_tokens_items', $callback, 10, 1 );

Description

Filters the items used to hydrate loop tokens within the Automator recipe core. Developers can add custom token items for loops, extending the functionality and available data for dynamic content within recipes. This hook is executed during recipe execution when loop actions are processed.


Usage

add_filter( 'automator_recipe_main_object_loop_tokens_items', 'your_function_name', 10, 1 );

Parameters

$this (mixed)
This parameter is an array that will be populated with the items to be used as tokens within the loop.

Return Value

The filtered value.


Examples

/**
 * Adds custom tokens to the loop object for a recipe.
 *
 * This function filters the 'automator_recipe_main_object_loop_tokens_items' hook
 * to add new, custom tokens that can be used within a loop action in AutomatorWP recipes.
 *
 * @param array $tokens The existing array of tokens.
 * @param object $loop_object The current loop object instance.
 * @return array The modified array of tokens, including the new custom tokens.
 */
add_filter( 'automator_recipe_main_object_loop_tokens_items', function( $tokens, $loop_object ) {

	// Example: Add a token for the current date and time in a specific format.
	$tokens['current_datetime_formatted'] = array(
		'name'       => __( 'Current Date and Time (Formatted)', 'your-text-domain' ),
		'value'      => date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), current_time( 'timestamp' ) ),
		'resolution' => __( 'The current date and time when the recipe is run.', 'your-text-domain' ),
	);

	// Example: Add a token for a hypothetical custom setting from the loop object.
	// This assumes your $loop_object might have a property like 'custom_loop_setting'.
	if ( isset( $loop_object->custom_loop_setting ) && ! empty( $loop_object->custom_loop_setting ) ) {
		$tokens['custom_loop_data'] = array(
			'name'       => __( 'Custom Loop Data', 'your-text-domain' ),
			'value'      => esc_html( $loop_object->custom_loop_setting ),
			'resolution' => __( 'Custom data associated with this loop.', 'your-text-domain' ),
		);
	}

	// You can add more custom tokens here based on your plugin's logic.
	// For instance, if the $loop_object has a method to retrieve a specific piece of data.

	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/services/recipe/structure/actions/item/loop.php:110

private function hydrate_tokens() {
		$this->tokens = (array) apply_filters( 'automator_recipe_main_object_loop_tokens_items', array(), $this );
	}

Internal Usage

Found in src/core/services/loopable/action-loopable-token.php:71:

add_filter( 'automator_recipe_main_object_loop_tokens_items', $closure, 10, 2 );

Found in src/core/services/loopable/trigger-loopable-token.php:63:

add_filter( 'automator_recipe_main_object_loop_tokens_items', $closure, 10, 2 );

Found in src/core/services/loopable/universal-loopable-token.php:72:

add_filter( 'automator_recipe_main_object_loop_tokens_items', $closure, 10, 2 );

Found in uncanny-automator-pro/src/core/loops/token/users/definition.php:19:

add_filter( 'automator_recipe_main_object_loop_tokens_items', array( new self(), 'list_tokens' ), 10, 2 );

Found in uncanny-automator-pro/src/core/loops/token/common/definition.php:17:

add_filter( 'automator_recipe_main_object_loop_tokens_items', array( new self(), 'list_tokens' ), 10, 2 );

Found in uncanny-automator-pro/src/core/loops/token/posts/definition.php:19:

add_filter( 'automator_recipe_main_object_loop_tokens_items', array( new self(), 'list_tokens' ), 10, 2 );
Scroll to Top