Filter uncanny-automator

automator_maybe_parse_{$match}

Filters the content before it's parsed, allowing modification of matched strings based on user and arguments.

add_filter( 'automator_maybe_parse_{$match}', $callback, 10, 5 );

Description

This filter hook allows developers to modify or replace the value of a token before it's parsed. It's applied after a token match is identified and before it's substituted into the field text. Developers can use this to dynamically alter token replacements based on custom logic or user context.


Usage

add_filter( 'automator_maybe_parse_{$match}', 'your_function_name', 10, 5 );

Parameters

$replaceable (mixed)
This parameter contains the value that will replace the matched token.
$field_text (mixed)
This parameter contains the value that will be used to replace a matched token in the text.
$match (mixed)
This parameter contains the raw text from the field where a token was found.
$current_user (mixed)
This parameter contains the specific token key being matched and processed.
$args (mixed)
This parameter contains the currently logged-in user object, which can be used to access user-specific data or perform checks.

Return Value

The filtered value.


Examples

add_filter(
	'automator_maybe_parse_user_display_name', // Example $match value
	'my_custom_automator_user_display_name_parser',
	10,
	5
);

/**
 * Filters the 'user_display_name' token for Automator recipes to provide a custom display name.
 *
 * This function intercepts the default parsing of the 'user_display_name' token
 * within the Uncanny Automator plugin and allows for custom logic to determine
 * what value should be substituted for the token.
 *
 * @param mixed $replaceable   The current value being parsed for replacement.
 * @param mixed $field_text    The original text of the field where the token was found.
 * @param mixed $match         The token that was matched (e.g., 'user_display_name').
 * @param WP_User|null $current_user The WP_User object of the current user if available.
 * @param array $args          An array of additional arguments passed to the filter.
 *
 * @return string The modified or custom value to replace the token with.
 */
function my_custom_automator_user_display_name_parser( $replaceable, $field_text, $match, $current_user, $args ) {
	// Only proceed if we have a $current_user object and the $match is indeed 'user_display_name'
	if ( $current_user instanceof WP_User && 'user_display_name' === $match ) {

		// Example: If the user has a specific meta key, use that as the display name.
		// Otherwise, fall back to the default display name.
		$custom_display_name = get_user_meta( $current_user->ID, 'preferred_display_name', true );

		if ( ! empty( $custom_display_name ) ) {
			// Return the custom display name if the meta key is set.
			return esc_html( $custom_display_name );
		} else {
			// Fallback to the default display name if no custom name is found.
			return esc_html( $current_user->display_name );
		}
	}

	// If the conditions aren't met, return the original $replaceable value
	// to allow other filters or the default logic to handle it.
	return $replaceable;
}

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:410
src/core/deprecated/legacy-token-parser.php:415

break;

					case 'recipe_run':
						$replaceable = $run_number;
						break;

					default:
						$replaceable = apply_filters( "automator_maybe_parse_{$match}", $replaceable, $field_text, $match, $current_user, $args );
						break;
				}
			}

			$replaceable = apply_filters( "automator_maybe_parse_{$match}", $replaceable, $field_text, $match, $user_id, $args );

			$replaceable = apply_filters( 'automator_maybe_parse_replaceable', $replaceable );


Scroll to Top