wp_trigger_error()
Creates a custom error, warning, notice, or deprecation message.
The function works only when WP_DEBUG is enabled. With debugging enabled it creates a PHP error through trigger_error(). When debugging is disabled, no PHP error is created, but the wp_trigger_error_always_run action still fires.
Uses: wp_kses()
Hooks from the function
Returns
null.
void- returns nothing.
Usage
wp_trigger_error( $function_name, $message, $error_level );
- $function_name(string) (required)
- Name of the function or method that triggered the error. It is prepended to the message as
function_name(): message. - $message(string) (required)
- Error description. The tags
<a href="">,<code>,<br>,<em>, and<strong>, plus HTTP and HTTPS protocols, are allowed. - $error_level(int)
Error level. Only
E_USER_NOTICE,E_USER_WARNING,E_USER_ERROR, andE_USER_DEPRECATEDare supported. AtE_USER_ERROR, the function throws WP_Exception.Default:
E_USER_NOTICE
Examples
#1 Display a configuration warning
function my_plugin_validate_config( array $config ): void {
if ( empty( $config['api_key'] ) ) {
wp_trigger_error(
__FUNCTION__,
'The API key is not configured.',
E_USER_WARNING
);
}
} #2 Intercept the message regardless of WP_DEBUG
The wp_trigger_error_always_run hook runs even when debugging is disabled.
add_action( 'wp_trigger_error_always_run', 'my_plugin_log_wp_error', 10, 3 );
function my_plugin_log_wp_error( string $function_name, string $message, int $error_level ): void {
error_log(
sprintf(
'[%d] %s(): %s',
$error_level,
$function_name,
$message
)
);
}
Changelog
| Since 6.4.0 | Introduced. |