Filter uncanny-automator

automator_before_background_action_executed

Filters the action object before it's executed in the background.

add_filter( 'automator_before_background_action_executed', $callback, 10, 1 );

Description

Fires before a background action is executed, allowing developers to modify or prevent the action. You can return `false` to skip execution. The `$action` parameter contains the action's data. This hook is crucial for customizing action flow before processing.


Usage

add_filter( 'automator_before_background_action_executed', 'your_function_name', 10, 1 );

Parameters

$action (mixed)
This parameter holds the action data that is about to be processed by the background action system.

Return Value

The filtered value.


Examples

/**
 * Example of using the 'automator_before_background_action_executed' filter hook.
 * This example demonstrates how to conditionally skip a background action
 * based on a specific trigger type.
 *
 * @param array $action The action data array.
 * @return array The potentially modified action data array, or false to skip.
 */
function my_automator_skip_certain_actions( $action ) {

    // Check if the action is related to a specific trigger, e.g., 'user_registered'.
    // Replace 'user_registered' with the actual trigger key you want to target.
    if ( isset( $action['trigger_key'] ) && 'user_registered' === $action['trigger_key'] ) {

        // Further condition: Only skip if the action type is 'send_email'
        // Replace 'send_email' with the actual action key you want to target.
        if ( isset( $action['action_key'] ) && 'send_email' === $action['action_key'] ) {

            // Add a flag to the action to indicate it should be skipped.
            // The core hook will check for 'process_further' and skip if it's false.
            $action['process_further'] = false;

            // Log that the action is being skipped for debugging.
            automator_log( 'Skipping "send_email" action for "user_registered" trigger via automator_before_background_action_executed filter.' );
        }
    }

    // Always return the action data, even if modified.
    // If you want to completely prevent execution, you would typically return false.
    // However, the provided source code specifically checks for 'process_further' set to false.
    return $action;
}
add_filter( 'automator_before_background_action_executed', 'my_automator_skip_certain_actions', 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:463

public function background_action_rest( $request ) {
		// Read request JSON parameters.
		$params = $request->get_json_params();

		$action = $params['action'];

		$action = apply_filters( 'automator_before_background_action_executed', $action );

		if ( isset( $action['process_further'] ) && false === boolval( $action['process_further'] ) ) {
			automator_log( 'Action was skipped by automator_before_background_action_executed filter.' );
			return;
		}

		$this->run_action( $action );
	}

Internal Usage

Found in src/core/lib/utilities/class-automator-input-parser.php:95:

add_filter( 'automator_before_background_action_executed', array( $this, 'attach_trigger_tokens_hook' ), 10, 1 );
Scroll to Top