Make Debugging WordPress Easy

Make Debugging WordPress Easy

I have debugged WordPress issues for over 15 years and have seen a lot. Failed updates, PHP errors, JS errors, you name it. There are a few ways to help easily debug most WordPress issues:

  1. Debugging WordPress with a plugin: Add the Health Check & Troubleshooting plugin to your site (easiest)
  2. Debugging with an error log: Add the debug constant to your wp-config.php file (more advanced)

Debugging WordPress with a Plugin

The WordPress community created a great plugin called Health Check & Troubleshooting While logged in, this plugin allows you to view the site with all plugins and themes disabled so you can enable your site with the default theme and no plugins to start debugging the issue. This all happens while your site visitors still see the site without any changes. The plugin then allows you to enable your theme and plugins one by one until you see the issue. You can then easily see what is causing the issue and make the needed fixes or perhaps temporarily disable a plugin until a fix is in place. Once you are done debugging you can simply deactivate the debug mode for your account and all will function as everyone else sees it.

This is a great option if you do not have access to error logs, need a quick fix, or are not comfortable reading and debugging error logs. Further documentation: https://make.wordpress.org/support/handbook/appendix/troubleshooting-using-the-health-check/

Debugging WordPress with an Error Log

As a developer I prefer using the error log while debugging an issue so I can see more in depth information. This allows you to see exactly what and where the issue is. In order to make sure debugging is enabled you first need to add this to your wp-config.php file:

define( 'WP_DEBUG', true ); // Or false
if ( WP_DEBUG ) {
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );
    @ini_set( 'display_errors', false );
}

That code will then log all errors while not displaying the errors to the front end user. The default debug log location is located at wp-content/debug.log. Please note: when you are done debugging any error, make sure to change that ‘WP_DEBUG’, true to ‘WP_DEBUG’, false. Those log files can get large and could cause issues if accidentally left enabled.

Once you have the logging enabled – if you are able to log into the server via SSH – you can then use a command to watch the log file as it is written to with

tail -f {path}/wp-content/debug.log (you would need to define the full path to the log file). If you do not have SSH access – you can simply load the page where you see the error on the front end and then open the debug log to see errors.

Errors will look something like this:

[11-Apr-2023 12:25:39 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function is_product() in /var/www/html/wp-content/themes/test-theme/functions.php:139
Stack trace:
#0 /var/www/html/wp-includes/class-wp-hook.php(308): test_menu_script()
#1 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()
#2 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action()
#3 /var/www/html/wp-includes/script-loader.php(2194): do_action()
#4 /var/www/html/wp-includes/class-wp-hook.php(308): wp_enqueue_scripts()
#5 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()
#6 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action()
#7 /var/www/html/wp-includes/general-template.php(3049): do_action()
#8 /var/www/html/wp-content/themes/test-theme/header.php(22): wp_head()
#9 /var/www/html/wp-includes/template.php(783): require_once('...')
#10 /var/www/html/wp-includes/template.php(718): load_template()
#11 /var/www/html/wp-includes/general-template.php(48): locate_template()
#12 /var/www/html/wp-content/themes/test-theme/home.php(3): get_header()
#13 /var/www/html/wp-includes/template-loader.php(106): include('...')
#14 /var/www/html/wp-blog-header.php(19): require_once('...')
#15 /var/www/html/index.php(17): require('...')
#16 {main}
  thrown in /var/www/html/wp-content/themes/test-theme/functions.php on line 139

Looking at that – I can then see the error stems from a function that is undefined ( is_product() ) which is on line 139 of /var/www/html/wp-content/themes/test-theme/functions.php. I would then open up that functions.php file and make the needed change.

Further documentation: https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#wp-debug

While there are other ways to debug errors in WordPress – when I get called with an emergency – I always start with one of these options. If you run into an issue on your site I would highly recommend you start with one of the two options documented above. If you still have issues – feel free to contact us.

Leave a Reply

Your email address will not be published.
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.