WordPress at Your Fingertips

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

#1 Handle a WordPress error

// $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
}

Changelog

Since 2.1.0 Introduced.

Code of is_wp_error() WP 5.8.1

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;
}
No comments
    Log In