Filter uncanny-automator

automator_core_api_triggers

Filters the available API triggers within the Automator Core, allowing for customization and addition of new triggers.

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

Description

This filter allows developers to modify the array of available API triggers before they are registered. Use it to add, remove, or alter existing triggers, enabling custom automation actions or integrating third-party services into the API trigger system. It fires after Automator's default triggers are loaded.


Usage

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

Parameters

$this (mixed)
This parameter contains the current collection of registered triggers, allowing for modification before they are loaded.

Return Value

The filtered value.


Examples

/**
 * Add a custom trigger to the Automator Core API triggers.
 *
 * This example demonstrates how to add a new trigger definition to the
 * 'automator_core_api_triggers' filter. The custom trigger is defined
 * with a title, description, and an action callback.
 *
 * @param array $current_triggers An array of existing trigger definitions.
 * @return array The modified array of trigger definitions including the new custom trigger.
 */
add_filter( 'automator_core_api_triggers', function( $current_triggers ) {

    // Define a unique identifier for our custom trigger.
    $custom_trigger_id = 'my_custom_plugin_new_user_registered';

    // Ensure we don't overwrite an existing trigger with the same ID.
    if ( ! isset( $current_triggers[ $custom_trigger_id ] ) ) {
        $current_triggers[ $custom_trigger_id ] = [
            'id'          => $custom_trigger_id,
            'title'       => __( 'New User Registered (Custom Plugin)', 'my-custom-plugin' ),
            'description' => __( 'Fires when a new user registers through your custom plugin.', 'my-custom-plugin' ),
            'action'      => [
                'type' => 'function',
                'callback' => 'my_custom_plugin_new_user_registered_trigger_handler',
            ],
            // You can add more properties here as needed, e.g., 'options', 'args', etc.
            // For example, to pass the user ID:
            // 'args' => [
            //     [
            //         'name'        => 'user_id',
            //         'type'        => 'integer',
            //         'description' => __( 'The ID of the newly registered user.', 'my-custom-plugin' ),
            //     ],
            // ],
        ];
    }

    return $current_triggers;
}, 10, 1 );

/**
 * Callback function for the custom trigger.
 * This function will be executed when the 'my_custom_plugin_new_user_registered' trigger fires.
 *
 * In a real-world scenario, this function would likely be defined in a separate file
 * or within your plugin's main class.
 *
 * @param array $trigger_data The data passed with the trigger.
 */
if ( ! function_exists( 'my_custom_plugin_new_user_registered_trigger_handler' ) ) {
    function my_custom_plugin_new_user_registered_trigger_handler( $trigger_data ) {
        // Your custom logic here.
        // For example, you might log the event or send a notification.
        // $user_id = $trigger_data['args']['user_id'] ?? null;
        // if ( $user_id ) {
        //     error_log( 'Custom plugin triggered: New user registered with ID: ' . $user_id );
        // }
    }
}

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/components/trigger/registry/class-wp-trigger-registry.php:220

private function load_triggers_from_wordpress(): void {
		// Load triggers from existing Automator registry (legacy compatibility)
		$this->load_from_automator_functions();

		// Allow other plugins/themes to register additional triggers via filters
		$this->triggers = apply_filters( 'automator_core_api_triggers', $this->triggers );
	}

Scroll to Top