WordPress is a significant product and debugging code can be difficult with just ‘echo’ calls. For this reason I frequently enable WordPress debug mode to see PHP warnings and notices and to log these messages to a file.
All my sites and client sites are on shared hosting so I cannot connect a debugger to them while I develop the sites. I have considered developing locally but I would have to push changes to external staging sites for clients to review. The overhead of that has discouraged me from going this route.
Enable Debug Mode
The first step is to enable debug mode. I just enable it for my IP address so that I don’t disrupt the browsing of others.
To demonstrate that it is working you can enable a tiny plugin – don’t forget to activate it.
Then browse the site and visit the sample post and page. You will see something like the following in wp-content/debug.log:
With debug mode enabled the wp-content/debug.log is now an insecure look inside your site. Warning messages may give a hacker insight for exploitable vulnerabilities. So, we must prevent that.
Create wp-content/.htaccess to limit access to wp-content/debug.log to just you.
While working on my Override Woocommerce template files in a plugin post I had to investigate the information provided to the wc_get_template filter in the wc_get_template() function. I needed to determine whether the filter provided a full or relative path in the $located parameter and what was in the $template_name parameter.
I wrote a small plugin to write this information to debug.log:
There was a lot of data from the plugin, primarily from the $args parameter. Here is a short example of the information related to the “notices/notice.php” template.
I could quickly see that $located is the full path and $template_name is a short version.
Conditional Debug Messages
If you wish you can add error_log() calls throughout your code and set them to only run when WordPress is in debug mode.