WP_Error not displaying errors

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 … Read more

How to customize the critical error message?

You can use the wp_php_error_message filter to customise this message: add_filter( ‘wp_php_error_message’, function( $message, $error ) { return ‘My custom message.’; }, 10, 2 ); I haven’t tested to be sure, but I would assume that if the critical error that causes this message happens to be in the code of same theme or plugin … Read more

Unable to show error message using wp_login action

Main problem with your code is that you use wp_login action. The wp_login action hook is triggered when a user logs in by the wp_signon() function. It is the very last action taken in the function, immediately following the wp_set_auth_cookie() call. So first of all – the user is already authenticated and his auth cookie … Read more

Cannot use object of type WP_Error as array

Well, it’s pretty clear, why this problem occurs. Let’s look at wp_insert_term documentation: Return Values (array|WP_Error) The Term ID and Term Taxonomy ID. (Example: array(‘term_id’=>12,’term_taxonomy_id’=>34)) As you can see, on success this function returns array. But… If any error occurs, it will return an object of WP_Error type. So… This Fatal Error occurs, because wp_insert_term … Read more

How to group multiple wp_errors together?

You can retrieve all of the message of the same code with the get_error_messages() method (hacked from some code in the Codex): function doer_of_stuff() { $err = new WP_Error( ‘broke’, “I’ve fallen and can’t get up 1”); $err->add(‘broke’, “I’ve fallen and can’t get up 2”); $err->add(‘borken’, “not this one”); $err->add(‘borken’, “not this one”); $err->add(‘broke’, “I’ve … Read more

Why when I instantiate wp_error in a validation method my user registration method stops working?

It seems you need to use $errors = new \WP_Error(); in your code, since WP_Error isn’t part of your namespace. I ran a quick test on my machine, using wp-cli commands: Using new WP_Error(): % wp eval ‘namespace PJ\NS\Test; $x = new WP_Error(); var_dump( $x );’ Fatal error: Uncaught Error: Class ‘PJ\NS\Test\WP_Error’ not found in … Read more

Object of class WP_Error could not be converted to string

get_term can return a WP_Error object in addition to a falsy value for term not found or an actual term row. You fix this, by adding an additional check: if (!$term) { continue; } Becomes: if (!$term || is_wp_error($term)) { continue; } You should also do this above the get_term_link call. $term = get_term($value, $fieldSettings[‘taxonomy’]); … Read more