Return WP_Error as WP_REST_Response
Already found, if I parse the response response.json() I get the messages in both cases
Already found, if I parse the response response.json() I get the messages in both cases
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
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
The function is_wp_error checks if the given var is an instance of WP_Error class (source code as WP 4.2.2): function is_wp_error( $thing ) { return ( $thing instanceof WP_Error ); } As you can see, if the given variable is a instance of WP_Error class, the function returns true, even if the object is empty. … Read more
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
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
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
You can use the $wp_error argument to return an error message. $post_id = wp_update_post( $my_post, true ); if ( is_wp_error( $post_id ) ) { echo $post_id->get_error_message(); } else { echo ‘true’; }
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
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