Third-Party Requests Inspector in the Admin Bar
Adds an admin bar item on the front of the site that counts the external domains the current page loads files from, and lists them on click, to help you keep your cookie notice and privacy policy accurate.
Opens WPIDE on
Code
add_action( 'admin_bar_menu', function ( $bar ) {
if ( is_admin() || ! current_user_can( 'manage_options' ) ) {
return;
}
$bar->add_node( array(
'id' => 'wpide-third-parties',
'title' => 'Third parties: ...',
'href' => '#',
) );
}, 95 );
add_action( 'wp_footer', function () {
if ( ! is_admin_bar_showing() || ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<script>
(function () {
var link = document.querySelector('#wp-admin-bar-wpide-third-parties > a');
if (!link || !window.performance || !performance.getEntriesByType) { return; }
function hosts() {
var own = location.hostname, map = {};
performance.getEntriesByType('resource').forEach(function (r) {
var h;
try { h = new URL(r.name).hostname; } catch (e) { return; }
if (h && h !== own) { map[h] = (map[h] || 0) + 1; }
});
return map;
}
function label() { link.textContent = 'Third parties: ' + Object.keys(hosts()).length; }
window.addEventListener('load', function () { setTimeout(label, 1500); });
link.addEventListener('click', function (e) {
e.preventDefault();
var map = hosts();
label();
var lines = Object.keys(map).sort().map(function (h) { return h + ' (' + map[h] + ' requests)'; });
window.alert(lines.length ? 'This page loaded files from:\n\n' + lines.join('\n') : 'This page loaded nothing from other domains.');
});
})();
</script>
<?php
}, 99 );
Notes
Uses the browser's resource timing list, so requests made after the click are not counted until you click again.
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.