WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running some of the WP functions directly inside a plugin, including wp_insert_post(), if something goes wrong, this returns a WP Error object, what is the correct method to catch this error? Either using built in WP functions or PHP exceptions or whatnot..

share|improve this question
Just to add to and clarify what is said in the answers here, WP_Error is not a PHP Exception object. You don't use try/catch methods with it. But as noted, there are convenience functions to make it easy to use. – Dougal Campbell Mar 4 '11 at 20:17
up vote 13 down vote accepted
  1. Assign return of the function to the variable.

  2. Check the variable with is_wp_error().

  3. If true handle accordingly, for example trigger_error() with message from WP_Error->get_error_message() method.

  4. If false - proceed as usual.

Example from Codex:

function doer_of_stuff() {
  return  new WP_Error('broke', __("I've fallen and can't get up"));

$return = doer_of_stuff();
if ( is_wp_error($return) )
   echo $return->get_error_message();
share|improve this answer


first, you check weather your result is a WP_Error object or not:

$id = wp_insert_post(...);
if (is_wp_error($id)) {
    $errors = $id->get_error_messages();
    foreach ($errors as $error) {
        echo $error; //this is just an example and generally not a good idea, you should implement means of processing the errors further down the track and using WP's error/message hooks to display them

This is the usual way.

But the WP_Error object can be instanciated without any error occuring, just to act as a general error store just in case. If you want to do so, you can check if there are any errors by using get_error_code():

function my_func() {
    $errors = new WP_Error();
    ... //we do some stuff
    if (....) $errors->add('1', 'My custom error'); //under some condition we store an error
    .... //we do some more stuff
    if (...) $errors->add('5', 'My other custom error'); //under some condition we store another error
    .... //and we do more stuff
    if ($errors->get_error_code()) return $errors; //the following code is vital, so before continuing we need to check if there's been errors...if so, return the error object
    .... // do vital stuff
    return $my_func_result; // return the real result

If you do that, you can then check for an process the returned error just as in the wp_insert_post() example above.

The Class is documented on the Codex.
And there's also a little article here.

share|improve this answer
Thanks! Yours first snippet did the work for wp_insert_user. – Mohammad Mursaleen Dec 17 '14 at 10:37
$wp_error = wp_insert_post( $new_post, true); 
                              echo '<pre>';
                              print_r ($wp_error);
                              echo '</pre>';

This will show you exactly what's wrong with wordpress post insert function. just try it !

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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