Filter uncanny-automator

automator_error_messages

Filters all error messages before a specific error message is set Filters all error messages before they are set for processing.

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

Description

Modify or customize core error messages before they are displayed. This filter allows developers to dynamically alter error strings, add context, or localize messages based on specific needs or integrations. Hook into `automator_error_messages` to access and manipulate the error message array.


Usage

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

Parameters

$this (mixed)

Return Value

The filtered value.


Examples

/**
 * Filters all error messages before a specific error message is set.
 *
 * This example demonstrates how to add a custom, more detailed error message
 * for a specific error key related to user role validation.
 *
 * @param array $error_messages An array of all registered error messages.
 *
 * @return array The modified array of error messages.
 */
add_filter(
	'automator_error_messages',
	function ( $error_messages ) {
		// Define a new error message for a hypothetical 'user_role_missing' key.
		// This could be triggered if a recipe requires a specific user role and it's not found.
		$error_messages['user_role_missing'] = __( 'The required user role for this action is not assigned to the current user.', 'uncanny-automator' ) . ' Please ensure the user has the appropriate role.';

		// You could also modify existing messages. For example, if you wanted to
		// prepend a global warning to all messages:
		// foreach ( $error_messages as $key => $message ) {
		//     $error_messages[ $key ] = '[WARNING] ' . $message;
		// }

		return $error_messages;
	},
	10, // Priority: Default is 10.
	1   // Accepted args: The number of arguments the callback function accepts.
);

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/error/class-automator-error-messages.php:29
src/core/lib/utilities/error/class-automator-error-messages.php:71

public function get( $error_key = null, $additional_information = '' ) {

		/**
		 * Filters all error messages before a specific error message is set
		 */
		$error_messages = apply_filters( 'automator_error_messages', $this->error_messages );
		if ( ! isset( $error_messages[ $error_key ] ) ) {
			return esc_html__( 'No message', 'uncanny-automator' );
		}
		$error_message = $error_messages[ $error_key ] . $additional_information;

		/**
		 * Filters the specific error message
		 */
		return apply_filters( 'automator_error_message', $error_message, $error_key, $additional_information );
	}


Scroll to Top