Last modified by Safiye A. Duman - 3 years ago
78 Views
Less than a minute read

How to get error message when using wp_remote_post and wp_remote_get

When using WordPress HTTP API, if the connection fails you could use the below code to display the error message.

$response = wp_remote_post($url);
if (is_wp_error($response)) {
$error = $response->get_error_message();
echo $error;
} else {
$content = wp_remote_retrieve_body($response);
}
view raw gistfile1.txt hosted with ❤ by GitHub

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.

Was this information helpful?