Skip to content

Instantly share code, notes, and snippets.

@khaledsaikat
Created November 23, 2015 23:28
Show Gist options
  • Save khaledsaikat/a262d0c8cc4f77c4b43c to your computer and use it in GitHub Desktop.
Save khaledsaikat/a262d0c8cc4f77c4b43c to your computer and use it in GitHub Desktop.
Example of WP_Error class
<?php
/**
* Creating instance of error class
*/
$error = new WP_Error( 'not_found', 'Page Not Found', 'Page Data' );
/**
* Add new error to object
*/
$error->add( 'not_match', 'Field Not Match' );
/**
* Return all error codes from object
*
* Output: Array ( [0] => not_found [1] => not_match )
*/
$data = $error->get_error_codes();
print_r( $data );
/**
* Return first error code
*
* Output: not_found
*/
echo $error->get_error_code();
/**
* Return all error message
*
* Output: Array ( [0] => Page Not Found [1] => Field Not Match )
*/
$data = $error->get_error_messages();
print_r( $data );
/**
* Return error message by error code
*
* Output: Array ( [0] => Field Not Match )
*/
$data = $error->get_error_messages( 'not_match' );
print_r( $data );
/**
* Return first error message if no code are given
*
* Output: Page Not Found
*/
echo $error->get_error_message();
/**
* Return first error message for given error code
*
* Output: Field Not Match
*/
echo $error->get_error_message( 'not_match' );
/**
* Return first error data
*
* Output: Page Data
*/
echo $error->get_error_data();
/**
* Return error data from error code.
*
* Output: Page Data
*/
echo $error->get_error_data( 'not_found' );
/**
* add error data to error code
* syntex: add_data( $data, $code );
*
*
* Output: Some data
*/
$error->add_data( 'Some data', 'not_match' );
echo $error->get_error_data( 'not_match' );
/**
* Check whether variable is a WordPress Error.
*
* return bool True, if WP_Error. False, if not WP_Error.
* Output: bool(true)
*/
$data = is_wp_error( $error );
var_dump( $data );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment