WordPress Debugging with PHP Console

Those of you who are relatively familiar with Javascript will know just how useful the console.log feature can be. In a nutshell, it allows you to send debug information directly to your browser console (assuming that the browser you’re using supports a debug console).

With PHP, it’s not so easy. Sure you can use breakpoint debug functions such as var_dump(), print_r(), and die(), but each of these will output the debug information directly onto your browser window, which is messy and often impractical.

If you’re using Google Chrome, PHP debugging gets a whole lot easier thanks to an extension called PHP Console. It allows you to display PHP errors and var dumps directly in the Chrome console as well as in notification popups. Even better, plugin author Fulvio Notarstefano has created a WordPress plugin implementation, so everything can be configured easily via a dashboard settings page on your site.

Below I’ll take you through how to setup and use PHP console debugging on your own WordPress sites:

Getting the extension and plugin

Add and activate the PHP Console extension from the Chrome Web Store here.

Download the WP PHP Console plugin here, and activate it on your site.

Configuring the plugin

Once you’ve activated the plugin on your site, you should notice a warning at the top of your WP dashboard prompting you to add a password.

Go to the WP PHP Console settings page, either by clicking the link in the warning or clicking WP PHP Console under Settings, and add a password. This is the only required setting – the rest are optional, but I recommend checking the ‘Register PC Class’ option as well (or else you’ll have to do it manually in your code), so do that too and save your settings.

Now reload any page of your installation and click the key icon next to your Chrome address bar. If you don’t see it, it’s probably hidden in your Chrome menu, so open the menu on the right of your address bar and find the icon there. Enter the password you just set and click ‘Login’.

Trying it out

If you want to see it in action immediately, add the following to your theme’s functions.php file:

add_action( 'init', 'php_console_example' ); // Run the function on init
function php_console_example() {
$message_var = "Here's an example of logging the contents of a variable with PHP Console!"; // A simple string var.
if ( class_exists( 'PC' ) ) { // Prevent your site from breaking if the 'PC' class has not been registered.
PC::debug( $message_var, 'Example' ); // Here's where the magic happens: The contents of $message_var will be logged with the title 'Example'.
}
}

The result:

Popup ExampleConsole Example

This is a very simple example using a basic string variable, but hopefully you get the idea.

To demonstrate a slightly more real-world example, I’ll use some code from our “Beyond admin-ajax, using the WordPress REST API” post. We’re going to log the contents of $request (the WP_REST_Request object) in the callback handler function so that we can easily see exactly what data is being sent whenever the form is submitted:

Tip of the iceberg

In this article I’m really just scratching the surface of what you can do with PHP Console, but I hope you can appreciate how useful adding this to your workflow can be. If you’d like to know more, take a look at the PHP Console presentation video and examples page.

Hey Ahmad, just gave it a test on a FlyWheel Local setup and calling PC::debug from within a REST API callback function was successful. Not sure why it isn’t working, you say it does work for you using it from outside a REST API callback function?