In wordpress, many functions return a WP_Error object when fail to process. In this tutorial, we will introduce how to check and process this object.
For example, in wordpress wp_insert_post() can insert a post into database. We should check the process of inserting post is successful or not.
wp_insert_post( array $postarr, bool $wp_error = false )
When $wp_error = true, if we fail to insert a wordpress post, this function will return a wp_error object.
To check the return value is wp_error object, we can use is_wp_error() function.
is_wp_error( mixed $thing )
This function can check whether variable is a WordPress Error.
if ( is_wp_error( $result ) ) { $error_string = $result->get_error_message(); echo '<div id="message" class="error"><p>' . $error_string . '</p></div>'; }
Then we can implement different processes when the return value is wp_error object.