Categories
Errors

How to Debug errors in WordPress

WordPress has excellent inbuilt features to help you debug. You can enable most of the features by making small changes in the wp-config.php file in WordPress and you can also use a few plugins listed below to easily debug not only PHP errors but also, database queries, HTTP calls blocks and many more.

Step 1: Enable debug mode

Add the PHP constant WP_DEBUG in the wp-config.php file with the value “true” to enable debug mode in your WordPress website. Here is the code : define( 'WP_DEBUG', true );. It’s set to false by default and once you enable it, you will be able to see error messages and other debug features.

Step 2: Enable error log

WordPress has a feature to log all error messages in a file named debug.log in the wp-content folder by default. You have to edit the wp-config.php file add the following code define( 'WP_DEBUG_LOG', true ); . You can also define a custom log file by defining the file location with the following code : define( 'WP_DEBUG_LOG', '/tmp/site-errors.log' );

Step 3: Display / Hide the error messages

You can also display or hide the errors directly on the respective pages. By default, when you enabled WP_DEBUG, it will display the error messages. But you may want to hide the error messages and only log them. You can use the following code to hide error messages in the front-end and log them : define( 'WP_DEBUG_DISPLAY', false ); . Remember that you still have to have the error log enabled to be able to see the error messages using the following code as given in step 2 : define( 'WP_DEBUG_LOG', true );.

Step 4: Save and display database queries

Sometimes slow database queries in WordPress can cause the whole site to load slower but you can debug it easily with WordPress’s inbuilt feature to save all database queries to a variable and print them anywhere on the page to analyze it. You can use the following code in the wp-config.php file, to enable it : define( 'SAVEQUERIES', true ); . This will save all database queries on a given page to a global variable $wpdb->queries. So, you still have to use the following code on the page to display the array with all the database queries: print_r($wpdb->queries);.

Step 5: Disable Fatal Error Handler

Whenever a fatal error occurs in a WordPress site, it doesn’t directly show it on the frontend but sends an email to the admin and helps users not get locked out from the admin section. This feature was introduced in WordPress 5.2 and I think it’s a wonderful feature to reduce the hassle for site owners. But if you are a developer, you may want to disable the fatal error handler with the following code in the wp-config.php file : define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true );.

Step 6 : Using plugins to debug errors in WordPress

WordPress has many plugins to help you debug PHP errors, warnings, deprecated messages, database queries, hooks and actions, block editor blocks, enqueued scripts and stylesheets, HTTP API calls and many more. Here is a list of the two most popular plugins that you can use for debug:

Query Monitor is my favorite plugin among the two, I highly recommend it to debug any issue in your WordPress website.

Complete code to debug in WordPress website :

// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

// Save database queries
define( 'SAVEQUERIES', true );

Debug is a very important part of any WordPress website project to make sure the site is functioning well without any issues. Hope I have listed most of the debug methods for it. Please comment below and let me know if you have any questions.

By Murali Kumar

I am a Full Stack Web Developer with over 12 years of experience and I love WordPress & Woocommerce.

If you need help of a WordPress developer, Get in touch now!.

Leave a Reply

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