Filter uncanny-automator

automator_get_action_sentence

Filters the sentence that describes an Automator action before it's displayed. This hook allows for modification of action descriptions.

add_filter( 'automator_get_action_sentence', $callback, 10, 3 );

Description

Fires after an action sentence is generated. Developers can modify the generated sentence, including translating, rephrasing, or providing alternative outputs based on the action ID or type. This hook is crucial for customizing how actions are displayed in the Automator plugin.


Usage

add_filter( 'automator_get_action_sentence', 'your_function_name', 10, 3 );

Parameters

$sentence (mixed)
This parameter contains the dynamically generated sentence representing the action, which can be modified by the filter.
$id (mixed)
The sentence built so far representing the action.
$type (mixed)
The ID of the action or trigger.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the 'automator_get_action_sentence' hook.
 * This function modifies the generated sentence for a specific action type.
 *
 * @param mixed  $sentence The original sentence data (often an array of sentences keyed by type).
 * @param mixed  $id       The ID of the action.
 * @param mixed  $type     The type of the action.
 *
 * @return mixed The potentially modified sentence data.
 */
function my_custom_automator_action_sentence( $sentence, $id, $type ) {
    // Let's assume $sentence is an array where keys are action types and values are the sentences.
    // For example: $sentence = [ 'email' => 'Send email to %user_email%', 'post_status' => 'Change post status to %post_status%' ];

    // We want to modify the sentence for a specific action type, let's say 'custom_integration'.
    $target_action_type = 'custom_integration';

    if ( $type === $target_action_type ) {
        // Check if the sentence for our target type already exists.
        if ( isset( $sentence[ $target_action_type ] ) ) {
            // Modify the existing sentence.
            // Let's prepend some text to it.
            $sentence[ $target_action_type ] = 'My custom integration action: ' . $sentence[ $target_action_type ];
        } else {
            // If the sentence for this type doesn't exist, we can add it.
            // This might be useful if your custom integration adds new types of sentences.
            $sentence[ $target_action_type ] = 'This is a brand new sentence for the custom integration action!';
        }
    }

    // Always return the $sentence variable, whether it was modified or not.
    return $sentence;
}

// Add the filter to WordPress.
// We specify 3 accepted arguments because the hook provides $sentence, $id, and $type.
add_filter( 'automator_get_action_sentence', 'my_custom_automator_action_sentence', 10, 3 );

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/utilities/class-automator-get-data.php:236

public function action_sentence( $id, $type = 'all' ) {

		global $wpdb;

		if ( 0 === absint( $id ) ) {
			return '';
		}

		$action_meta = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d",
				$id
			)
		);

		$sentence = $this->get_trigger_action_sentence( $id );
		$sentence = apply_filters_deprecated(
			'get_action_sentence',
			array(
				$sentence,
				$type,
				$action_meta,
			),
			'3.0',
			'automator_get_action_sentence'
		);
		$sentence = apply_filters( 'automator_get_action_sentence', $sentence, $id, $type );

		if ( 'all' === $type ) {
			return $sentence;
		}

		if ( in_array( $type, array_keys( $sentence ), true ) ) {
			return $sentence[ $type ];
		}

		return $sentence;
	}

Scroll to Top