Display custom debug info inside page and not before head

Don’t print any debug info into the page directly. WordPress offers a wp-content/debug.log file which you can use.

Enable debug and debug_log in your wp-config.php

define( 'WP_DEBUG', true ); // turn debug mode on
define( 'WP_DEBUG_DISPLAY', false ); // do not display errors
define( 'WP_DEBUG_LOG', true ); // save errors to wp-content/debug.log
define( 'SAVEQUERIES', true ); // save queries for debug

Add this handy function to your functions.php or plugin file

/**
 * Simple debug trace to wp-content/debug.log
 * @usage _log( $var );
 */
if ( ! function_exists( '_log' ) ) {
    function _log( $log ) {

        if ( true !== WP_DEBUG ) {
            return;
        }

        if ( is_array( $log ) || is_object( $log ) ) {
            error_log( print_r( $log, true ) );
        } else {
            error_log( $log );
        }

    }
}

Use it in your code whenever you need to print some debug info

_log( $my_var );

Open wp-content/debug.log and see you data printed there. I usually keep it as a pinned file in my IDE (PHPStorm).