Action uncanny-automator

automator_bg_action_after_run

Fires after an AutomatorWP background action successfully completes its execution.

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

Description

Fires after a background action has successfully run. Developers can use this hook to perform cleanup, logging, or trigger follow-up processes based on the completed action. The `$action` parameter contains details about the action that was just executed.


Usage

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

Parameters

$action (mixed)
This parameter contains the action object that was just executed in the background.

Examples

<?php
/**
 * Example function to hook into the 'automator_bg_action_after_run' action.
 * This function will be executed after a background action has been processed.
 * It logs the action details to the WordPress debug log.
 *
 * @param array $action The processed action data.
 */
function my_automator_log_background_action_completion( $action ) {
	// Ensure we are in a context where logging is appropriate, e.g., not during frontend rendering.
	if ( ! is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() ) {
		return;
	}

	// Log the action details to the WordPress debug log.
	// This is useful for debugging and monitoring background processes.
	if ( WP_DEBUG === true && WP_DEBUG_LOG === true ) {
		$action_id   = isset( $action['ID'] ) ? $action['ID'] : 'unknown';
		$action_type = isset( $action['type'] ) ? $action['type'] : 'unknown';
		$recipe_id   = isset( $action['recipe_id'] ) ? $action['recipe_id'] : 'unknown';

		$log_message = sprintf(
			'Automator background action completed: Recipe ID %1$s, Action ID %2$s (%3$s). Timestamp: %4$s',
			$recipe_id,
			$action_id,
			$action_type,
			date( 'Y-m-d H:i:s' )
		);

		error_log( $log_message );

		// Optionally, log more detailed information if needed for debugging.
		// error_log( print_r( $action, true ) );
	}
}

// Add the action hook with the callback function.
// The 'automator_bg_action_after_run' hook passes one argument: the $action array.
add_action( 'automator_bg_action_after_run', 'my_automator_log_background_action_completion', 10, 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/classes/class-background-actions.php:496

public function run_action( $action ) {

		$action_code = $this->get_action_code( $action );

		$action_execution_function = Automator()->get->action_execution_function_from_action_code( $action_code );

		if ( isset( $action['process_further'] ) ) {
			unset( $action['process_further'] );
		}

		$action['action_data']['background_action_processed'] = time();

		try {
			call_user_func_array( $action_execution_function, $action );
			do_action( 'automator_bg_action_after_run', $action );
		} catch ( Error $e ) {
			$this->complete_with_error( $action, $e->getMessage() );
		} catch ( Exception $e ) {
			$this->complete_with_error( $action, $e->getMessage() );
		}
	}

Scroll to Top