wppaste
WordPress

is_wp_error( mixed $thing ): bool

Since
2.1.0
Source
wp-includes/load.php:1804

Detects whether a returned value is a WP_Error object so your code can branch on failure instead of misreading false or null. Many WordPress functions such as wp_insert_post() or get_term() return WP_Error on failure and false, null, or an ID on success, so checking is_wp_error() first is the safe way to branch. Since WordPress 5.6.0 it also fires the is_wp_error_instance action every time it finds an actual error, which is handy for centralized logging.

Checks whether the given variable is a WordPress Error.

Description

Returns whether $thing is an instance of the WP_Error class.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$thingmixed
The variable to check.

Return value

bool
Whether the variable is an instance of WP_Error.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check whether wp_insert_post() returned an error

Try to insert a post into a post type that was never registered so wp_insert_post() has to report a failure.

$result = wp_insert_post(
	array(
		'post_title'  => 'Draft created from a snippet',
		'post_type'   => 'wpp_scratch_notes',
		'post_status' => 'publish',
		'wp_error'    => true,
	)
);

if ( is_wp_error( $result ) ) {
	echo esc_html( 'Insert failed: ' . $result->get_error_message() );
} else {
	echo esc_html( 'Created post ID ' . $result );
}

wp_insert_post() only returns a WP_Error instead of 0 when 'wp_error' is passed as true.

Check whether get_term() failed because the taxonomy doesn't exist

Ask for a term from a taxonomy slug that was never registered, which makes get_term() return a WP_Error rather than a WP_Term object.

$term = get_term( 1, 'wpp_not_a_real_taxonomy' );

if ( is_wp_error( $term ) ) {
	echo esc_html( 'Term lookup failed: ' . $term->get_error_message() );
} else {
	echo esc_html( 'Term name: ' . $term->name );
}

Common problems and fixes · 3

Why does an if ( ! $result ) check let a WP_Error slip through as truthy?

A WP_Error object is truthy in PHP, so code that only tests for false, null, or 0 treats it as success and then crashes trying to use it like a post ID or array. is_wp_error() is the only reliable way to catch that case because it explicitly tests for a WP_Error instance rather than a falsy value. - Call is_wp_error( $result ) before doing anything else with the return value. - Only fall back to a falsy check ( ! $result ) for functions that never return WP_Error.

Why does is_wp_error() return false for my own custom error object?

The function does a strict instanceof WP_Error check, so a class that merely looks like WP_Error (same method names, no inheritance) is never recognized. - Extend the WP_Error class instead of duplicating its interface. - Or return a real WP_Error from your function and store extra data with WP_Error::add_data().

How do I log every WordPress error without adding is_wp_error() checks everywhere?

Since WordPress 5.6.0, is_wp_error() fires the is_wp_error_instance action every time it is passed an actual WP_Error, right before it returns true. Hooking that action lets you log or report errors centrally instead of instrumenting every call site. add_action( 'is_wp_error_instance', function( $thing ) { error_log( 'WP_Error seen: ' . $thing->get_error_message() ); } );

Alternatives and related functions

WP_Error::get_error_message
When you already know a value is a WP_Error and just need the human-readable message text out of it.
WP_Error::has_errors
When you're holding a WP_Error instance that might have been created empty and need to know if it actually contains any errors.
is_a
When you need to test an object against a class other than WP_Error, since is_wp_error() is hardcoded to that one class.

Performance profile

How much work a call to is_wp_error() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4–8

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 8.

Plugin surface
1 hook

Third-party callbacks on 'is_wp_error_instance' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost is_wp_error() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!($thing instanceof)4none
$thing instanceof8do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 8 instructions, 4–8 executed per call, 1 branch. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while is_wp_error() runs, in this order:

  1. do_action( is_wp_error_instance )actionline 1815 (+11 into the body)

    Fires when `is_wp_error()` is called and its parameter is an instance of WP_Error.

Uses · 1

  • do_action()Calls the callback functions that have been added to an action hook.

Used by · 50

Show all 50

Source code

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;}

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/load.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.