is_wp_error is missing error

I am getting the following error in the code snippet below---and pointing to the line that references the $result[ 'body' ] assignment. The is_wp_error should have caught the error. ??????????????

Fatal error: Cannot use object of type WP_Error as array

    $result = wp_remote_post( ..... ) );
if( is_wp_error( $result ) ):
    $display = 'Error Message';
else:
    $display = $result[ 'body' ];
endif;

Topic errors Wordpress

Category Web


Both wp_remote_post and wp_remote_get return WP_Error object if there is an error. You could use the get_error_message function of WP_Error class to receive the error and show it.

$request = wp_remote_post( $url );
if ( is_wp_error( $request ) ) {
    // If the request has failed, show the error message
    echo $request->get_error_message();
} else {
    $content = wp_remote_retrieve_body( $request );
    // Do stuff with $content
}

For more details go ahead Here

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.