Debugging in WordPress

“If debugging is the process of removing software bugs, then programming must be the process of putting them in” – Edsger Dijkstra (Dutch computer scientist, winner of the 1972 Turing Award)

One cannot simply start debugging of a program unless he/she knows how the program is built or how it is working. For the programmers who wrote that program, finding bug is much easy as they know where and how of that thing.

So a few steps to make debugging easy:

Comment your program appropriately, it is the single most powerful weapon you have, while debugging a large chunk of code.

Make your code as readable as possible, coz Martin goldman once said “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live”.

Debugging In WordPress

WordPress has many inbuilt features to make debugging easy. Some of these tools are listed below and should be used in development phase.

Debugging With WP_DEBUG

The most important debugging tool you need to know about is WP_DEBUG.

WP_DEBUG is a boolean constant, which triggers the “debug” mode throughout WordPress. It’s found in the wp-config.php file in your WordPress install. When set to “true,” you’ll start seeing PHP notices – not so much errors as they are helpful messages for developers – and WordPress-generated debug messages, particularly deprecated function usage, displayed on your site’s pages.

To turn on WP_DEBUG, simply add the following line of code to your wp-config.php file:

define( 'WP_DEBUG', true );

To turn the constant off, just replace “true” with “false”. WP_DEBUG is set to false by default.

WP_DEBUG provides a handy way to troubleshoot problems when something goes wrong with your site.

It’s important to keep in mind that WP_DEBUGshould not be used on a live site. While it’s a useful feature during development, it can be dangerous on a live site because text in the PHP notices can reveal details about your code, paths and other information to visitors to your site.

Logging Errors With WP_DEBUG_LOG

Another handy tool is WP_DEBUG_LOG, which can be used in conjunction with WP_DEBUG to save all error messages to a debug.log file located in the /wp-content/ directory of your site. This is an especially useful feature if you want to review notices later and see the difference after some changes in code.

To turn on debug logging, simply add the following line of code to your wp-config.php file:

define('WP_DEBUG_LOG', true);

Turn Off Displaying Errors On Your Site With WP_DEBUG_DISPLAY

If you don’t want error messages published to your site’s pages you should use WP_DEBUG_DISPLAY. This is another useful constant, which allows you to control whether debug messages are shown inside the HTML of your site. The default is “true,” which shows errors and warnings as they are generated. Changing this to “false” will hide all errors.

This constant should be used in conjunction with WP_DEBUG_LOG.

To use this feature, just add the following line of code to your wp-config.php file:

define('WP_DEBUG_DISPLAY', false);

So using this tool you have prevented error messages to be displayed on website and still you can see them.

SCRIPT_DEBUG

In the admin, WordPress minimizes and concatenates JavaScript and CSS. But WP also comes with the “development” scripts, in the form of dev.js and dev.css. To use these instead:

define( 'SCRIPT_DEBUG', true );

SAVEQUERIES

The WordPress database class can be told to store query history:

define( 'SAVEQUERIES', true );

When this is defined, $wpdb->queries stores an array of queries that were executed, along with the time it takes to execute them.

Note: Enabling this setting affects your site’s performance. You should only enable this setting for as long as it is necessary to debug a problem, and then disable it.

Following PHP code snippet demonstrates how to dump the entire contents of the $wpdb->queries array to a page:

<?php
global $wpdb;
print_r( $wpdb->queries );
?>

The ‘all’ and ‘shutdown’ hooks

There’s an ‘all’ hook that fires for all actions and filters. Example usage:

Few Other WordPress Plugins that Help in Debugging:

Query Monitor – (https://wordpress.org/plugins/query-monitor/) This plugin will do all the work, required to get the hint of problem in debugging and this has literally saved my day many times.Below you can see the detailed data of Hooks executed on this page

About Priyanka Thakur

A stylist by heart, and a blogger by choice. Though, fashion and food are her long-abiding passions, she spends a considerable amount of time researching and experiencing the latest marketing trends. And when not stalking the world of digital ecstasy, she’s probably found travelling or hunting for an undiscovered boutique.

42Works Reviews

The content development team at 42Works were able to provide me with a keyword rich set of blog posts for a project I had. I requested 3 original blog posts for use on a campaign and got them quickly but without sacrificing quality. Every time I think the 42Works team have exhausted what they can offer me I am always happy to find out that they are a one stop shop and can deliver almost any internet project both quickly and cost effectively.

A. Sherman

Debugging in WordPress

I have had the pleasure of working with 42Works and Anmol on a number of projects over a variety of different platforms and solutions. At every turn, the team has provided outstanding and responsive technical solutions and often a more elegant result than was originally anticipated. I would highly recommend 42Works for your project.

P. Daya

Debugging in WordPress

Extremely, fast and professional service from this team. If I could give 10 stars I would. They solved current issues with ease. They went above and beyond task. Will be hiring them again to redo my site. Thank you.

Jared R.

Debugging in WordPress

The best thing about Anmol & the 42Works team is that they know what I want and need before I even do. I am astonished at how smart, professional and easily available their services are. Had I known them about 5 years ago I’d have saved a lot of wasted efforts on projects. No one is like Anmol. In a world of imitators he is the real deal!

Andrew A.

Debugging in WordPress

The 42Works team was extremely easy to work with and performed amazing work. I was impressed by how responsive they were, and also with how quickly they delivered each iteration of the design. I’ll definitely work with them again and again!

Shez A.

Debugging in WordPress

Said it a dozen times already — A team of truly dedicated, motivated, and great coder. Moreover, their designers have a strong understanding of the importance of quality assurance.

S. Paula

Debugging in WordPress

Working with the 42Works team gave me an experience I was not familiar with. In them, I found a bunch of attentive and responsible consultants. Not only did they knew what was needed to be done, the team made many suggestions that brought about a better project overall. I would say I am lucky to have found 42Works, but the truth is that I am fortunate. This may have been my first project with them but it certainly won’t be my last.