Define WP_DEBUG conditionally / for admins only / log errors (append query arg for all links?)

I’m developing a site on a server that the client has access to as well and what I’d like to do is show WP_DEBUG only for administrators. Referencing Yoast’s article on a way around this:

if ( isset($_GET['debug']) && $_GET['debug'] == 'true')
    define('WP_DEBUG', true);

would show WP_DEBUG only for URLs that have ?debug=true attached to them, like http://domain.com/?debug=true

I was thinking that the Debug Bar might hold some of this information in there by default (whether or not WP_DEBUG is turned on), but I was thinkin craziness as I don’t believe that is the case.

So, what I was thinking would be useful, would be a check for the current user (having the manage_options capability and then run links through add_query_arg():

function zs_admin_debug() {
    if (!current_user_can('manage_options')) {
        add_query_arg('debug','true');
    }
}

but what I’m unsure about is – is there a hook I can use to effect all links on a site with this? This way, admins always see debug which I thought would be extremely useful. Thanks for any help as always!

6

I don’t think there is a universal URL hook. There are a lot of hooks and I may have missed it, but I don’t think there is one. You can look through the hooks at adambrown.info. There are a lot of URL hooks, but not a universal one.

If I may suggest another solution: Log the errors to a files.

/**
 * This will log all errors notices and warnings to a file called debug.log in
 * wp-content (if Apache does not have write permission, you may need to create
 * the file first and set the appropriate permissions (i.e. use 666) ) 
 */
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors',0);

That code is straight from the Codex for the wp-config.php file. If you do that, you won’t have to worry about juggling $_GET or sorting out who is and who isn’t an admin.

Edit:

I forgot one possible solution. You can do this with Javascript. A short script could attach your parameter to all the URLs on the page, and you can pretty easily load the script only for admins.

I’d still suggest the ‘log’ solution since errors for everybody are logged. If your people are like mine and send error ‘reports’ like “hey, the site is broken when you do that form” you will appreciate the log. 🙂

Leave a Comment