0

I am trying to utilize WP_Error for my plugin, but nothing happens. Is this too late for WP_Error? Too early? Or am I doing something else wrong?

function cpt_pre_post_publish(){
    return new WP_Error('error', __('Error!' ));
}

add_action('pre_get_posts', 'cpt_pre_post_publish');
  • 1
    All you're doing is returning the WP_Error. Do you try to display it somewhere else? – Pat J Jul 5 '13 at 3:57
1

Actions don't typically return data, so I doubt you will get this working the way you are trying to. Something like...

function cpt_pre_post_publish(){
  global $my_error;
  $my_error = new WP_Error('error', __('Error!' ));
}
add_action('pre_get_posts', 'cpt_pre_post_publish');

... should set a variable that you could access in a template file with...

global $my_error;
var_dump($my_error);

It is really not clear exactly what you are trying to do though.

  • I thought if I returned a WP_Error object in a hook then Wordpress would display it itself, but that is clearly not the case. – jasonaburton Jul 6 '13 at 1:19
0

Please see the example on the Codex, which indicates that you can return the WP_Error from a function, later use the function is_wp_error() (essentially the same as checking instanceof WP_Error), and display the error message.

s_ha_dum is right that actions shouldn't really return -- there's nothing to catch the information being returned.

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.