Debugging a PHP code is almost necessary for any project. For this purpose, WordPress uses a specific debugging system, to simplify and standardize the process of debugging WordPress, codes in plugins, addons, and the WordPress core.
In this article, we tend to review different ways of WordPress debug mode.
- Why use WordPress debug mode?
- Enabling the WordPress to debug mode with code
- Enabling WP_DEBUG
- Enabling WP_DEBUG_LOG
- Enable WP_DEBUG_DISPLAY
- Enable SCRIPT_DEBUG
- Enable logging query
- How to enable WordPress debug mode with plugins?
- The debug.log file can’t be located
- Does WordPress debug mode affect my website?
Why use WordPress debug mode?
By default, if we don’t make any changes to the core, add plugins, templates or any customization, enabling the WordPress to debug mode won’t change or fix anything. As soon as we install a plugin or a template, everything changes.
The main reason to enable WordPress to debug mode is to fix the occurred errors in PHP. If WordPress is hosted on localhost or a test server, it’s best to use the WordPress debug mode. Using the debug mode is necessary if you are designing a plugin or a template.
Enabling the WordPress to debug mode with code
By following the steps below, you can enable WordPress to debug mode.
We can use the WP_DEBUG to enable WordPress to debug mode. By default, the value is set to false, therefore, we need to make some changes to the wp-config.php file and change the value to true.
define( 'WP_DEBUG', true ); define( 'WP_DEBUG', false );
Using the WP_DEBUG isn’t recommended for live websites. It is best used for websites in localhost and test servers.
Enabling the WP_DEBUG may cause PHP errors and it will display them to you. In this case, you need to change the default values of PHP, only by causing too many PHP errors you are able to see the white screen.
Displaying all the PHP errors may cause other errors that have no issue to display. After identifying the code, fixing these errors becomes much easier. Finally, the fixed code is more resistant to bugs and any security holes.
By enabling the WP_DEBUG outdated arguments and functions currently used in your WordPress will be displayed to you. Some of these functions are no longer useable by WordPress but they are still in the core.
WP_DEBUG_LOG acts as a supplement to WP_DEBUG. By enabling it, all of the activities and errors will be stored in the debug.log file.
Keep in mind, by using the WP_DEBUG_LOG you can use the error_log() function in the PHP and make changes or write errors in the debug.log file and use this file in Ajax events.
When you change the value to true, you can save the system’s activities in the wp-content/debug.log file. You may also change this path.
define( 'WP_DEBUG_LOG', true ); -or- define( 'WP_DEBUG_LOG', '/tmp/wp-errors.log' );
To enable the WP_DEBUG_LOG, WP_DEBUG must be enabled beforehand.
By enabling this feature in WordPress, debug messages will be displayed in the HTML format. If this feature is enabled on live website(s), your website’s crucial information may be exposed. Therefore, enable this feature at your own risk. To enable this debug mode, add the following code to the wp-config.php file:
define( ‘WP_DEBUG_DISPLAY’, true );
define( ‘SCRIPT_DEBUG’, true );
Enable logging query
You can have a WordPress database query status statement as an array. This mode is used when there are any errors in the WordPress database.
To enable this debugging mode in WordPress, add the following code to the wp-config.php file:
By enabling this mode, all queries will be saved in the $wpdb->queries.
How to enable WordPress debug mode with plugins?
Debug Bar is one of the best plugins in this field. Although, it may not be updated regularly and this is the reason it may not be compatible with the latest WordPress versions.
The debug.log file can’t be located
Some hosting providers won’t allow you to create the debug.log file. They save all errors and notifications in a separate log file. Therefore, if you failed to locate the debug.log file, it may be found under error_log or in the /logs path.
But if you couldn’t find any log files, ask your host to give you the path. Although, by adding the following code to the wp-config.php you can create this file:
define(‘WP_DEBUG_DISPLAY’ , true);
By enabling this feature, the website visitors can also see the errors on WordPress, therefore, make sure you disable it once you have fixed the errors.
Does WordPress debug mode affect my website?
To answer this question, yes. Even when the query log is activated. Therefore, it is recommended to use debug mode in the localhost server or a staging website and make sure you disable it as soon as you have fixed any errors.