Every developer knows code is never perfect. Even at its best, it can be tweaked and optimized. In contrast, code can be riddled with bugs, syntax errors. typos, and generally poorly written and executed. This simultaneously causes nightmares for webmasters that use it. As a result, both groups need tools to catch errors and bugs (hence debugging), make necessary corrections, and repeat the process until no issues come up. Oftentimes, in the early stages, this can take longer than writing the code itself. Luckily, learning how to debug in WordPress shouldn’t be that complicated. Let’s begin.
1. Enable WP_DEBUG
WP_DEBUG is a constant that triggers debug mode in WordPress, documents errors into a single log file, and lets you know exactly where the problem lies. We demonstrated the procedure under the headline titled How to use WordPress Debug Mode when we taught you to fix the “This site is experiencing technical difficulties“ error.
2. Activate PHP error logging
First, check whether the cPanel of your web hosting provider has a PHP error logging option like “Kinsta” and “Hostinger” do. If it doesn’t, follow these steps:
- Access your website via FTP.
- Open the public_html (can be called root, www, or yourwebsite) folder and create a new document file named error_log.txt inside.
- Now, open the .htaccess file in the same folder, and paste the following code at the bottom:
php_flag log_errors on
php_value error_reporting 32767
php_value error_log "error_log.txt"
- Save changes, and confirm you want to upload and replace the file.
- Check the error_log.txt file, and you’ll see which files are problematic, what the problem is, and on which line the error occurred.
3. Debug WordPress database
Here are 2 ways to find WordPress database issues:
1. Enable WPDP error logging
This method will record various WordPress database-related errors, for example, trouble establishing a database connection. So, to activate WPDP error reporting, do this:
- Get to the public_html folder using FTP once again.
- Now, go to the wp-includes folder, then open the file named wp-db.php.
- Search for wpdb class, then locate the $show_errors variable below.
- Change the variable from $show_errors = false; to $show_errors = true;
- Now any given query will produce SQL errors to the screen.
2. Turn on WordPress database query logging
This is an alternative method that might prove easier for some users. To begin logging WordPress database queries, do this:
- Open the public_html/wp-config.php file, then add the following line:
define('SAVEQUERIES', true);
- Now add a code snippet or edit/create a site-specific plugin with this code:
<?php
global $wpdb;
print_r( $wpdb->queries );
?>
- Tip. This takes a toll on system resources. You should delete the code snippet and change true to false or delete the line in step 1 after you’re done debugging.
4. Debug WordPress via a staging website
A staging website is a clone of your main website you can use to experiment with code safely. It’s also not visible to search engines or visitors, so you have complete freedom to tune up and troubleshoot things or fix errors and bugs. Many web hosting providers offer a quick and easy way to do this as we demonstrated in method 2 of “update PHP in WordPress”. We also provided a workaround if yours doesn’t.
5. Activate SCRIPT_DEBUG
If you’ve read our guide on killer ways to increase website speed, you must’ve installed one of the top caching plugins for WordPress. And while minifying PHP, JavaScript, and CSS code reduces website loading time, the action can conceal underlying errors. To temporarily make WordPress load non-minified versions of these files, open the public_html/wp-config.php file again, then add the following line of code:
define( 'SCRIPT_DEBUG', true );
Tip. Don’t forget to delete the line once you’re finished debugging.
6. Test PHP code externally
On some types of web hosting, especially shared hosting, root access to your WordPress website or the server it’s hosted on is forbidden. This becomes a problem when you need a way to debug WordPress. Luckily, you can use one of the following solutions to examine your PHP code:
- Standalone online. Use a free service such as “PHP Code Checker” (phpcodechecker.com).
- IDE framework software. Two examples include Eclipse PHP Development Tools (eclipse.org/pdt/) and “JetBrains’ PhpStorm” (jetbrains.com/phpstorm/)
7. Debug via WordPress plugins
If the methods above prove too difficult, install WordPress plugin(s) designed to debug in WordPress. Some examples, which we’re not affiliated with, include:
- Query Monitor (wordpress.org/plugins/query-monitor/). An all-in-one developer tools panel that tracks PHP, database, scripts, stylesheets, WordPress hooks and actions, Ajax, HTTP and REST API calls, and more.
- Debug Bar (wordpress.org/plugins/debug-bar/). Implements a debug bar at the top of the WordPress Admin Section.
- WP Debugging (wordpress.org/plugins/wp-debugging/). The WordPress developer for the plugin recommends installing this one alongside the two above for best results.