Filter uncanny-automator

automator_api_register_actions

Allow additional actions to be registered. Filters actions that can be registered with the Automator API, allowing modification of existing or adding new ones.

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

Description

Filters the array of actions registered with the Automator API. Developers can add their own custom actions to the Automator platform by returning an array where keys are unique action codes and values are action objects. This hook fires during the API's action registration process.


Usage

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

Parameters

$actions (array)
Existing actions array.

Return Value

The filtered value.


Examples

<?php
/**
 * Registers a custom action for the Automator plugin.
 *
 * This function adds a new action to the Automator plugin's API,
 * allowing it to be used in automations. The example registers an action
 * that sends an email to a specified recipient.
 *
 * @param array $existing_actions The array of currently registered actions.
 * @return array The modified array of actions, including the new custom action.
 */
add_filter( 'automator_api_register_actions', 'my_custom_automator_action', 10, 1 );

function my_custom_automator_action( $existing_actions ) {
    // Define the structure of our custom action.
    // 'label' is what appears in the Automator UI.
    // 'description' provides more detail.
    // 'handler' is the callback function that will be executed when the action is triggered.
    // 'settings' defines the fields that will be available for configuring this action in the UI.
    $custom_action = array(
        'label'       => __( 'Send Custom Email', 'your-text-domain' ),
        'description' => __( 'Sends a personalized email to a specified recipient.', 'your-text-domain' ),
        'handler'     => 'my_custom_email_handler',
        'settings'    => array(
            array(
                'id'          => 'recipient_email',
                'label'       => __( 'Recipient Email', 'your-text-domain' ),
                'type'        => 'text',
                'required'    => true,
                'description' => __( 'Enter the email address of the recipient.', 'your-text-domain' ),
            ),
            array(
                'id'          => 'email_subject',
                'label'       => __( 'Email Subject', 'your-text-domain' ),
                'type'        => 'text',
                'required'    => true,
                'description' => __( 'Enter the subject of the email.', 'your-text-domain' ),
            ),
            array(
                'id'          => 'email_body',
                'label'       => __( 'Email Body', 'your-text-domain' ),
                'type'        => 'textarea',
                'required'    => true,
                'description' => __( 'Enter the content of the email.', 'your-text-domain' ),
            ),
        ),
    );

    // Add our custom action to the array of existing actions.
    // The key 'my_custom_email_action' is a unique identifier for this action.
    $existing_actions['my_custom_email_action'] = $custom_action;

    return $existing_actions;
}

/**
 * Handles the execution of the custom email action.
 *
 * This function is called by the Automator plugin when the 'my_custom_email_action'
 * is triggered. It sends an email using the provided settings.
 *
 * @param array $settings The settings configured for this action.
 * @return bool True on success, false on failure.
 */
function my_custom_email_handler( $settings ) {
    // Basic validation of settings.
    if ( empty( $settings['recipient_email'] ) || empty( $settings['email_subject'] ) || empty( $settings['email_body'] ) ) {
        // Log an error or handle it appropriately if essential settings are missing.
        error_log( 'My Custom Automator Action: Missing required email settings.' );
        return false;
    }

    $to      = sanitize_email( $settings['recipient_email'] );
    $subject = sanitize_text_field( $settings['email_subject'] );
    $body    = wp_kses_post( $settings['email_body'] ); // Sanitize HTML content for email body.

    // Set default headers for a HTML email.
    $headers = array( 'Content-Type: text/html; charset=UTF-8' );

    // Attempt to send the email.
    $mail_sent = wp_mail( $to, $subject, $body, $headers );

    if ( $mail_sent ) {
        // Log success if needed for debugging.
        // error_log( 'My Custom Automator Action: Email sent successfully to ' . $to );
        return true;
    } else {
        // Log failure for debugging.
        error_log( 'My Custom Automator Action: Failed to send email to ' . $to );
        return false;
    }
}

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/action/registry/class-wp-action-registry.php:221

private function load_actions_from_wordpress(): void {
		// Get actions from the existing Automator system
		$wp_actions = Automator()->get_actions();

		foreach ( $wp_actions as $code => $action ) {
			$this->actions[ $code ] = $this->normalize_action_definition( $action );
		}

		/**
		 * Allow additional actions to be registered.
		 *
		 * @param array $actions Existing actions array.
		 */
		$additional_actions = apply_filters( 'automator_api_register_actions', array() );

		foreach ( $additional_actions as $code => $action ) {
			$this->register_action( $code, $action );
		}
	}

Scroll to Top