automator_create_post_sanitize_meta_values_{dynamic}
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the meta values before they are sanitized for post creation, allowing custom modification.
add_filter( 'automator_create_post_sanitize_meta_values_{dynamic}', $callback, 10, 2 );
Description
This dynamic filter hook allows developers to sanitize meta values before they are saved with a new post. It fires after meta values have been parsed but before they are added to the post. Developers can use this to clean, validate, or modify meta data, ensuring data integrity. The hook receives the current sanitization status, the meta key, and the meta value.
Usage
add_filter( 'automator_create_post_sanitize_meta_values_{dynamic}', 'your_function_name', 10, 2 );
Parameters
-
$meta_key(mixed) - This parameter likely represents the original, unsanitized meta value that is being processed.
-
$meta_value(mixed) - This parameter contains the meta key associated with the post data.
Return Value
The filtered value.
Examples
/**
* Example filter to sanitize specific meta values when creating a post via the Automator plugin.
* This example will ensure that any meta value for 'custom_email_address' is a valid email format.
*
* @param bool $allowed Whether the meta value is allowed to be saved (default true).
* @param string $meta_key The meta key being processed.
* @param mixed $meta_value The meta value to sanitize.
* @param int $recipe_id The ID of the recipe being executed.
* @param array $args Additional arguments passed to the action.
*
* @return bool|mixed Returns false if the meta value is invalid, otherwise returns the original or modified meta value.
*/
add_filter( 'automator_create_post_sanitize_meta_values', function ( $allowed, $meta_key, $meta_value, $recipe_id, $args = array() ) {
// Only apply custom sanitization for a specific meta key
if ( 'custom_email_address' === $meta_key ) {
// Check if the meta value is a string and attempt to validate it as an email
if ( is_string( $meta_value ) && ! is_email( $meta_value ) ) {
// If it's not a valid email, disallow saving this meta value by returning false
return false;
}
}
// For all other meta keys, or if the meta_value passed the check, allow it.
// The original $allowed parameter is also respected.
return $allowed;
}, 10, 5 );
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:496
src/integrations/wp/actions/wp-createpost.php:497
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() )
);
$meta_value = Automator()->utilities->automator_sanitize(