Action Since 7.0.0 uncanny-automator

automator_agent_before_execute

Fires before action execution. Fires before an API action executes, providing access to action code, fields, user ID, and the action object.

add_action( 'automator_agent_before_execute', $callback, 10, 4 );

Description

Fires just before an API action is executed. Developers can use this hook to inspect or modify the action code, provided fields, user context, or the action object itself before processing begins. It offers a prime opportunity for pre-execution validation or dynamic adjustments.


Usage

add_action( 'automator_agent_before_execute', 'your_function_name', 10, 4 );

Parameters

$action_code (string)
The action code.
$fields (array)
Field values provided.
$user_id (int)
User ID context.
$action (object)
The action instance.

Examples

add_action( 'automator_agent_before_execute', 'my_automator_agent_before_execute_handler', 10, 4 );

/**
 * Example handler for the 'automator_agent_before_execute' action hook.
 *
 * This function demonstrates how to intercept an Automator action before it's executed.
 * In this example, we'll log the action details and conditionally prevent execution
 * if a specific condition is met (e.g., if the user is a specific role).
 *
 * @param string $action_code The action code.
 * @param array  $fields      Field values provided.
 * @param int    $user_id     User ID context.
 * @param object $action      The action instance.
 */
function my_automator_agent_before_execute_handler( $action_code, $fields, $user_id, $action ) {

    // Log the details of the action being executed.
    error_log( sprintf(
        'Automator Agent - Before Execute: Action Code: %s, User ID: %d, Fields: %s',
        $action_code,
        $user_id,
        print_r( $fields, true )
    ) );

    // Example: Prevent execution for administrators in a specific scenario.
    // Replace 'your_specific_action_code' with an actual action code you want to control.
    if ( 'your_specific_action_code' === $action_code ) {
        $user = get_user_by( 'id', $user_id );
        if ( $user && in_array( 'administrator', $user->roles, true ) ) {
            error_log( 'Automator Agent - Skipping execution for administrator for action: ' . $action_code );

            // To prevent execution, you would typically need to hook into a filter
            // that allows you to stop the execution flow. The 'automator_agent_before_execute'
            // hook itself doesn't provide a direct way to *stop* execution.
            // However, in a real-world scenario, you might use a filter like
            // 'automator_allow_action_execution' if one exists, or rely on
            // the action's internal logic to handle such conditions based on the
            // data you might modify or add to the `$fields` if the action supported it.
            // For demonstration, we'll just log the intent to skip.

            // If there was a filter to stop execution, it would look something like:
            // add_filter('automator_allow_action_execution', function($allow, $action_code, $fields, $user_id, $action) {
            //     if ('your_specific_action_code' === $action_code && user_can($user_id, 'administrator')) {
            //         return false; // Prevent execution
            //     }
            //     return $allow;
            // }, 10, 5);
        }
    }

    // You could also modify `$fields` here if the action executor is designed to pick up changes.
    // For example:
    // if ( 'some_other_action' === $action_code ) {
    //     $fields['new_dynamic_value'] = 'processed_value_' . time();
    // }

    // This is an action hook, so no return value is expected or used by the hook itself.
}

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/api/application/sub_tooling/class-action-executor.php:147

* @param string $action_code The action code.
		 * @param array  $fields      Field values provided.
		 * @param int    $user_id     User ID context.
		 * @param object $action      The action instance.
		 *
		 * @since 7.0.0
		 */
		do_action( 'automator_agent_before_execute', $action_code_str, $fields, $user_id, $action );

		// Route to appropriate execution method based on action style.
		if ( method_exists( $action, 'process_action' ) ) {
			$result = $this->invoke_modern_action( $action, $fields, $user_id );
		} else {
			$method = $definition['method'];
			if ( empty( $method ) || ! method_exists( $action, $method ) ) {


Scroll to Top