Filter Dynamic uncanny-automator

automator_token_renderable_before_set_{dynamic}

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters token data before it's set, allowing modification for dynamic placeholder rendering.

add_filter( 'automator_token_renderable_before_set_{dynamic}', $callback, 10, 4 );

Description

Fires before renderable tokens are set for a specific trigger, allowing modification of the token array. Developers can use this hook to dynamically alter or add to the available tokens for a given trigger code before they are presented. The trigger code is appended to the hook name.


Usage

add_filter( 'automator_token_renderable_before_set_{dynamic}', 'your_function_name', 10, 4 );

Parameters

$this (mixed)
This parameter represents the object that the hook is being called on, likely an instance of a class related to trigger token management within the Automator plugin.
$trigger_code (mixed)
This parameter represents the current object or class instance that the hook is being called within.
$tokens (mixed)
This parameter represents the unique code or identifier of the trigger that is currently being processed.
$args (mixed)
This parameter contains an array of tokens that are available for rendering.

Return Value

The filtered value.


Examples

add_filter( 'automator_token_renderable_before_set_new_post', 'my_automator_modify_new_post_tokens', 10, 4 );

/**
 * Modifies the renderable tokens for the 'new_post' trigger.
 *
 * This example demonstrates how to conditionally remove or alter tokens
 * before they are set for the 'new_post' trigger in AutomatorWP.
 *
 * @param array $tokens The current array of renderable tokens.
 * @param string $trigger_code The code of the trigger ('new_post' in this case).
 * @param array $original_tokens The original tokens retrieved from the trigger.
 * @param array $args Additional arguments passed to the filter.
 *
 * @return array The modified array of renderable tokens.
 */
function my_automator_modify_new_post_tokens( $tokens, $trigger_code, $original_tokens, $args ) {
    // Ensure we are actually working with the 'new_post' trigger
    if ( 'new_post' !== strtolower( $trigger_code ) ) {
        return $tokens;
    }

    // Example: Remove the 'post_content' token if it's a draft post.
    // This assumes $original_tokens contains information about the post status.
    // In a real scenario, you'd need to inspect $args or have a way to access
    // post data if it's not directly available in $original_tokens.
    // For demonstration, let's assume $original_tokens might contain a 'post_status' key.
    $post_status = isset( $original_tokens['post_status'] ) ? $original_tokens['post_status'] : 'publish'; // Default to publish if not found

    if ( 'draft' === $post_status && isset( $tokens['post_content'] ) ) {
        unset( $tokens['post_content'] );
    }

    // Example: Add a custom token if the post is published by a specific user role.
    // This is a hypothetical example. You'd need to check user roles based on $args
    // or how the trigger is configured.
    $user_role = 'editor'; // Hypothetical role
    if ( 'publish' === $post_status && isset( $original_tokens['post_author_role'] ) && $user_role === $original_tokens['post_author_role'] ) {
        $tokens['custom_editor_publish'] = array(
            'name'    => __( 'Post Published by Editor', 'your-text-domain' ),
            'content' => __( 'This post was published by an editor.', 'your-text-domain' ),
            'options' => array(
                'is_generated' => true, // Mark as generated by this filter
            ),
        );
    }

    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/trait-trigger-tokens.php:90

protected function add_trigger_tokens_filter( $trigger_code = '', $integration = '' ) {

		// Integrations uses `$this->set_tokens()` to create tokens.
		// Bail if the tokens are not set, or if they are empty.
		if ( empty( $this->get_tokens() ) ) {
			return;
		}

		$filter = strtr(
			'automator_maybe_trigger_{{integration}}_{{code}}_tokens',
			array(
				'{{integration}}' => strtolower( $integration ),
				'{{code}}'        => strtolower( $trigger_code ),
			)
		);

		$callback_closure = function ( $tokens, $args ) use ( $trigger_code ) {

			if ( $this->has_token_renderable( $trigger_code ) ) {
				return $this->get_tokens_renderable( $trigger_code );
			}

			// Allow modification of renderable tokens before setting.
			// @since 4.5
			$tokens = apply_filters( 'automator_token_renderable_before_set_' . strtolower( $trigger_code ), $this->get_tokens(), $trigger_code, $tokens, $args );

			// Otherwise, set the renderable tokens.
			$this->set_tokens_renderable( $tokens, $trigger_code );

			return $this->get_tokens_renderable( $trigger_code );

		};

		// Add some filter to append the tokens.
		add_filter( $filter, $callback_closure, 99, 2 );

	}

Scroll to Top