You can get the response code with wp_remote_retrieve_response_code()
and log the response with error_log()
.
function my_better_uptime_heartbeat() {
$worked = false;
$response = wp_remote_get( 'https://betteruptime.com/api/v1/heartbeat/<heartbeat_ID>' );
if ( ! is_wp_error( $response ) ) {
if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
// It worked, no need to log anything.
$worked = true;
}
}
if ( ! $worked ) {
// Logs the response that was received for debugging.
error_log( print_r( $response, true ) );
}
}
add_action( 'my_heartbeat', 'my_better_uptime_heartbeat' );
This should log failures to WordPress’ debug log, assuming that you’ve got something like the following in your site’s wp-config.php
:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );