Action uncanny-automator

automator_action_created

Action DB entry created and status changed. Fires after an AutomatorWP action's database entry is created and its status is updated.

add_action( 'automator_action_created', $callback, 10, 1 );

Description

Fires when a new action's database entry is successfully created and its status is updated. This hook provides access to `$do_action_args`, containing details about the action. Developers can use this to log action creation, trigger further processes, or modify action data before it's finalized.


Usage

add_action( 'automator_action_created', 'your_function_name', 10, 1 );

Parameters

$do_action_args (array)

Examples

<?php
/**
 * Example function to log details about a newly created Automator action.
 *
 * This function hooks into the 'automator_action_created' action and
 * logs the ID of the newly created action and its associated recipe ID.
 *
 * @param array $do_action_args An array containing arguments passed to the 'automator_action_created' hook.
 *                              Expected keys include 'action_id' and 'recipe_id'.
 */
function log_new_automator_action_details( $do_action_args ) {
	if ( ! is_array( $do_action_args ) ) {
		return; // Ensure we have an array to work with.
	}

	$action_id   = isset( $do_action_args['action_id'] ) ? absint( $do_action_args['action_id'] ) : 0;
	$recipe_id = isset( $do_action_args['recipe_id'] ) ? absint( $do_action_args['recipe_id'] ) : 0;

	if ( $action_id > 0 ) {
		// In a real-world scenario, you'd likely use a more robust logging system
		// or a specific WordPress logging plugin. For this example, we'll use
		// error_log for simplicity.
		$log_message = sprintf(
			'Automator Action Created: Action ID %d, Recipe ID %d.',
			$action_id,
			$recipe_id
		);
		error_log( $log_message );
	}
}

// Hook the function to the 'automator_action_created' action.
// The '10' is the priority, and '1' indicates that the function accepts one argument.
add_action( 'automator_action_created', 'log_new_automator_action_details', 10, 1 );

/**
 * Example function to potentially update a meta field based on action creation.
 *
 * This function demonstrates how to react to a new Automator action and potentially
 * update a related post or object's meta data. For instance, if an action is
 * created for a specific user, we might want to add a meta field to that user's
 * profile indicating they have an Automator action pending or completed.
 *
 * @param array $do_action_args An array containing arguments passed to the 'automator_action_created' hook.
 *                              This example assumes it might contain a 'user_id' or similar key.
 * @return array The modified $do_action_args, or original if no modifications made.
 */
function update_related_post_meta_on_action_create( $do_action_args ) {
	if ( ! is_array( $do_action_args ) ) {
		return $do_action_args;
	}

	// Example: If the action creation involves a specific post or user.
	// Let's assume there's a 'target_post_id' in the arguments.
	$target_post_id = isset( $do_action_args['target_post_id'] ) ? absint( $do_action_args['target_post_id'] ) : 0;

	if ( $target_post_id > 0 ) {
		$action_id = isset( $do_action_args['action_id'] ) ? absint( $do_action_args['action_id'] ) : 0;
		if ( $action_id > 0 ) {
			// In a real scenario, you'd fetch the existing meta and merge,
			// or update based on specific logic.
			$existing_meta = get_post_meta( $target_post_id, '_automator_action_ids', true );
			$existing_meta = ! empty( $existing_meta ) ? (array) $existing_meta : array();

			if ( ! in_array( $action_id, $existing_meta, true ) ) {
				$existing_meta[] = $action_id;
				update_post_meta( $target_post_id, '_automator_action_ids', $existing_meta );
			}
		}
	}

	return $do_action_args; // It's good practice to return the arguments, especially if this was a filter.
}

// Hooking this as an action. If it were a filter that modified arguments,
// it would be a filter hook, but 'automator_action_created' is an action hook.
// We still return arguments for consistency and potential future conversion to a filter if needed.
add_action( 'automator_action_created', 'update_related_post_meta_on_action_create', 15, 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/lib/process/class-automator-recipe-process-complete.php:645

);

		/**
		 * Action DB entry created and status changed.
		 *
		 * @param array $do_action_args
		 */
		do_action( 'automator_action_created', $do_action_args );

		/**
		 * Inject `complete_with_notice` to $args.
		 *
		 * @since 4.6
		 */
		if ( ! empty( $action_data['complete_with_notice'] ) && true === $action_data['complete_with_notice'] ) {

Scroll to Top