Custom Error Handling in WordPress Plugins

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?

Read More

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!

Related posts

Leave a Reply

1 comment

  1. 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:

    define('WP_DEBUG', true);
    define('WP_DEBUG_DISPLAY', false);
    define('WP_DEBUG_LOG', true);
    define('SAVEQUERIES', true);
    

    Of course, when developing, you should have WP_DEBUG as true, and set it to false on a production enviroment.

    Check the Developer and Debug Bar plugins, and the WordPress Error Handling series of articles for more info