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.
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?
- Why does is_wp_error() return false for my own custom error object?
- How do I log every WordPress error without adding is_wp_error() checks everywhere?
Why does an if ( ! $result ) check let a WP_Error slip through as truthy?
Why does is_wp_error() return false for my own custom error object?
How do I log every WordPress error without adding is_wp_error() checks everywhere?
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
- Scaling
- Constant
- Instructions
- 4–8
- Plugin surface
- 1 hook
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 8.
Third-party callbacks on 'is_wp_error_instance' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
!($thing instanceof) | 4 | none |
$thing instanceof | 8 | do_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:
- 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
- Automatic_Upgrader_Skin::feedback()Stores a message about the upgrade.
- Bulk_Upgrader_Skin::after()Performs an action following a bulk update.
- Bulk_Upgrader_Skin::error()Displays an error message about the update.
- Core_Upgrader::upgrade()Upgrades WordPress core.
- Custom_Image_Header::ajax_header_crop()Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- Custom_Image_Header::step_3()Displays third step of custom header image page.
- Featured_Content::validate_settings()Validates featured content settings.
- Language_Pack_Upgrader::check_package()Checks that the package source contains .mo and .po files.
- Plugin_Installer_Skin::after()Performs an action following a plugin install.
- Plugin_Installer_Skin::do_overwrite()Checks if the plugin can be overwritten and outputs the HTML for overwriting a plugin on upload.
- Plugin_Upgrader::active_after()Turns off maintenance mode after upgrading an active plugin.
Show all 50
- Plugin_Upgrader::active_before()Turns on maintenance mode before attempting to background update an active plugin.
- Plugin_Upgrader::bulk_upgrade()Upgrades several plugins at once.
- Plugin_Upgrader::check_package()Checks that the source package contains a valid plugin.
- Plugin_Upgrader::deactivate_plugin_before_upgrade()Deactivates a plugin before it is upgraded.
- Plugin_Upgrader::delete_old_plugin()Deletes the old plugin during an upgrade.
- Plugin_Upgrader::install()Install a plugin package.
- Plugin_Upgrader::upgrade()Upgrades a plugin.
- Plugin_Upgrader_Skin::after()Performs an action following a single plugin update.
- Theme_Installer_Skin::after()Performs an action following a single theme install.
- Theme_Installer_Skin::do_overwrite()Checks if the theme can be overwritten and outputs the HTML for overwriting a theme on upload.
- Theme_Upgrader::bulk_upgrade()Upgrades several themes at once.
- Theme_Upgrader::check_package()Checks that the package source contains a valid theme.
- Theme_Upgrader::check_parent_theme_filter()Checks if a child theme is being installed and its parent also needs to be installed.
- Theme_Upgrader::current_after()Turns off maintenance mode after upgrading the active theme.
- Theme_Upgrader::current_before()Turns on maintenance mode before attempting to upgrade the active theme.
- Theme_Upgrader::delete_old_theme()Deletes the old theme during an upgrade.
- Theme_Upgrader::install()Install a theme package.
- Theme_Upgrader::upgrade()Upgrades a theme.
- Theme_Upgrader_Skin::after()Performs an action following a single theme update.
- WP_AI_Client_Ability_Function_Resolver::execute_ability()Executes a WordPress ability from a function call.
- WP_AI_Client_HTTP_Client::sendRequest()Sends a PSR-7 request and returns a PSR-7 response.
- WP_AI_Client_HTTP_Client::sendRequestWithOptions()Sends a PSR-7 request with transport options and returns a PSR-7 response.
- WP_Ability::check_permissions()Checks whether the ability has the necessary permissions.
- WP_Ability::execute()Executes the ability after input validation and running a permission check.
- WP_Ability::validate_input()Validates input data against the input schema.
- WP_Ability::validate_output()Validates output data against the output schema.
- WP_Ajax_Response::add()Appends data to an XML response based on given arguments.
- WP_Ajax_Upgrader_Skin::error()Stores an error message about the upgrade.
- WP_Ajax_Upgrader_Skin::feedback()Stores a message about the upgrade.
- WP_Automatic_Updater::after_core_update()Checks whether to send an email and avoid processing future updates after attempting a core update.
- WP_Automatic_Updater::has_fatal_error()Performs a loopback request to check for potential fatal errors.
- WP_Automatic_Updater::send_debug_email()Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
- WP_Automatic_Updater::send_email()Sends an email upon the completion or failure of a background core update.
- WP_Automatic_Updater::update()Updates an item, if appropriate.
- WP_Block_Type::prepare_attributes_for_render()Validates attributes against the current block schema, populating defaulted and missing values.
- WP_Community_Events::get_events()Gets data about events near a particular location.
- WP_Customize_Custom_CSS_Setting::update()Store the CSS setting value in the custom_css custom post type for the stylesheet.
- WP_Customize_Manager::_publish_changeset_values()Publishes the values of a changeset.
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.
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.