Everyone knows that. You work on a website and you run into issues. Sometime you even get a critical error like this:
But in some cases, you don’t get the error on every single request. Then you have to activate the debug mode and log all errors.
Activating the debug mode
To activate the debug mode for WordPress you simply have to set some constants in your wp-config.php
file. These are the defaults:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
This will not show any errors on the frontend or backend, but it will log them to the file wp-content/debug.log
and as this file is public, it’s highly recommended to either protect this file using some server configurations (your might want to block access to any .log
file) or you give it a random name:
define( 'WP_DEBUG_LOG', '/var/www/html/wp-content/debug-4f63f64.log' );
Now any PHP error will be written to that file. But also any PHP notice or warning. If you happen to have a lot of them (maybe due to some old plugin/theme code on a new PHP version) it might be hard to find the PHP errors in the log file.
Set the debug level
Fortunately you can set the debug level in PHP with the error_reporting function with takes either a numeric value (bitmask) or some named constants. The function call might look like this, when you only want to show any type of errors:
error_reporting( E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR );
You might want to put this file in the wp-config.php
file just below the constants from above. Unfortunately, this will not work. That’s because WordPress will always call error_reporting( E_ALL )
whenever you have set WP_DEBUG
to true
(or 1
). So how can we still set it?
You have to set the debug level once WordPress has set it’s default. The best place for that (without modifying the core, which you should never do) is using a Must-Use-Plugin. They are loaded before any regular plugin and before the theme is loaded. So create a file like wp-content/mu-plugins/000-set-debug-level.php
and copy the line into this file (add a small plugin comment so you see a plugin name in the backend):
<?php
/**
* Plugin Name: Control debug level
*/
error_reporting( E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR );
Now only errors will be logged into the debug log file. This should help your finding those errors without having the log file to grow in size and making it harder to find the errors.
Conclusion
Unfortunately we don’t have a constant to set the debug level and setting it too early will not work. But with a little mu-plugin we can still make it work.
This is my first blog post of 2021. Just as last year I will participate again at the #project26 hashtag. Last year I’ve postet my English blog posts in one week and translated them in the following week. As I was not able to publish the English blog post in the first week of 2021, I will now publish the post in both language in the same week.
WOW! And there I was having to debug with ugly messages right there on the frontend (I’m embarassed to admit).
THANK YOU! Now I’ll be able to do it better next time 🙂
#Project26 😉
I’m gald this helped you. So then it was probably more the hint about how to activate the
WP_DEBUG_LOG
, right?