Filter Since 3.0 uncanny-automator

automator_parse_token_parse_text

May be run a do_shortcode on the field itself if it contains a shortcode? Ticket# 22255 Filters the text content of a field before it's processed, allowing shortcode execution within the field.

add_filter( 'automator_parse_token_parse_text', $callback, 10, 2 );

Description

This filter hook allows developers to modify the text content of a field before it's processed for shortcodes. Use it to manipulate or escape text, ensuring safe and accurate shortcode rendering within Automator. It's applied directly before `do_shortcode`, offering a chance to preprocess dynamic content.


Usage

add_filter( 'automator_parse_token_parse_text', 'your_function_name', 10, 2 );

Parameters

$field_text (mixed)
- **$field_text** `mixed`
$args (mixed)

Return Value

The filtered value.


Examples

add_filter(
	'automator_parse_token_parse_text',
	function( $field_text, $original_field_text, $args ) {
		// Example: If the field text contains a specific placeholder, replace it with a dynamic value.
		// This could be useful for dynamic content generation within Automator's fields.

		// Check if $args contains user data and a specific token key.
		if ( isset( $args['user'] ) && is_object( $args['user'] ) && property_exists( $args['user'], 'ID' ) ) {
			$user_id = $args['user']->ID;

			// Example: Replace a token like "{user_id}" with the actual user ID.
			if ( strpos( $field_text, '{user_id}' ) !== false ) {
				$field_text = str_replace( '{user_id}', $user_id, $field_text );
			}

			// Example: Replace a token like "{user_email}" with the user's email.
			if ( strpos( $field_text, '{user_email}' ) !== false ) {
				$field_text = str_replace( '{user_email}', $args['user']->user_email, $field_text );
			}
		}

		// Example: If the field text contains a placeholder for the current date, replace it.
		if ( strpos( $field_text, '{current_date}' ) !== false ) {
			$field_text = str_replace( '{current_date}', current_time( 'mysql' ), $field_text );
		}

		// Return the modified field text.
		return $field_text;
	},
	10, // Priority
	3  // Accepted args: $field_text, $original_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:907
src/core/deprecated/legacy-token-parser.php:916
src/core/lib/utilities/class-automator-input-parser.php:872
src/core/lib/utilities/class-automator-input-parser.php:882

public function maybe_parse_shortcodes_in_fields( $field_text, $recipe_id = null, $user_id = null, $args = array() ) {

		$skip_do_shortcode_actions = apply_filters(
			'automator_skip_do_shortcode_parse_in_fields',
			array(
				'CREATEPOST',
				'BULKUPDATE_CODE',
			)
		);

		$action_meta_code = isset( $args['action_meta'] ) && isset( $args['action_meta']['code'] ) ? $args['action_meta']['code'] : '';
		if ( true === apply_filters( 'automator_skip_cslashing_value', false, $field_text, $action_meta_code, $recipe_id, $args ) ) {
			return $field_text;
		}

		// If filter is set to true OR action meta matches
		if ( in_array( $action_meta_code, $skip_do_shortcode_actions, true ) || true === apply_filters( 'automator_skip_do_action_field_parsing', $field_text, $recipe_id, $user_id, $args ) ) {
			// The function stripcslashes preserves the a, b, f, n, r, t and v characters.
			return apply_filters( 'automator_parse_token_parse_text', stripcslashes( $field_text ), $field_text, $args );
		}

		/**
		 * May be run a do_shortcode on the field itself if it contains a shortcode?
		 * Ticket# 22255
		 *
		 * @since 3.0
		 */
		return do_shortcode( apply_filters( 'automator_parse_token_parse_text', stripcslashes( $field_text ), $field_text, $args ) );
	}


Scroll to Top