Show Outgoing HTTP Requests in the Admin Bar
Counts the external HTTP requests WordPress, plugins and themes make while building a page, and lists each URL, status and time in an admin bar menu for administrators.
Opens WPIDE on
Code
$wpide_http_log = array();
add_filter( 'pre_http_request', function ( $pre, $args, $url ) {
$GLOBALS['wpide_http_started'][ md5( $url ) ] = microtime( true );
return $pre;
}, 1, 3 );
add_action( 'http_api_debug', function ( $response, $context, $class, $args, $url ) use ( &$wpide_http_log ) {
$started = $GLOBALS['wpide_http_started'][ md5( $url ) ] ?? microtime( true );
$wpide_http_log[] = array(
'url' => $url,
'method' => $args['method'] ?? 'GET',
'status' => is_wp_error( $response ) ? $response->get_error_code() : wp_remote_retrieve_response_code( $response ),
'time' => round( ( microtime( true ) - $started ) * 1000 ),
);
}, 10, 5 );
add_action( 'admin_bar_menu', function ( $admin_bar ) use ( &$wpide_http_log ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$admin_bar->add_node( array(
'id' => 'wpide-http',
'title' => 'HTTP: ' . count( $wpide_http_log ),
) );
foreach ( $wpide_http_log as $i => $request ) {
$admin_bar->add_node( array(
'id' => 'wpide-http-' . $i,
'parent' => 'wpide-http',
'title' => esc_html( sprintf( '%s %s - %s (%d ms)', $request['method'], wp_parse_url( $request['url'], PHP_URL_HOST ) . wp_parse_url( $request['url'], PHP_URL_PATH ), $request['status'], $request['time'] ) ),
) );
}
}, 999 );
Notes
Slow pages often come from plugins calling remote APIs on every load; this shows them at a glance.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.