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');

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Get Involved & Explore More

an abstract painting with blue and yellow colors

Catch up on what I’ve been writing lately.

Show your gratitude.

Join Dare To Code Email List

Get emails from me on full-stack PHP development by subscribing to the Dare To Code mailing list.