Class Reference/WP Error

跳转至: 导航搜索


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.

Methods and Properties


Array containing the list of errors.
Array containing the list of data for error codes.


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.
Retrieve all error codes. Access public, returns array List of error codes, if available.
Retrieve first error code available. Access public, returns string, int or Empty if there is no error codes
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 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.
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.


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();


WP_Error is located in onlyincludecodewp-includes/class-wp-error.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on


  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.


Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre


Change Log


  • 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')

模板:Class Footer