I’m creating a public use WordPress plugin and working on general error handling/logging. I’ve struggled to find any sort of “best practices” when it comes to WordPress plugins.
In the realm of regular PHP, it seems good practice to set your error_reporting
, create a custom error handler using set_error_handler
and then trigger_error
to capture/log details about an error. Is this a BAD idea in WordPress plugins?
In the large number of plugins I’ve looked at, it seems no one is using this practice, which is why I wonder. Do setting custom handlers like this interfere with other plugins or something? Is there a better way to handle/log errors in a WordPress plugin?
Thanks!
In WordPress, you would use the
WP_Error
class to handle errors.There are a set of constants that will determine if errors are reported, displayed or logged, that you would usually define on the
wp-config.php
:Of course, when developing, you should have
WP_DEBUG
astrue
, and set it tofalse
on a production enviroment.Check the Developer and Debug Bar plugins, and the WordPress Error Handling series of articles for more info