Class: WP_Error

WordPress Error class.

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.

Properties


Properties

Name Type(s) Default Value Summary
errors array Stores the list of errors.
error_data array Stores the list of data for error codes.

Methods

WP_Error:: __construct( string | integer $code = '', string $message = '', mixed $data = '' )

Initialize the error. Since 2.1.0.

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.

Arguments

Name Type(s) Default Value Description
$code string | integer

Error code

$message string

Error message

$data mixed

Optional. Error data.


WP_Error:: add( string | integer $code, string $message, mixed $data = '' )

Add an error or append additional message to an existing error. Since 2.1.0.

Arguments

Name Type(s) Default Value Description
$code string | integer

Error code.

$message string

Error message.

$data mixed

Optional. Error data.


WP_Error:: add_data( mixed $data, string | integer $code = '' )

Add data for error code. Since 2.1.0.

The error code can only contain one error data.

Arguments

Name Type(s) Default Value Description
$data mixed

Error data.

$code string | integer

Error code.


WP_Error:: get_error_code( )

Retrieve first error code available. Since 2.1.0.

Returns

string | integer

Empty string, if no error codes.


WP_Error:: get_error_codes( )

Retrieve all error codes. Since 2.1.0.

Returns

array

List of error codes, if available.


WP_Error:: get_error_data( string | integer $code = '' )

Retrieve error data for error code. Since 2.1.0.

Arguments

Name Type(s) Default Value Description
$code string | integer

Optional. Error code.

Returns

mixed

Error data, if it exists.


WP_Error:: get_error_message( string | integer $code = '' )

Get single error message. Since 2.1.0.

This will get the first message available for the code. If no code is given then the first code available will be used.

Arguments

Name Type(s) Default Value Description
$code string | integer

Optional. Error code to retrieve message.

Returns

string


WP_Error:: get_error_messages( string | integer $code = '' )

Retrieve all error messages or error messages matching code. Since 2.1.0.

Arguments

Name Type(s) Default Value Description
$code string | integer

Optional. Retrieve messages matching code, if exists.

Returns

array

Error strings on success, or empty array on failure (if using code parameter).


WP_Error:: remove( string | integer $code )

Removes the specified error. Since 4.1.0.

This function removes all error messages associated with the specified error code, along with any error data for that code.

Arguments

Name Type(s) Default Value Description
$code string | integer

Error code.


WordPress Developer Newsletter

Stay on top of the latest WordPress API changes, developer tool updates, security alerts and more.