Log PHP Data In WordPress To Your Browser’s Console

pile of tree logs

Debugging PHP can be a pain in WordPress. Add this handy code snippet to your theme’s functions.php file. The function will log PHP data to your browser’s console.

/**
 * @param ...$data
 *
 * @return void
 */
function my_console_log(...$data) {
    $json = json_encode($data);
    add_action('shutdown', function() use ($json) {
       echo "<script>console.log({$json})</script>";
    });
}

// Example usage
my_console_log(['foo' => 'bar'], null, 1, 'hi');

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.