Class Reference/WP Error

来自WordPress中文文档
跳转至: 导航搜索

Description

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

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

Source

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 http://core.trac.wordpress.org/browser/.

Parameters

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

Usage

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

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

/div

wordpress.org.cn

Change Log

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

模板:Class Footer