$errors = new WP_Error( 'not_found', 'The post you are looking for does not exist.', array( 'status' => 404 ) ); $codes = $errors->get_error_codes(); foreach ( $codes as $code ) { echo $code . "
"; }
function my_custom_function(){ //some code if($error){ $errors = new WP_Error(); $errors->add( 'my_custom_error', 'There was an error in processing', array( 'status' => 400 ) ); return $errors; }else{ //continue code } } $results = my_custom_function(); if( is_wp_error( $results ) ) { $codes = $results->get_error_codes(); foreach ( $codes as $code ) { //handle error } }In this example, a custom function 'my_custom_function' is defined which creates a new WP_Error object and assigns it an error code 'my_custom_error' with a message and an array of additional data. Then, using the get_error_codes function, the list of error codes is retrieved and further error handling is performed. This code belongs to the WordPress Core package library.