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. Your $error
variable is a WP_Error
instance, empty but a WP_Error
instance, so it returns true. You could do something:
if ( is_wp_error( $error ) && ! empty( $error->errors ) )
Or initiate WP_Error
only on certains conditions (write here, not tested):
$errors = array();
if ( empty( $comment_content ) ) {
$errors['comment_empty'] = esc_html__("Comment field can't be empty.");
}
if ( strlen( $comment_content ) < 30 ) {
$errors['comment_short'] = esc_html__("Your comment is too short. Write down at least 30 characters.");
}
if( ! empty( $errors ) ) {
$wp_error = new WP_Error();
foreach ( $errors as $code => $message ) {
$wp_error->add( $code, $message );
}
}
if ( is_wp_error( $wp_error ) ) {
}