WP_Error not displaying errors

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');

Related posts

2 comments

  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.

  2. 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.

Comments are closed.