Filter uncanny-automator

automator_triggers

Filters available triggers for automations, allowing modification before they are used.

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

Description

Fires when the plugin initializes triggers. Developers can use this filter to add, remove, or modify registered triggers. This hook is crucial for extending the plugin's automation capabilities by integrating custom trigger types or altering existing ones before they are made available.


Usage

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

Parameters

$this (mixed)
This parameter contains the current integrations data for the automator.

Return Value

The filtered value.


Examples

/**
 * Filters the list of available triggers to add a custom trigger.
 *
 * This example demonstrates how to add a new trigger for when a user comments on a specific post.
 *
 * @param array $triggers An array of existing triggers.
 * @return array The modified array of triggers, including the new custom trigger.
 */
add_filter( 'automator_triggers', function( $triggers ) {

    // Define the details of the new custom trigger.
    $custom_trigger = array(
        'name'              => __( 'User comments on specific post', 'your-text-domain' ),
        'singular_name'     => __( 'User comments on specific post', 'your-text-domain' ),
        'icon'              => 'dashicons dashicons-editor-comment', // WordPress dashicon
        'description'       => __( 'Triggers when a user comments on a specified post.', 'your-text-domain' ),
        'edit_label'        => __( 'Comment on Post', 'your-text-domain' ),
        'options'           => array(
            'post_id' => array(
                'type'        => 'text',
                'label'       => __( 'Post ID', 'your-text-domain' ),
                'placeholder' => __( 'Enter the Post ID', 'your-text-domain' ),
                'required'    => true,
            ),
        ),
        'function'          => function( $trigger, $recipe ) {
            // This function would contain the logic to check if the trigger condition is met.
            // For example, you would hook into the 'comment_post' action and check if
            // the comment's post_id matches the 'post_id' option from the trigger.
            // If it matches, you would then trigger the recipe.
            // This is a simplified representation for the example.

            // In a real scenario, you'd likely store the trigger details and recipe ID
            // and then have a separate function hooked into 'comment_post' that checks
            // against these stored values to execute the recipe.
            error_log( 'Custom trigger function executed for trigger: ' . print_r( $trigger, true ) );
            return true; // Placeholder: In a real implementation, this would return true if conditions are met.
        },
        'function_to_run'   => 'comment_post', // Example of an action to hook into.
        'edit_function'     => function( $trigger ) {
            // This function would render the UI for editing the trigger options.
            ?>
            <input type="text" name="post_id" value="<?php echo esc_attr( $trigger['options']['post_id'] ); ?>" placeholder="<?php esc_attr_e( 'Enter the Post ID', 'your-text-domain' ); ?>">
            <?php
        },
    );

    // Add the custom trigger to the array of triggers.
    $triggers[] = $custom_trigger;

    return $triggers;
}, 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/lib/class-automator-functions.php:348

public function filter_recipe_parts() {

		$this->integrations = apply_filters_deprecated( 'uap_integrations', array( $this->integrations ), '3.0', 'automator_integrations' );
		$this->integrations = apply_filters( 'automator_integrations', $this->integrations );

		$this->actions = apply_filters_deprecated( 'uap_actions', array( $this->actions ), '3.0', 'automator_actions' );
		$this->actions = apply_filters( 'automator_actions', $this->actions );

		$this->triggers = apply_filters_deprecated( 'uap_triggers', array( $this->triggers ), '3.0', 'automator_triggers' );
		$this->triggers = apply_filters( 'automator_triggers', $this->triggers );

		$this->closures = apply_filters_deprecated( 'uap_closures', array( $this->closures ), '3.0', 'automator_closures' );
		$this->closures = apply_filters( 'automator_closures', $this->closures );

		$this->recipe_items = apply_filters( 'automator_recipe_items', $this->recipe_items );

		$this->all_integrations = apply_filters( 'automator_all_integrations', $this->all_integrations );

		$this->recipe_types = apply_filters_deprecated( 'uap_recipe_types', array( $this->recipe_types ), '3.0', 'automator_recipe_types' );
		$this->recipe_types = apply_filters( 'automator_recipe_types', $this->recipe_types );
	}


Internal Usage

Found in src/core/lib/recipe-parts/triggers/abstract-trigger.php:200:

add_filter( 'automator_triggers', array( $this, 'register_trigger' ) );
Scroll to Top