is_wp_error()WP 2.1.0

Checks whether the passed variable is a WordPress Error.

It is used in WordPress to check whether the returned value of the function is a WordPress Error. Such errors have a description that can be used to find out the reason for the error. For example, wp_insert_term() function may return a WP_Error, and you can use its description to understand what has gone wrong.

Hooks from the function

Return

true|false. Whether the variable is an instance of WP_Error.

Usage

is_wp_error( $thing );
$thing(mixed) (required)
The variable to check.

Examples

0

#1 Handle a WordPress error

An example of handling a WordPress error passed in the WP_Error object. If the error is specified, display a message about it:

// $result - is some variable that may be a WP_Error.

if ( is_wp_error( $result ) ) {

	$error_string = $result->get_error_message();

	echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';

	echo $res->get_error_code(); // -> 'error' or some other error key
}
0

#2 Example with wp_insert_user()

$email = '[email protected]';
$userdata = [
	'user_login'    => $email,
	'user_email'    => $email,
];

$user_id = wp_insert_user( $userdata );

if ( is_wp_error( $user_id ) ) {
	$error_message = $user_id->get_error_message();
}

Changelog

Since 2.1.0 Introduced.

is_wp_error() code WP 6.5.2

function is_wp_error( $thing ) {
	$is_wp_error = ( $thing instanceof WP_Error );

	if ( $is_wp_error ) {
		/**
		 * Fires when `is_wp_error()` is called and its parameter is an instance of `WP_Error`.
		 *
		 * @since 5.6.0
		 *
		 * @param WP_Error $thing The error object passed to `is_wp_error()`.
		 */
		do_action( 'is_wp_error_instance', $thing );
	}

	return $is_wp_error;
}