Debugging in WordPress – Writing Custom PHP Data or Log Messages

WordPress has a configurable debugging system that allows us to display as well as log PHP errors, notices, and warnings. Besides that, sometimes we need to debug the code by writing custom PHP data or messages in a log file.

This is particularly useful during in custom code, theme as well plugin development in WordPress. If something isn’t working as expected then we log inputs, intermediate values, messages, variables etc. and debug further.

Debugging in WordPress

Here, I’m explaining you a bit about WordPress debugging. Further, I’ve written a function to log custom messages or PHP data in the WordPress log file. Also, we aren’t displaying them to visitors or the browser. We are only writing them to a file for inspection.

Debugging in WordPress

WordPress offers five constants for debugging. Out of them, we’re using three PHP constants here. They are WP_DEBUG, WP_DEBUG_LOG, and WP_DEBUG_DISPLAY. Further, all of these three constants are placed in the wp-config.php file. Let us discuss each in context to writing custom data.

Are you using Bootstrap with WordPress? Don’t miss awesome pagination, page break, multi-column menu, and a better search box with dropdown as well.

WP_DEBUG

WP_DEBUG is a PHP constant that can enable the “debug” mode throughout WordPress. It’s the first constant you need to set true in the wp-config.php file. If the constant is already there then change “false” to “true” (without parenthesis).

WP_DEBUG_LOG

This is a companion to WP_DEBUG. Further, it causes all errors or your messages to be written to a debug.log log file. WordPress creates this file inside the wp-content directory and write logs there. We will log our custom data along with PHP errors, notices, and warnings in the same file. So you need to set WP_DEBUG_LOG to true in the wp-config.php as well. 

WP_DEBUG_DISPLAY

Setting this constant to true will cause WordPress to display the error on the page in the browser. Further, true is the default value for this constant. That is alright in the development environment but seriously dangerous in the production. So for a live site, the recommended value of WP_DEBUG_DISPLAY is false.

Do you know what SEO tools and better tips we use to increase the traffic that nobody else will tell?

Setting WordPress Debugging Constants

Putting all them together, add the following lines of code in the wp-config.php file to enable debugging and logging. If these constants are already present then update their values as given.

Note that if WP_DEBUG is false then neither the logging nor the displaying will work despite its constant’s value. Also, it is better to enable writing in the debug.log only for the production environment. Additionally, once you have fixed the code, set WP_DEBUG back to false.

How much do you know WordPress? Check the constantly updated article.

Writing Custom PHP Data to debug.log

You can use PHP’s error_log() function in conjunction with WP_DEBUG constant to write custom data or messages. Add the following line of code in your plugin’s file or theme’s functions.php.

And call the function like this below as much as you need:

Also, you would need some string assignment to log a boolean variable as shown in the example below:

So it’s all about debugging in WordPress. Further, we have written custom messages or PHP data to the debug.log. While it’s quite useful to fix the code, it’s also worth mentioning that debugging isn’t to run on a live site.

However, sometimes the code works fine in a local server but fails on the production server. In such cases, you can debug by writing your custom log messages while hiding the same from visitors. Just don’t forget to disable debugging later. Happy coding and debugging!

You Might Interested In

1 COMMENT

Leave a Reply