WordPress Function to Enable Error Debugging And Logging To Use On Live Sites

Enable Error Debugging And Logging To Use On Live Sites

This is a piece of code I wrote to make use of the WP_DEBUG constants that are normally disabled by default. Well I created a way to not only enable WP_DEBUG so you can use it on a live site with no negative side-effects, but I also made use of the other debugging constants for forcing errors to be displayed, and for creating a log file of the errors and Notices in the /wp-content directory.

Drop this code in your wp-config.php file ( AFTER YOU SAVE A BACKUP JUST IN CASE ) and then you can pass the ?debug=1, 2, or 3 parameters at the end of any url on your site.

?debug=1 = shows all errors/notices
?debug=2 = forces them to be displayed
?debug=3 = creates a debug.log file of all errors in /wp-content dir.

/**
* Written by Jared Williams - http://new2wp.com
* @wp-config.php replace WP_DEBUG constant with this code
* Enable WP debugging for usage on a live site
* http://core.trac.wordpress.org/browser/trunk/wp-includes/load.php#L230
* Pass the '?debug=#' parameter at the end of any url on site
*
* http://example.com/?debug=1, /?debug=2, /?debug=3
*/
if ( isset($_GET['debug']) && $_GET['debug'] == '1' ) {
    // enable the reporting of notices during development - E_ALL
    define('WP_DEBUG', true);
} elseif ( isset($_GET['debug']) && $_GET['debug'] == '2' ) {
    // must be true for WP_DEBUG_DISPLAY to work
    define('WP_DEBUG', true);
    // force the display of errors
    define('WP_DEBUG_DISPLAY', true);
} elseif ( isset($_GET['debug']) && $_GET['debug'] == '3' ) {
    // must be true for WP_DEBUG_LOG to work
    define('WP_DEBUG', true);
    // log errors to debug.log in the wp-content directory
    define('WP_DEBUG_LOG', true);
}

I go into more detail on the guest post I wrote for Comluv if you’re interested, here:
http://comluv.com/dev/enable-debugging-and-logging-for-live-site-usage/

I’m still working on a way to make this either password protected, or preferrably somehow make it work on if (current_user_can(‘manage_themes’) and is_logged_in().

But that’s where it gets alot more tricky.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *