WordPress – How to debug PHP to console

Here’s a neat trick that allows you to output your theme, plugins or any part of a PHP script to the browsers console.

This allows you to use Firebug, or your favourite browser debug tool, to read the console and debug your PHP without affecting the design of the website.

This does not require the installation of any third-party plugins, just a PHP script – so you know exactly what’s going on.

Simply add the following to your theme’s functions.php

function debug_to_console( $data ) {
if ( is_array( $data ) )
 $output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
 else
 $output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
echo $output;
}

Then use the following line to output to the console

debug_to_console( "Test" );

Where ‘Test’ can be any variable you need to output.

WordPress-DebugPHPtoConsole1

Reference: http://stackoverflow.com/questions/4323411/how-can-i-write-to-console-in-php