I am looking for a way to handle errors for a custom post type with its own custom fields. This is all part of a plugin I'm writing, however, after extensive research the only way I found is through sessions (or similar) and show it on the next page, (in which case it would save the changes).

Is there any other way of handling errors and preventing WordPress from saving the post type if something is wrong? If so how? I figured WP_Error class would work, but without luck.

If anyone can help, that would be great!


  • What kind of errors do want to catch? Some concrete examples?
    – fuxia
    Feb 6, 2012 at 12:30
  • For example, one of the custom fields allows to add a link (separate from the content body), however, the link is required, and if it hasn't been filled out, it shouldn't save, instead, it shows an error saying it's required. If I can achieve this, I should be able to do the rest too.
    – Hosh Sadiq
    Feb 6, 2012 at 13:53

1 Answer 1


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

All methods

//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
$data = $error->get_error_codes();
print_r( $data );
//Output: Array ( [0] => not_found [1] => not_match )

//Return first error code
echo $error->get_error_code();
//Output: not_found

//Return all error message
$data = $error->get_error_messages();
print_r( $data );
//Output: Array ( [0] => Page Not Found [1] => Field Not Match )

//Return error message by error code
$data = $error->get_error_messages( 'not_match' );
print_r( $data );
//Output: Array ( [0] => Field Not Match )

//Return first error message if no code are given
echo $error->get_error_message();
//Output: Page Not Found

//Return first error message for given error code
echo $error->get_error_message( 'not_match' );
//Output: Field Not Match

//Return first error data
echo $error->get_error_data();
//Output: Page Data

//Return error data from error code.
echo $error->get_error_data( 'not_found' );
//Output: Page Data

//add error data to error code
//syntex: add_data( $data, $code );
$error->add_data( 'Some data', 'not_match' );
echo $error->get_error_data( 'not_match' );
//Output: Some data

//Check whether variable is a WordPress Error.
//return bool True, if WP_Error. False, if not WP_Error.
$data = is_wp_error( $error );
var_dump( $data );
//Output: bool(true)

Source and hint: more background on this post

  • Would this prevent a post being saved? Feb 6, 2012 at 13:27
  • no, its only messages on top of Admin. You can add or change the default messages. But if you prevent any doings, you must check for this, the best way it I think about an capability object; which you can add to different or each role.
    – bueltge
    Feb 6, 2012 at 13:32
  • I really need it to prevent to save anything rather than just change the default message. Not quite sure what you mean with the capabilities bit?
    – Hosh Sadiq
    Feb 6, 2012 at 13:55
  • capabilities are objects; like 'manage_options'. You can create custom objects and ask for this; see this codex page.
    – bueltge
    Feb 6, 2012 at 15:19
  • I already have custom capabilities added to administrator (and to a custom roles). But how would I prevent the saving if something is wrong?
    – Hosh Sadiq
    Feb 6, 2012 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.