Class Reference/WP Error
About: Role of WP_Error
Introduced with WordPress 2.1, WP_Error is a class that makes error handling within plugins and WordPress itself much easier.
Instances of WP_Error store error codes and messages representing one or more errors, and whether or not a variable is an instance of WP_Error can be determined using the is_wp_error() function.
Code
You may find the class WP_Error in your file /wp-includes/class-wp-error.php at line 21 and function is_wp_error() at line 206.
You can also view this file online at the trac.
Methods and Properties
Properties
- $errors
- Array containing the list of errors.
- $error_data
- Array containing the list of data for error codes.
Methods
- WP_Error($code, $message, $data)
- Constructor. sets up error message. If code parameter is empty then nothing will be done. It is possible to add multiple messages to the same code, but with other methods in the class. All parameters are optional, but if the $code parameter is set, then the $data parameter is optional.
- get_error_codes()
- Retrieve all error codes. Access public, returns array List of error codes, if available.
- get_error_code()
- Retrieve first error code available. Access public, returns string, int or Empty if there is no error codes
- get_error_messages($code)
- Retrieve all error messages or error messages matching code. Access public, returns an array of error strings on success, or empty array on failure (if using code parameter)
- get_error_message($code)
- Get single error message. This will get the first message available for the code. If no code is given then the first code available will be used. Returns an error string.
- get_error_data($code)
- Retrieve error data for error code. Returns mixed or null, if no errors.
- add($code, $message, $data)
- Append more error messages to list of error messages. No return.
- add_data($data, $code)
- Add data for error code. The error code can only contain one error data. No return.
Example
function doer_of_stuff() {
return new WP_Error('broke', __("I've fallen and can't get up"));
}
$return = doer_of_stuff();
if ( is_wp_error($return) )
echo $return->get_error_message();
Related
- see is_wp_error() for more information on trapping for errors (particularly useful when faced with the dreaded 'Catchable fatal error: Object of class WP_Error could not be converted to string')