doing_it_wrong_trigger_error
Allows you to prevent an error from being generated when _doing_it_wrong() is called.
It is used to control and handle situations in which a function is called or functionality is used incorrectly or is considered obsolete.
It allows you to:
-
Track errors:
When a function is called incorrectly or a deprecated method is used, WordPress fires this hook through _doing_it_wrong(). This helps developers discover potential errors and improve their code. -
Log errors:
The hook can record these errors in a log file, database, or another destination. This is especially useful during development and testing. - Change behavior:
Developers can change the default behavior when an error or deprecated feature is detected. For example, an admin notice can be displayed instead of generating an error, or a notification can be sent to the developer.
How does it work?
It works only when the WP_DEBUG option (constant) is enabled.
When _doing_it_wrong() is called, this hook determines whether an error should be generated:
-
If the hook returns true, an error message is created and wp_trigger_error() is called. By default, that function calls PHP's standard
trigger_error()function. - If the hook returns false, WordPress does not handle this type of error.
Usage
add_filter( 'doing_it_wrong_trigger_error', 'wp_kama_doing_it_wrong_trigger_error_filter', 10, 4 );
/**
* Function for `doing_it_wrong_trigger_error` filter-hook.
*
* @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls.
* @param string $function_name The function that was called.
* @param string $message A message explaining what has been done incorrectly.
* @param string $version The version of WordPress where the message was added.
*
* @return bool
*/
function wp_kama_doing_it_wrong_trigger_error_filter( $trigger, $function_name, $message, $version ){
// filter...
return $trigger;
}
- $trigger(true|false)
- Whether to generate the error.
Default: true - $function_name(string)
- Function that was called.
- $message(string)
- Message explaining what was done incorrectly.
- $version(string)
- WordPress version in which the message was added.
Examples
#1 Simple usage example
// Disable the default error handler.
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
// Log the message using custom logic.
add_action( 'doing_it_wrong_run', function ( $function, $message, $version ) {
error_log( "Function: $function — Error: $message (since version: $version)" );
} );
Or implement the same behavior with the filter alone:
add_filter( 'doing_it_wrong_trigger_error', function ( $trigger, $function, $message, $version ) {
// Log the message instead of displaying the error.
error_log( "Function: $function — Error: $message (since version: $version)" );
return false; // Disable the standard trigger_error behavior.
}, 10, 4 );
Possible use cases:
- Tracking deprecated function usage while migrating to a new WordPress version.
- Developing complex plugins or themes where current standards must be followed.
- Configuring centralized logging for all code rule violations.
This hook can make code more robust and better aligned with WordPress best practices.
#2 Suppress the load_textdomain_just_in_time() notice
If errors like the following are unwanted:
PHP Notice: Function _load_textdomain_just_in_time was called <strong>incorrectly</strong>. Translation loading for the <code>sg-cachepress</code> domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the <code>init</code> action or later. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 6.7.0.) in G:\server\www\site.loc\wp-includes\functions.php on line 6114
This code suppresses such notices for _load_textdomain_just_in_time():
add_filter( 'doing_it_wrong_trigger_error', function ( $bool, $function_name ) {
if ( '_load_textdomain_just_in_time' === $function_name ) {
return false;
}
return $bool;
}, 10, 2 );
#3 Find where _doing_it_wrong() was called in the code
Changelog
| Since 3.1.0 | Introduced. |
| Since 5.1.0 | Added the $function_name, $message, and $version parameters. |
Where the hook is called
if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true, $function_name, $message, $version ) ) {
Where the hook is used in WordPress
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );