Filter uncanny-automator

automator_maybe_parse_replaceable

Filters a value to determine if it should be parsed as a replaceable placeholder.

add_filter( 'automator_maybe_parse_replaceable', $callback, 10, 1 );

Description

Allows developers to modify or replace matched tokens within trigger or action data before they are processed. Use this filter to dynamically alter token values based on custom logic or external data sources, ensuring accurate and personalized automation execution.


Usage

add_filter( 'automator_maybe_parse_replaceable', 'your_function_name', 10, 1 );

Parameters

$replaceable (mixed)
This parameter represents the value that might be replaced or modified based on the provided arguments.

Return Value

The filtered value.


Examples

/**
 * Example of using the 'automator_maybe_parse_replaceable' filter to modify or conditionally parse replaceable tokens.
 *
 * This filter is applied after individual token parsing logic and before further processing.
 * It allows for a final modification of the `$replaceable` value.
 *
 * In this example, we'll check if the `$replaceable` value is a string and if it contains a specific placeholder.
 * If it does, we'll replace that placeholder with a hardcoded value.
 *
 * @param mixed $replaceable The value that might be replaced. This could be a string, an array, or other data types depending on the context.
 * @return mixed The potentially modified `$replaceable` value.
 */
function my_automator_modify_replaceable_token( $replaceable ) {

    // Check if the $replaceable is a string and if it contains a specific placeholder we want to handle.
    // This is a hypothetical placeholder for demonstration. In a real scenario, this would likely be
    // a token generated by the Automator plugin itself.
    if ( is_string( $replaceable ) && strpos( $replaceable, '{my_custom_placeholder}' ) !== false ) {
        // Replace our custom placeholder with a specific string.
        // In a real-world scenario, this might involve looking up data from the user, post, or a custom option.
        $replaceable = str_replace( '{my_custom_placeholder}', 'This is a custom replacement value.', $replaceable );
    }

    // You could also add logic here to prevent parsing under certain conditions,
    // for example, if the $replaceable is a specific type of data you don't want to parse.
    // For instance, if you wanted to ensure that only strings are processed and not, say, arrays:
    // if ( ! is_string( $replaceable ) ) {
    //     return $replaceable; // Return the original value without attempting to parse.
    // }

    return $replaceable;
}
add_filter( 'automator_maybe_parse_replaceable', 'my_automator_modify_replaceable_token', 10, 1 );

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

$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 );

			/**
			 * Rare instance when an action token is not "yet" parsed Trigger tokens try to parse it.
			 *
			 * This occurs when there is a nested tokens, or a token inside a token.
			 *
			 * @since 5.0.1


Scroll to Top