How to Debug in WordPress

It’s pretty easy to >debug in wordpress. Just open your wp-config.php file and add the following lines of code:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );

Just before where it says
/* That's all, stop editing! Happy blogging. */

Now, go to the wp-content folder and see that the error logs are written in a file named debug.log

Extra tips:

If you wish to print certain php variable / array / object values to the debug log then you can use the following code to do so:

function print_to_log( $message )
if ( true === WP_DEBUG ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );

Just place the above function into functions.php and call the function somewhere in your plugin, templates or functions.php itself. Example:

$sample_variable = "I want to print this to the debug log";
print_to_log( $sample_variable );

This is really useful when we don’t get expected value from our custom codes or we need to check if a function or a code block works by calling the above function inside that code block or function.

Leave a Reply

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