Terence Eden’s Blog on Nostr: Find the URl causing your WordPress Error ...
Find the URl causing your WordPress Error
https://shkspr.mobi/blog/2023/10/find-the-url-causing-your-wordpress-error/
PHP has some pretty good error handling and logging, but I do sometimes find it confusing. For example, look at this warning message:
[18-Oct-2023 12:34:56 UTC] PHP Warning: Something bad happened in /wp-content/something.php on line 123
OK, so we can go to something.php and scroll to line 123 and try to figure out what's gone wrong. But we don't know which page, post, or URl caused the error. If the error only occurs on /page/test?input=6 and not /page/test?output=7 that would be handy to know, right?
So here's some PHP you can stick in your theme's functions.php file:function custom_warning_handler($errno, $errstr, $errfile, $errline) { $url = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : 'Unknown URL'; error_log("Warning at $url: [$errno] $errstr in $errfile on line $errline");}set_error_handler('custom_warning_handler', E_WARNING);
Now your error_log file will contain:
[18-Oct-2023 12:34:56 UTC] Warning at example.com/blog/page.php?foo=bar: [2] Cannot apply fuzz to kittens in /wp-content/something.php on line 123
You can find out more about set_error_handler() and how to configure it.
https://shkspr.mobi/blog/2023/10/find-the-url-causing-your-wordpress-error/
#HowTo #php #WordPress
https://shkspr.mobi/blog/2023/10/find-the-url-causing-your-wordpress-error/
PHP has some pretty good error handling and logging, but I do sometimes find it confusing. For example, look at this warning message:
[18-Oct-2023 12:34:56 UTC] PHP Warning: Something bad happened in /wp-content/something.php on line 123
OK, so we can go to something.php and scroll to line 123 and try to figure out what's gone wrong. But we don't know which page, post, or URl caused the error. If the error only occurs on /page/test?input=6 and not /page/test?output=7 that would be handy to know, right?
So here's some PHP you can stick in your theme's functions.php file:function custom_warning_handler($errno, $errstr, $errfile, $errline) { $url = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : 'Unknown URL'; error_log("Warning at $url: [$errno] $errstr in $errfile on line $errline");}set_error_handler('custom_warning_handler', E_WARNING);
Now your error_log file will contain:
[18-Oct-2023 12:34:56 UTC] Warning at example.com/blog/page.php?foo=bar: [2] Cannot apply fuzz to kittens in /wp-content/something.php on line 123
You can find out more about set_error_handler() and how to configure it.
https://shkspr.mobi/blog/2023/10/find-the-url-causing-your-wordpress-error/
#HowTo #php #WordPress