Filter
uncanny-automator
automator_text_field_parsed
Filters the arguments passed to text fields after parsing, allowing modification of field data before it's used.
add_filter( 'automator_text_field_parsed', $callback, 10, 2 );
Description
Filters the parsed text content of a field, typically after variables and tokens have been replaced. Developers can use this hook to further modify the field's output before it's used in a recipe, for instance, to sanitize or format the text. This hook is part of the core input parsing system.
Usage
add_filter( 'automator_text_field_parsed', 'your_function_name', 10, 2 );
Parameters
-
$this(mixed) - This parameter is an instance of the `Automator_Input_Parser` class, providing access to its methods and properties for parsing input fields.
-
$args(mixed) - This parameter represents the current instance of the AutomatorInputParser class.
Return Value
The filtered value.
Examples
/**
* Example function to modify the parsed text field for automator.
* This function might be used to add custom prefixes or suffixes,
* or to replace certain placeholders with dynamic content before
* shortcodes are parsed.
*
* @param string $field_text The parsed text field content.
* @param array $args An array of arguments passed to the parse_vars method,
* potentially containing recipe_triggers and action_meta.
* @return string The modified parsed text field content.
*/
add_filter( 'automator_text_field_parsed', function( $field_text, $args ) {
// Let's say we want to prepend a specific identifier to all text fields
// coming from the automator actions, but only if a certain condition is met.
// For this example, we'll check if 'action_meta' exists in the $args
// and if a specific key 'add_custom_prefix' is set to true.
if ( isset( $args['action_meta']['add_custom_prefix'] ) && true === $args['action_meta']['add_custom_prefix'] ) {
// Prepend a custom string. In a real scenario, this might be dynamic.
$field_text = '[AUTOMATED_PREFIX] ' . $field_text;
}
// We can also perform replacements based on other data available in $args.
// For instance, if a specific user ID is available and we want to
// subtly modify a placeholder related to that user.
if ( isset( $args['user_id'] ) && ! empty( $args['user_id'] ) ) {
// Example: replace a placeholder with a user-specific string.
// In a real scenario, you'd likely fetch user data here.
$field_text = str_replace( '{user_placeholder}', 'User ID ' . $args['user_id'] . ' specific data', $field_text );
}
return $field_text;
}, 10, 2 ); // Priority 10, accepts 2 arguments ($field_text, $args)
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:876
src/core/lib/utilities/class-automator-input-parser.php:821
public function text( $field_text = null, $recipe_id = null, $user_id = null, $trigger_args = null ) {
// Sanity check that there was a $field_text passed
if ( null === $field_text ) {
return null;
}
$args = array(
'field_text' => $field_text,
'meta_key' => null,
'user_id' => $user_id,
'action_data' => isset( $trigger_args['action_meta'] ) ? $trigger_args['action_meta'] : null,
'recipe_id' => $recipe_id,
);
// Action tokens hook.
$args['field_text'] = apply_filters( 'automator_action_token_input_parser_text_field_text', $args['field_text'], $args, $trigger_args );
if ( ! empty( $trigger_args['trigger_log_id'] ) ) {
$args['trigger_log_id'] = $trigger_args['trigger_log_id'];
}
if ( ! empty( $trigger_args['run_number'] ) ) {
$args['run_number'] = $trigger_args['run_number'];
}
if ( ! empty( $trigger_args['recipe_log_id'] ) ) {
$args['recipe_log_id'] = $trigger_args['recipe_log_id'];
}
if ( ! empty( $trigger_args['trigger_id'] ) ) {
$args['trigger_id'] = $trigger_args['trigger_id'];
}
if ( isset( $trigger_args['recipe_triggers'] ) ) {
$args['recipe_triggers'] = $trigger_args['recipe_triggers'];
}
if ( isset( $trigger_args['action_meta'] ) ) {
$args['action_meta'] = $trigger_args['action_meta'];
}
$field_text = apply_filters( 'automator_text_field_parsed', $this->parse_vars( $args, $trigger_args ), $args );
return $this->maybe_parse_shortcodes_in_fields( $field_text, $recipe_id, $user_id, $args );
}