_doing_it_wrong() WordPress Function

The doing_it_wrong() function is used to log a message that indicates that a developer has done something wrong. This function is useful for debugging purposes, and can be used to help improve code quality.

_doing_it_wrong( string $function, string $message, string $version ) #

Mark something as being incorrectly called.


Description

There is a hook ‘doing_it_wrong_run’ 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.


Top ↑

Parameters

$function

(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.


Top ↑

Source

File: wp-includes/functions.php

function _doing_it_wrong( $function, $message, $version ) {

	/**
	 * Fires when the given function is being used incorrectly.
	 *
	 * @since 3.1.0
	 *
	 * @param string $function 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.
	 */
	do_action( 'doing_it_wrong_run', $function, $message, $version );

	/**
	 * Filters whether to trigger an error for _doing_it_wrong() calls.
	 *
	 * @since 3.1.0
	 * @since 5.1.0 Added the $function, $message and $version parameters.
	 *
	 * @param bool   $trigger  Whether to trigger the error for _doing_it_wrong() calls. Default true.
	 * @param string $function 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.
	 */
	if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true, $function, $message, $version ) ) {
		if ( function_exists( '__' ) ) {
			if ( $version ) {
				/* translators: %s: Version number. */
				$version = sprintf( __( '(This message was added in version %s.)' ), $version );
			}

			$message .= ' ' . sprintf(
				/* translators: %s: Documentation URL. */
				__( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
				__( 'https://wordpress.org/support/article/debugging-in-wordpress/' )
			);

			trigger_error(
				sprintf(
					/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: WordPress version number. */
					__( 'Function %1$s was called <strong>incorrectly</strong>. %2$s %3$s' ),
					$function,
					$message,
					$version
				),
				E_USER_NOTICE
			);
		} else {
			if ( $version ) {
				$version = sprintf( '(This message was added in version %s.)', $version );
			}

			$message .= sprintf(
				' Please see <a href="%s">Debugging in WordPress</a> for more information.',
				'https://wordpress.org/support/article/debugging-in-wordpress/'
			);

			trigger_error(
				sprintf(
					'Function %1$s was called <strong>incorrectly</strong>. %2$s %3$s',
					$function,
					$message,
					$version
				),
				E_USER_NOTICE
			);
		}
	}
}


Top ↑

Changelog

Changelog
VersionDescription
5.4.0This function is no longer marked as "private".
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More