Filter
uncanny-automator
automator_post_token_parsed
Filters a post token after it has been parsed, allowing modification of its arguments before use.
add_filter( 'automator_post_token_parsed', $callback, 10, 3 );
Description
Fires after a token has been parsed and processed, allowing developers to modify the final output before it's saved. Use this to alter the parsed token's value, apply custom formatting, or perform conditional logic. The hook passes the parsed token value, its meta key, and the original token arguments.
Usage
add_filter( 'automator_post_token_parsed', 'your_function_name', 10, 3 );
Parameters
-
$this(mixed) - This parameter represents the current instance of the action parser class.
-
$meta_key(mixed) - This parameter represents the current instance of the `Action_Parser` class.
-
$token_args(mixed) - This parameter contains the meta key that is being parsed.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the automator_post_token_parsed filter hook.
*
* This filter allows you to modify the parsed token value after it has been processed
* by the Automator plugin. In this example, we'll check if the parsed value is a
* number and if so, we'll prepend a currency symbol to it.
*/
add_filter( 'automator_post_token_parsed', function ( $parsed_value, $meta_key, $token_args ) {
// Check if the parsed value is a numeric string and not empty.
if ( is_numeric( $parsed_value ) && '' !== $parsed_value ) {
// Prepend a currency symbol (e.g., '$') to the numeric value.
// This assumes the token represents a monetary amount.
$currency_symbol = '$'; // Replace with your desired currency symbol
$parsed_value = $currency_symbol . $parsed_value;
}
// Always return the modified (or original) value.
return $parsed_value;
}, 10, 3 ); // 10 is the priority, 3 is the number of arguments accepted by the callback function.
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/actions/trait-action-parser.php:191
public function maybe_parse_tokens( $user_id, $action_data, $recipe_id, $args ) {
// Allows the parser to know that this action is under a loop.
if ( isset( $action_data['loop'] ) ) {
$args['loop'] = $action_data['loop'];
}
if ( ! array_key_exists( 'meta', $action_data ) ) {
return $this->get_parsed();
}
$metas = $action_data['meta'];
if ( empty( $metas ) ) {
return $this->get_parsed();
}
$this->pre_parse();
// Pass the 'should_apply_extra_formatting_property' to the $args['action_meta'] key.
$args['action_meta']['should_apply_extra_formatting'] = $this->get_should_apply_extra_formatting();
foreach ( $metas as $meta_key => $meta_value ) {
$is_json_string = Automator()->utilities->is_json_string( $meta_value );
// Parse the json string.
if ( true === $is_json_string ) {
// @added 5.9
$parser_args = array(
'recipe_id' => $recipe_id,
'user_id' => $user_id,
'args' => $args,
);
$json_parsed_string = $this->parse_json_string( $meta_value, $parser_args );
// Only skip if self::parse_json_string is successful.
if ( ! is_wp_error( $json_parsed_string ) ) {
$this->set_parsed( $meta_key, $json_parsed_string );
continue;
}
}
// Prevents autop when text only contains a single line.
if ( ! Automator()->utilities->has_multiple_lines( $meta_value ) ) {
$this->set_parsed( $meta_key, Automator()->parse->text( $meta_value, $recipe_id, $user_id, $args ) );
continue;
}
if ( ! $this->is_valid_token( $meta_key, $meta_value ) ) {
$parsed = Automator()->parse->text( $meta_value, $recipe_id, $user_id, $args );
$this->set_parsed( $meta_key, $this->should_wpautop( $parsed, $meta_key ) );
continue;
}
$parsed = Automator()->parse->text( $meta_value, $recipe_id, $user_id, $args );
$token_args = array(
'user_id' => $user_id,
'action_data' => $action_data,
'recipe_id' => $recipe_id,
'args' => $args,
);
$parsed = apply_filters( 'automator_pre_token_parsed', $parsed, $meta_key, $token_args );
$should_process_shortcode = apply_filters( 'automator_trait_action_parser_maybe_parse_tokens_should_process_shortcode', true, $token_args );
if ( true === $should_process_shortcode && $this->is_do_shortcode() ) {
$parsed = do_shortcode( $parsed );
}
$parsed = apply_filters( 'automator_post_token_parsed', $this->should_wpautop( $parsed, $meta_key ), $meta_key, $token_args );
$this->set_parsed( $meta_key, $parsed );
}
return $this->get_parsed();
}