_doing_it_wrong()
Mark something as being incorrectly called.
There is a doing_it_wrong_run hook that will be called that can be used to get the backtrace up to what file and function called the deprecated function.
The current behavior is to trigger a user error if WP_DEBUG is true.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
Hooks from the function
Return
null
. Nothing (null).
Usage
_doing_it_wrong( $function_name, $message, $version );
- $function_name(string) (required)
- The function that was called.
- $message(string) (required)
- A message explaining what has been done incorrectly.
- $version(string) (required)
- The version of WordPress where the message was added.
Examples
#1 Show the error when the function is called on the wrong hook.
This example is from WP core. It tells the user that he called the REST route registration function not on the rest_api_init hook, as it should be done.
if ( ! did_action( 'rest_api_init' ) ) { _doing_it_wrong( 'register_rest_route', sprintf( /* translators: %s: rest_api_init */ __( 'REST API routes must be registered on the %s action.' ), '<code>rest_api_init</code>' ), '5.1.0' ); }
See code register_rest_route().
#2 Show the error when there is no global variable in the function
In this example, the function is_archive() needs the global variable $wp_query, which means that the function must be called after this variable is defined. If it is called before, we will print an error about it.
function is_archive() { global $wp_query; if ( ! isset( $wp_query ) ) { _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); return false; } return $wp_query->is_archive(); }
Changelog
Since 3.1.0 | Introduced. |
Since 5.4.0 | This function is no longer marked as "private". |