output the debug lines to the end of the page
Use the shutdown hook. It runs very late– “just before PHP shuts down execution” per comments in the source. Proof of concept: add_action( ‘shutdown’, function() { echo ‘This runs very late in the page load’; } );