Filter uncanny-automator-pro

automator_pro_call_a_custom_function_return_value

Filters the return value of a custom function executed within the Run Code integration.

add_filter( 'automator_pro_call_a_custom_function_return_value', $callback, 10, 2 );

Description

Allows developers to modify the return value of a custom function executed via the Run Code action. This filter is applied after the function has been called, enabling developers to process or alter the output before it's used by the automation. The current automation object is also provided for context.


Usage

add_filter( 'automator_pro_call_a_custom_function_return_value', 'your_function_name', 10, 2 );

Parameters

$return_value (mixed)
This parameter contains the value returned by the custom function that was called, and it can be modified by other filters before being returned by the action.
$this (mixed)
This parameter holds the current return value of the custom function being called, which can be modified by other filters.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the return value of a custom function called by Uncanny Automator Pro's Run Code action.
 * This example demonstrates how to check if the return value is a WordPress Post object and, if so,
 * extract and return its title. Otherwise, it returns the original value.
 */
add_filter(
	'automator_pro_call_a_custom_function_return_value',
	function ( $return_value, $automator_run_code_instance ) {
		// Check if the returned value is a WP_Post object.
		if ( $return_value instanceof WP_Post ) {
			// If it's a post, return its title.
			return $return_value->post_title;
		}

		// Otherwise, return the original return value.
		return $return_value;
	},
	10, // Priority
	2  // Accepted arguments: $return_value, $this (the Automator instance)
);

?>

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

uncanny-automator-pro/src/integrations/run-code/actions/run-code-call-function-everyone.php:121

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {

		$function_name = isset( $parsed[ $this->get_action_meta() ] ) ? $parsed[ $this->get_action_meta() ] : '';

		// Check if the function exists.
		if ( function_exists( $function_name ) ) {

			try {

				$function_args = $this->hydrate_field_values( $action_data, $user_id, $recipe_id, $args );

				// The parameters to be passed to the callback, as an indexed array.
				$callback_args = array();

				foreach ( $function_args as $function_arg ) {
					$callback_args[] = $this->parse( $function_arg );
				}

				// Run the function.
				$return_value = call_user_func_array( $function_name, $callback_args );
				// Give some filters.
				$return_value = apply_filters( 'automator_pro_call_a_custom_function_return_value', $return_value, $this );
				// Send the tokens.
				$this->hydrate_tokens(
					array(
						'RETURN_VALUE' => $return_value,
					)
				);

				Automator()->complete->action( $user_id, $action_data, $recipe_id );

			} catch ( Exception $e ) {

				$action_data['complete_with_errors'] = true;
				Automator()->complete->action( $user_id, $action_data, $recipe_id, $e->getMessage() );

			}
		} else {

			// Log the error if the function does not exist.
			$action_data['complete_with_errors'] = true;

			$error = sprintf(
			/* translators: Function is not defined error message. */
				esc_html__(
					'The function/method (%s) you are trying to call is not found or not yet registered.',
					'uncanny-automator-pro'
				),
				$function_name
			);

			Automator()->complete->action( $user_id, $action_data, $recipe_id, $error );

		}

	}

Scroll to Top