Filter uncanny-automator

automator_create_post_sanitize_meta_values

Filters post meta values before they are saved during post creation via the Automator.

add_filter( 'automator_create_post_sanitize_meta_values', $callback, 10, 3 );

Description

Filters the meta key and value before saving a post. Developers can modify or validate meta data, ensuring it conforms to specific requirements before it's attached to a newly created post. The first parameter, a boolean, controls whether the meta data is saved.


Usage

add_filter( 'automator_create_post_sanitize_meta_values', 'your_function_name', 10, 3 );

Parameters

$meta_key (mixed)
This parameter is a boolean value indicating whether the sanitization process should proceed.
$meta_value (mixed)
This parameter contains the meta key associated with the post to be created.
$recipe_id (mixed)
This parameter contains the sanitized meta value that will be saved for the post.

Return Value

The filtered value.


Examples

/**
 * Example filter for automator_create_post_sanitize_meta_values.
 *
 * This function demonstrates how to sanitize a specific meta value
 * before it's saved with a post. In this example, it ensures that a meta value
 * intended for a 'discount_code' key is converted to uppercase.
 *
 * @param bool   $should_proceed Whether the meta value should proceed to be saved.
 * @param string $meta_key       The meta key for the data being saved.
 * @param mixed  $meta_value     The meta value for the data being saved.
 * @param int    $recipe_id      The ID of the Automator recipe.
 *
 * @return bool True if the meta value should proceed, false otherwise.
 */
add_filter(
	'automator_create_post_sanitize_meta_values',
	function ( $should_proceed, $meta_key, $meta_value, $recipe_id ) {
		// Only modify if the meta_key is 'discount_code'
		if ( 'discount_code' === $meta_key ) {
			// Ensure the meta value is a string before attempting to uppercase
			if ( is_string( $meta_value ) ) {
				// Convert discount code to uppercase for consistency
				$meta_value = strtoupper( $meta_value );

				// You could also potentially return false here to prevent saving if the discount code is invalid
				// For example:
				// if ( ! preg_match( '/^[A-Z0-9]{5,10}$/', $meta_value ) ) {
				//     error_log( "Invalid discount code format: {$meta_value}" );
				//     return false; // Prevent saving invalid discount code
				// }

				// Since we've modified the value, we need to re-apply it to the original context if possible.
				// However, filters directly modifying the return value indicate if the item should proceed.
				// If you need to modify the value *and* have it used, you might need a different approach
				// or rely on the fact that the source code might re-fetch the value after this filter.
				// For this example, we'll just ensure it proceeds with the modification implicitly handled.
			}
		}

		// Return true to indicate that the meta value should be processed and saved.
		// Return false if you want to prevent this specific meta value from being saved.
		return $should_proceed;
	},
	10, // Priority
	4  // Number of accepted arguments
);

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/integrations/wp/actions/wp-createpost.php:495

$meta_pairs = json_decode( $action_data['meta']['CPMETA_PAIRS'], true );

			if ( ! empty( $meta_pairs ) ) {
				foreach ( $meta_pairs as $pair ) {
					$meta_key   = $pair['KEY'];
					$meta_value = Automator()->parse->text( $pair['VALUE'], $recipe_id, $user_id, $args );
					if (
						true === apply_filters( 'automator_create_post_sanitize_meta_values', true, $meta_key, $meta_value, $recipe_id ) &&
						true === apply_filters( 'automator_create_post_sanitize_meta_values_' . $recipe_id, true, $meta_key, $meta_value ) &&
						true === apply_filters( 'automator_create_post_sanitize_meta_values_' . sanitize_title( $meta_key ), true, $meta_value, $recipe_id )
					) {
						$meta_key   = Automator()->utilities->automator_sanitize(
							$meta_key,
							apply_filters( 'automator_sanitize_get_field_type', 'text', $meta_key, array() )
						);


Scroll to Top