WP_Error
- Since
- 2.1.0
- Source
wp-includes/class-wp-error.php:18
WordPress Error class.
Description
Container for checking for WordPress errors and error messages. Return WP_Error and use is_wp_error() to check if this class is returned. Many core WordPress functions pass this class in the event of an error and if not handled properly will result in code errors.
Compatibility
- WordPress
- since 2.1.0
- 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).
Hooks and filters fired · 1
Every hook that fires from inside WP_Error, in the order it appears in the class, grouped by the method that fires it.
WP_Error::add()
- do_action( wp_error_added )action line 209
Properties · 3
$errorsarraypublic- Stores the list of errors.
$error_dataarraypublic- Stores the most recently added data for each error code.
$additional_dataarray[]protected- Stores previously added data added for error codes, oldest-to-newest by code.
Methods · 14
- __construct()Initializes the error.
- get_error_codes()Retrieves all error codes.
- get_error_code()Retrieves the first error code available.
- get_error_messages()Retrieves all error messages, or the error messages for the given error code.
- get_error_message()Gets a single error message.
- get_error_data()Retrieves the most recently added error data for an error code.
- has_errors()Verifies if the instance contains errors.
- add()Adds an error or appends an additional message to an existing error.
- add_data()Adds data to an error with the given code.
- get_all_error_data()Retrieves all error data for an error code in the order in which the data was added.
- remove()Removes the specified error.
- merge_from()Merges the errors in the given error object into this one.
- export_to()Exports the errors in this object into the given one.
- copy_errors()Copies errors from one WP_Error instance to another.
Source code
#[AllowDynamicProperties]class WP_Error { /** * Stores the list of errors. * * @since 2.1.0 * @var array */ public $errors = array(); /** * Stores the most recently added data for each error code. * * @since 2.1.0 * @var array */ public $error_data = array(); /** * Stores previously added data added for error codes, oldest-to-newest by code. * * @since 5.6.0 * @var array[] */ protected $additional_data = array(); /** * Initializes the error. * * If `$code` is empty, the other parameters will be ignored. * When `$code` is not empty, `$message` will be used even if * it is empty. The `$data` parameter will be used only if it * is not empty. * * Though the class is constructed with a single error code and * message, multiple codes can be added using the `add()` method. * * @since 2.1.0 * * @param string|int $code Error code. * @param string $message Error message. * @param mixed $data Optional. Error data. Default empty string. */ public function __construct( $code = '', $message = '', $data = '' ) { if ( empty( $code ) ) { return; } $this->add( $code, $message, $data ); } /** * Retrieves all error codes. * * @since 2.1.0 * * @return array List of error codes, if available. */ public function get_error_codes() { if ( ! $this->has_errors() ) { return array(); } return array_keys( $this->errors ); } /** * Retrieves the first error code available. * * @since 2.1.0 * * @return string|int Empty string, if no error codes. */ public function get_error_code() { $codes = $this->get_error_codes(); if ( empty( $codes ) ) { return ''; }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 6.8.8 tag, from
src/wp-includes/class-wp-error.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.