Filter uncanny-automator

automator_postmeta_token_parsed

Filters the parsed post meta value before it is used, allowing modification based on post ID and meta key.

add_filter( 'automator_postmeta_token_parsed', $callback, 10, 4 );

Description

Fires after a post meta token has been parsed and its value retrieved. Developers can filter the retrieved `$value` before it's used, modify it, or return a different value. This hook provides access to the original post ID, meta key, and the parsed token pieces.


Usage

add_filter( 'automator_postmeta_token_parsed', 'your_function_name', 10, 4 );

Parameters

$value (mixed)
This parameter contains the parsed post meta value, potentially modified by the filter.
$post_id (mixed)
The `$value` parameter contains the original post meta value before any parsing or manipulation.
$meta_key (mixed)
This parameter contains the ID of the post that the meta value is associated with.
$value (mixed)
This parameter represents the meta key of the post meta being processed.

Return Value

The filtered value.


Examples

add_filter(
	'automator_postmeta_token_parsed',
	'my_custom_automator_postmeta_token_parsing',
	10,
	4
);

/**
 * Modifies the parsed post meta value for a specific meta key,
 * adding a prefix if the value meets certain criteria.
 *
 * @param mixed $value       The current post meta value.
 * @param int   $post_id     The ID of the post.
 * @param string $meta_key    The meta key.
 * @param array $token_data  Additional data related to the token parsing.
 * @return mixed The modified or original post meta value.
 */
function my_custom_automator_postmeta_token_parsing( $value, $post_id, $meta_key, $token_data ) {
	// Example: Only modify if the meta_key is 'my_special_field'
	if ( 'my_special_field' === $meta_key ) {
		// Example: Add a prefix if the value is not empty and is a string.
		if ( ! empty( $value ) && is_string( $value ) ) {
			$value = 'PREFIX_' . $value;
		}
	}

	// Always return the potentially modified value.
	return $value;
}

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:1036
src/core/lib/utilities/class-automator-input-parser.php:1037

public function automator_maybe_parse_postmeta_token( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
		if ( ! in_array( 'POSTMETA', $pieces, true ) ) {
			return $value;
		}
		$pieces  = $this->parse_inner_token( $pieces, $replace_args );
		$post_id = isset( $pieces[1] ) ? absint( $pieces[1] ) : null;
		if ( null === $post_id ) {
			return $value;
		}
		$meta_key = sanitize_text_field( $pieces[2] );
		$value    = get_post_meta( $post_id, $meta_key, true );
		if ( is_array( $value ) ) {
			$value = join( ', ', $value );
		}

		return apply_filters(
			'automator_postmeta_token_parsed',
			$value,
			$post_id,
			$meta_key,
			array(
				'value'        => $value,
				'pieces'       => $pieces,
				'recipe_id'    => $recipe_id,
				'trigger_data' => $trigger_data,
				'user_id'      => $user_id,
				'replace_args' => $replace_args,
			)
		);
	}


Scroll to Top