Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created August 17, 2023 00:00
Show Gist options
  • Save sybrew/f72f58394e62447d9bad61fdcc0bcb1b to your computer and use it in GitHub Desktop.
Save sybrew/f72f58394e62447d9bad61fdcc0bcb1b to your computer and use it in GitHub Desktop.
Dumps HTML timer in meta id=htmltimerdump as late as possible for you to snatch via JS.
<?php
/**
* Plugin Name: HTML Timer Dump
* Description: Dumps HTML timer in meta id=htmltimerdump as late as possible for you to snatch via JS.
* Author: Sybre Waaijer
* Author URI: https://cyberwire.nl/
* Version: 1.0.0
* License: GLPv3
*
* @package HTMLTimerDump
*/
// See accompanying JS script: https://gist.github.com/sybrew/a256408606bf2a614b0a7176ba2b8b3c
add_action(
'shutdown',
function() {
// Gotta use microtime here -- hrtime uses another clock.
// Fetch phpruntime before further calculation whether we should actually output it.
$phpruntime = microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'];
// Prevent breaking non-HTML pages.
if ( ! did_action( 'wp_head' ) && ! did_action( 'admin_head' ) ) return;
// 6 significant numbers, force decimal dot, force no thousand-separators.
$phpruntime = number_format( $phpruntime, 6, '.', '' );
// phpcs:ignore, WordPress.Security.EscapeOutput -- $phpruntime doesn't contain escape-worthy characters.
echo <<<HTML
<meta id=htmltimerdump phpruntime="{$phpruntime}" />
HTML;
},
PHP_INT_MAX - 1
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment