Debugging Scripts in the Console

The easiest way to see what data is being used inside a script. Add this script to your page (or in functions.php for sitewide use).

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 call it inside your function like this:

debug_to_console($mydata);

Then just look at your console log to see the output.

Hint: if you are tracking one variable through a script and want to see how it changes, try something like this:

debug_to_console('step 1 -' . $mydata);

 

Comments are closed.

Skip to toolbar