If you stumble upon any error with your WordPress website you will need to know how to debug WordPress and troubleshoot errors and issues. This article aims to show you how to debug errors and issues in WordPress.
This beginner guide covers everything related to debugging in WordPress.
From the activation of the debugging, both ways, manual and via a plugin, it also covers the overview of the Query Monitor plugin as it provides more advanced options to debug the WordPress website.
How to enable debug manually
You need to be able to access your WordPress website template files, via local file editing, FTP, or some control panel File Manager. If you have access, just navigate your editor to the root directory of your WordPress website and find the file wp-config.php and start editing it.
After the file open’s up in the editor, just scroll down below and find line 77 that holds this value below:
define( 'WP_DEBUG', false );
Now, just turn on the debugger by altering the value of false into true. Like this:
define( 'WP_DEBUG', true );
WordPress should print the error messages and notices at the top of the pages if any at all.
Way to record the debug log
Please do note that you can also record the output of the debugger into a flat-file which can be later inspected or send to developers for a review. To record this, just add these lines below.
define( 'WP_DEBUG_LOG', '/tmp/debug.log' );
Turn off errors and notices for users while debugging
This is crucial if you are running a production website.
We can disable the printing of error messages and notices to the users and admin while keeping the debugger active and recording the data to a log file for inspection.
define( 'WP_DEBUG_DISPLAY', false );
So, after you add this line of code at the end, the errors will only get recorded in the debug.log we created first. Then you can use the text editor or file viewer to see the actual errors and notices and resolve the problem quickly.
Debugging WordPress using plugins
If you don’t have the access to your WordPress template files for some reason or you don’t like to alter the core files, you can also activate the debugging via a plugin.
WP Debug is a simple plugin that will offer you the options page where you can turn any of these values on-off dynamically and without the need to alter the wp-config file at all.
Besides these basic features, it also has some additional functionality like debugging Script errors and it can save queries for later review, this is highly useful if you are developing anything.
You can install it easily from the WordPress admin area. Just head to WordPress admin -> Plugins -> Add new. Here just enter the word “Debug” inside the search form at the right side and wait for results. See this image:
Once the plugin is listed below, click install and activate.
Activate debugging using Debug plugin
Now, just head to the plugin’s page and access the settings page. You will see the list of options related to the debugging in the image below. You can turn on/off any of the listed features and save changes.
Once you finish debugging, you can safely uninstall this plugin.
Software for advanced debugging in WordPress
You can also consider using more plugins for advanced debugging in WordPress. For example, install the Query Monitor plugin and it will provide very useful data.
It comes with a bunch of advanced debugging features to help you along, here are some of the features worth mentioning:
- Database queries, including notifications for slow, duplicate, or erroneous queries
- Admin bar warnings on errors
- PHP errors presented nicely along with their responsible component and call stack
- Responsible component and calling function also provides separate aggregate views for each
- The template filename and complete template hierarchy
- Blocks and associated properties in post content for WP 5.0+ or the Gutenberg plugin.
- Matched rewrite rules, associated query strings, and query vars
- Enqueued scripts and stylesheets, along with their dependencies, dependents, and alerts for broken dependencies
- Language settings and loaded translation files (MO files) for each text-domain
- HTTP API requests, with response code, responsible component
- User capability checks, along with the result and any parameters passed to the capability check
- Environment information PHP, the database, WordPress, and the webserver
- Transients that were updated
- …and more
So, besides the basics, this plugin can record long queries, show the template used on the specific page or URI, you can also record everything that comes from PHP, WordPress, theme, and plugins.
You can also view the queue for styles and scripts loaded by the theme and plugins installed on your website. You can see the requests, queries to the database, and quickly isolate the error and look for the fix.
You can now inspect the error log and look for a way to fix them. It will also be helpful to submit the errors and messages to the theme or plugin authors.