Filter uncanny-automator-pro

automator_set_wordpress_post_{$type}_tokens

Filters the available tokens for a specific WordPress post type within the Automator plugin before they are used.

add_filter( 'automator_set_wordpress_post_{$type}_tokens', $callback, 10, 1 );

Description

Fires after WordPress post tokens are generated for a specific post type. Developers can use this filter to add custom tokens or modify existing ones for post-related actions within Uncanny Automator. Ensure returned data is a correctly formatted array of token definitions.


Usage

add_filter( 'automator_set_wordpress_post_{$type}_tokens', 'your_function_name', 10, 1 );

Parameters

$tokens (mixed)
This parameter contains an array of tokens that will be available for use within the WordPress post automation.

Return Value

The filtered value.


Examples

/**
 * Add a custom token to the 'post' tokens for Uncanny Automator.
 *
 * This function demonstrates how to add a new token for the post's author's display name.
 *
 * @param array $tokens The original array of tokens.
 * @param string $type The post type (e.g., 'post', 'page', 'custom_post_type').
 * @return array The modified array of tokens.
 */
add_filter( 'automator_set_wordpress_post_tokens', 'my_automator_add_post_author_token', 10, 2 );
function my_automator_add_post_author_token( $tokens, $type ) {
	// Only add this token for the default 'post' type, or adjust as needed.
	if ( 'post' === $type ) {
		// Get the current post ID (assuming this filter runs within a context where $post is available,
		// or you might need to pass it in if the hook context provides it).
		// For demonstration, we'll assume we can get the current post object.
		$current_post = get_post();

		if ( $current_post ) {
			$author_id = $current_post->post_author;
			$author_display_name = get_the_author_meta( 'display_name', $author_id );

			// Define the new token. The key should be unique and descriptive.
			$tokens['POST_AUTHOR_DISPLAY_NAME'] = array(
				'name' => esc_attr_x( 'Post Author Display Name', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'text',
			);
		}
	}

	return $tokens;
}

/**
 * Hydrate the custom 'POST_AUTHOR_DISPLAY_NAME' token for Uncanny Automator.
 *
 * This function is intended to be called by Uncanny Automator when it needs to get the value
 * for the 'POST_AUTHOR_DISPLAY_NAME' token.
 *
 * @param string $option_code The token option code (e.g., 'POST_AUTHOR_DISPLAY_NAME').
 * @param int $post_id The ID of the post.
 * @return mixed The value of the token, or false if it cannot be retrieved.
 */
add_filter( 'automator_hydrate_wordpress_post_tokens', 'my_automator_hydrate_post_author_token_value', 10, 2 );
function my_automator_hydrate_post_author_token_value( $value, $option_code ) {
	// Check if this is our custom token.
	if ( 'POST_AUTHOR_DISPLAY_NAME' === $option_code ) {
		// We need to get the post ID from the current context.
		// Uncanny Automator usually provides this or it can be retrieved globally.
		// For this example, we'll assume get_post_id() is available and works.
		// In a real scenario, you might need to check the hook's provided parameters.
		$post_id = get_post_id(); // This is a hypothetical function, you'd use what Automator provides.

		if ( $post_id ) {
			$author_id = get_post_field( 'post_author', $post_id );
			$author_display_name = get_the_author_meta( 'display_name', $author_id );
			return $author_display_name;
		}
	}

	return $value; // Return original value if it's not our token.
}

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

uncanny-automator-pro/src/integrations/wp/helpers/wp-pro-helpers.php:197

public function get_post_relevant_tokens( $type = 'trigger', $option_code = 'WPPOSTTYPES' ) {
		$tokens = array(
			$option_code                => array(
				'name' => esc_attr_x( 'Post title', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'text',
			),
			$option_code . '_ID'        => array(
				'name' => esc_attr_x( 'Post ID', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'int',
			),
			$option_code . '_URL'       => array(
				'name' => esc_attr_x( 'Post URL', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'text',
			),
			$option_code . '_POSTNAME'  => array(
				'name' => esc_attr_x( 'Post slug', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'text',
			),
			$option_code . '_THUMB_ID'  => array(
				'name' => esc_attr_x( 'Post featured image ID', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'int',
			),
			$option_code . '_THUMB_URL' => array(
				'name' => esc_attr_x( 'Post featured image URL', 'WordPress Token', 'uncanny-automator-pro' ),
				'type' => 'text',
			),
		);

		return apply_filters( "automator_set_wordpress_post_{$type}_tokens", $tokens );
	}

Scroll to Top