Filter uncanny-automator-pro

automator_wpuserupdatedmeta_value

Filters the meta value after a WordPress user's meta data is updated within a recipe.

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

Description

Fires after user meta is updated for a WordPress trigger. Developers can use this filter to modify the meta value that will be saved and used for matching trigger conditions. The original meta key, trigger ID, and recipe ID are available for context.


Usage

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

Parameters

$recipe_id (mixed)
This parameter contains the ID of the recipe that is currently being processed.
$trigger_id (mixed)
This parameter holds the ID of the recipe that is currently being processed.
$recipe_id (mixed)
This parameter contains the unique identifier for the specific trigger instance that fired.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the automator_wpuserupdatedmeta_value filter.
 *
 * This filter allows you to modify the value of user meta that is being
 * tracked by the "User meta field updated" trigger in Uncanny Automator.
 *
 * For instance, you might want to convert a serialized array back into an array
 * for easier processing in a subsequent action, or format a date.
 *
 * @param mixed $meta_value The original meta value from the database.
 * @param int   $trigger_id The ID of the trigger.
 * @param array $recipe_args Arguments passed from the recipe.
 *
 * @return mixed The modified meta value.
 */
add_filter( 'automator_wpuserupdatedmeta_value', 'my_automator_modify_user_meta_value', 10, 3 );

function my_automator_modify_user_meta_value( $meta_value, $trigger_id, $recipe_args ) {

	// Ensure the meta_value is unserialized if it was previously serialized.
	$unserialized_value = maybe_unserialize( $meta_value );

	// Example: If the meta field is a specific key (e.g., 'user_roles')
	// and we want to ensure it's always an array, even if it's a single string.
	if ( isset( $recipe_args['meta_field'] ) && 'user_roles' === $recipe_args['meta_field'] ) {
		if ( is_string( $unserialized_value ) ) {
			// If it's a single role string, wrap it in an array.
			return array( $unserialized_value );
		} elseif ( is_array( $unserialized_value ) ) {
			// If it's already an array, return it as is.
			return $unserialized_value;
		} else {
			// Handle unexpected types, perhaps return an empty array.
			return array();
		}
	}

	// Example: If the meta field stores a timestamp and we want to format it.
	if ( isset( $recipe_args['meta_field'] ) && 'last_login' === $recipe_args['meta_field'] ) {
		if ( is_numeric( $unserialized_value ) ) {
			// Convert the timestamp to a readable date format.
			return date( 'Y-m-d H:i:s', $unserialized_value );
		}
	}

	// If no specific modification is needed for this meta field, return the original (unserialized) value.
	return $unserialized_value;
}

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

uncanny-automator-pro/src/integrations/wp/triggers/wp-usermetaupdated.php:150

public function updated_user_data( $meta_id, $object_id, $meta_key, $_meta_value ) {

		if ( 'added_user_meta' === (string) current_action() && empty( $_meta_value ) ) {
			return;
		}

		$user_id            = $object_id;
		$recipes            = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$matched_recipe_ids = array();

		foreach ( $recipes as $recipe ) {
			foreach ( $recipe['triggers'] as $trigger ) {
				if ( key_exists( $this->trigger_meta, $trigger['meta'] ) ) {
					$trigger_field = $trigger['meta'][ $this->trigger_meta ];
					if ( (string) $trigger_field === (string) $meta_key ) {
						$matched_recipe_ids[ $trigger['ID'] ] = array(
							'recipe_id'  => $recipe['ID'],
							'trigger_id' => $trigger['ID'],
							'meta_field' => $meta_key,
							'meta_value' => $_meta_value,
						);
					}
				}
			}
		}

		if ( ! $matched_recipe_ids ) {
			return;
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $trigger_id => $recipe_id ) {

				if ( ! Automator()->is_recipe_completed( $recipe_id['recipe_id'], $user_id ) ) {
					$args = array(
						'code'             => $this->trigger_code,
						'meta'             => $this->trigger_meta,
						'recipe_to_match'  => $recipe_id['recipe_id'],
						'trigger_to_match' => $trigger_id,
						'ignore_post_id'   => true,
						'user_id'          => $user_id,
						'post_id'          => - 1,
					);

					$result = Automator()->maybe_add_trigger_entry( $args, false );

					if ( $result ) {
						foreach ( $result as $r ) {
							if ( true === $r['result'] ) {
								if ( isset( $r['args'] ) && isset( $r['args']['trigger_log_id'] ) ) {
									//Saving form values in trigger log meta for token parsing!
									$save_meta = array(
										'user_id'        => $user_id,
										'trigger_id'     => $r['args']['trigger_id'],
										'run_number'     => $r['args']['run_number'],
										'trigger_log_id' => $r['args']['trigger_log_id'],
										'ignore_user_id' => true,
									);

									$save_meta['meta_key']   = $r['args']['trigger_id'] . ':' . $this->trigger_code . ':' . $this->trigger_meta;
									$save_meta['meta_value'] = $recipe_id['meta_field'];
									Automator()->insert_trigger_meta( $save_meta );

									$save_meta['meta_key']   = $r['args']['trigger_id'] . ':' . $this->trigger_code . ':UMETAVALUE';
									$save_meta['meta_value'] = apply_filters( 'automator_wpuserupdatedmeta_value', maybe_serialize( $recipe_id['meta_value'] ), $trigger_id, $recipe_id );
									Automator()->insert_trigger_meta( $save_meta );

								}
								Automator()->maybe_trigger_complete( $r['args'] );
							}
						}
					}
				}
			}
		}
	}

Scroll to Top