WordPress Error handling with WP_Error class

WordPress has WP_Error class for checking WordPress errors and error messages since version 2.1.0. WordPress uses object of WP_Error class for reporting errors from several WP functions. However, we can use this object in plugin or theme to handle error within WordPress. This class containing various useful method for managing error.

wp

Constructor of WP_Error allows three parameters: $code, $message and $data
Syntex is:

$error = new WP_Error( $code, $message, $data );

here, $code containing error code. it may be string or integer, $message containing error message and $data containing error data. however all parameter are optional.

Suppose, we want to report new error, so we can use:

$error = new WP_Error( 'not_found', 'Page Not Found', 'Page Data' );

Here, we consider not_found as error code, ‘Page Not Found’ as error message and ‘Page Data’ as error data.

After creating error object we can check error by is_wp_error function.