WP_Error

WordPress Error class.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-error.php  
  1. class WP_Error { 
  2. /** 
  3. * Stores the list of errors. 
  4. * @since 2.1.0 
  5. * @var array 
  6. */ 
  7. public $errors = array(); 
  8.  
  9. /** 
  10. * Stores the list of data for error codes. 
  11. * @since 2.1.0 
  12. * @var array 
  13. */ 
  14. public $error_data = array(); 
  15.  
  16. /** 
  17. * Initialize the error. 
  18. * If `$code` is empty, the other parameters will be ignored. 
  19. * When `$code` is not empty, `$message` will be used even if 
  20. * it is empty. The `$data` parameter will be used only if it 
  21. * is not empty. 
  22. * Though the class is constructed with a single error code and 
  23. * message, multiple codes can be added using the `add()` method. 
  24. * @since 2.1.0 
  25. * @param string|int $code Error code 
  26. * @param string $message Error message 
  27. * @param mixed $data Optional. Error data. 
  28. */ 
  29. public function __construct( $code = '', $message = '', $data = '' ) { 
  30. if ( empty($code) ) 
  31. return; 
  32.  
  33. $this->errors[$code][] = $message; 
  34.  
  35. if ( ! empty($data) ) 
  36. $this->error_data[$code] = $data; 
  37.  
  38. /** 
  39. * Retrieve all error codes. 
  40. * @since 2.1.0 
  41. * @access public 
  42. * @return array List of error codes, if available. 
  43. */ 
  44. public function get_error_codes() { 
  45. if ( empty($this->errors) ) 
  46. return array(); 
  47.  
  48. return array_keys($this->errors); 
  49.  
  50. /** 
  51. * Retrieve first error code available. 
  52. * @since 2.1.0 
  53. * @access public 
  54. * @return string|int Empty string, if no error codes. 
  55. */ 
  56. public function get_error_code() { 
  57. $codes = $this->get_error_codes(); 
  58.  
  59. if ( empty($codes) ) 
  60. return ''; 
  61.  
  62. return $codes[0]; 
  63.  
  64. /** 
  65. * Retrieve all error messages or error messages matching code. 
  66. * @since 2.1.0 
  67. * @param string|int $code Optional. Retrieve messages matching code, if exists. 
  68. * @return array Error strings on success, or empty array on failure (if using code parameter). 
  69. */ 
  70. public function get_error_messages($code = '') { 
  71. // Return all messages if no code specified. 
  72. if ( empty($code) ) { 
  73. $all_messages = array(); 
  74. foreach ( (array) $this->errors as $code => $messages ) 
  75. $all_messages = array_merge($all_messages, $messages); 
  76.  
  77. return $all_messages; 
  78.  
  79. if ( isset($this->errors[$code]) ) 
  80. return $this->errors[$code]; 
  81. else 
  82. return array(); 
  83.  
  84. /** 
  85. * Get single error message. 
  86. * This will get the first message available for the code. If no code is 
  87. * given then the first code available will be used. 
  88. * @since 2.1.0 
  89. * @param string|int $code Optional. Error code to retrieve message. 
  90. * @return string 
  91. */ 
  92. public function get_error_message($code = '') { 
  93. if ( empty($code) ) 
  94. $code = $this->get_error_code(); 
  95. $messages = $this->get_error_messages($code); 
  96. if ( empty($messages) ) 
  97. return ''; 
  98. return $messages[0]; 
  99.  
  100. /** 
  101. * Retrieve error data for error code. 
  102. * @since 2.1.0 
  103. * @param string|int $code Optional. Error code. 
  104. * @return mixed Error data, if it exists. 
  105. */ 
  106. public function get_error_data($code = '') { 
  107. if ( empty($code) ) 
  108. $code = $this->get_error_code(); 
  109.  
  110. if ( isset($this->error_data[$code]) ) 
  111. return $this->error_data[$code]; 
  112.  
  113. /** 
  114. * Add an error or append additional message to an existing error. 
  115. * @since 2.1.0 
  116. * @access public 
  117. * @param string|int $code Error code. 
  118. * @param string $message Error message. 
  119. * @param mixed $data Optional. Error data. 
  120. */ 
  121. public function add($code, $message, $data = '') { 
  122. $this->errors[$code][] = $message; 
  123. if ( ! empty($data) ) 
  124. $this->error_data[$code] = $data; 
  125.  
  126. /** 
  127. * Add data for error code. 
  128. * The error code can only contain one error data. 
  129. * @since 2.1.0 
  130. * @param mixed $data Error data. 
  131. * @param string|int $code Error code. 
  132. */ 
  133. public function add_data($data, $code = '') { 
  134. if ( empty($code) ) 
  135. $code = $this->get_error_code(); 
  136.  
  137. $this->error_data[$code] = $data; 
  138.  
  139. /** 
  140. * Removes the specified error. 
  141. * This function removes all error messages associated with the specified 
  142. * error code, along with any error data for that code. 
  143. * @since 4.1.0 
  144. * @param string|int $code Error code. 
  145. */ 
  146. public function remove( $code ) { 
  147. unset( $this->errors[ $code ] ); 
  148. unset( $this->error_data[ $code ] );