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

Read More

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!

Related posts

Leave a Reply

6 comments

  1. 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. ๐Ÿ™‚

  2. Even though my first approach was for the garbage bin and s_ha_dums answer is a clean, and probably the best, way of going about it, let me offer one more working scenario:

    The following sets a cookie that is valid for the next 24 hours (86400 seconds) when an administrator logs into the system. In wp-config.php, the constant WP_DEBUG is conditionally defined depending on the presence and value of said cookie.

    Caveat: WP_DEBUG will thereafter be set to true for everyone logging in from the same browser on the same machine on the same day.

    in functions.php (or as a plugin):

    function wpse_69549_admin_debug( $user_login, $user )
    {
        if ( in_array( 'administrator', $user->roles ) ) {
            setcookie( 'wp_debug', 'on', time() + 86400, '/', get_site_option( 'siteurl' ) );
        }
    }
    add_action( 'wp_login', 'wpse_69549_admin_debug', 10, 2 );
    

    See: Codex > Action Reference > wp_login

    in wp-config.php:

    if ( isset( $_COOKIE['wp_debug'] ) && 'on' === $_COOKIE['wp_debug'] ) {
        define( 'WP_DEBUG', true );
    } else {
        define( 'WP_DEBUG', false );
    }
    
  3. It doesn’t answer your question precisely, but from personal experience I found it is better to enable debug mode by matching IP address instead of URL.

    That requires to modification of links and solves how to identify admin before WP loads required user functionality.

  4. This is also possible trick, but you need to put this in your wp-config.php since WP_DEBUG is defined in there:

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

    Add ?debugsecret=debugsecret to the page URL you like to debug.

  5. Just to add another option, I made a combination of several answers. This one allows you to instantly change the debug mode state (enabled / disabled) for one hour with a url parameter and also restrict who can do the change by IP

    Replace your WP_DEBUG line in wp-config.php with this:

    //Set the default debug mode. Usually it should be false
    $CURRENT_DEBUG_MODE=false;
    //uncomment to set a restriction by IP.
    //$DEVEL_IP_ADDRESS = 'YOUR IP';
    
    //use cookie to set the debug mode for 1 hour with the url parameter ?debugmode = [on,off]
    if (!isset($DEVEL_IP_ADDRESS) || $DEVEL_IP_ADDRESS == $_SERVER['REMOTE_ADDR'])
    {
        if (isset($_GET['debugmode'])) {
            $CURRENT_DEBUG_MODE = 'on'==$_GET['debugmode']?true:false;
            setcookie("wp_debugmode", $_GET['debugmode'],time()+3600,'/');
        }
        else if (isset($_COOKIE['wp_debugmode'])) {$CURRENT_DEBUG_MODE = 'on'==$_COOKIE['wp_debugmode']?true:false;}
    }
    define('WP_DEBUG', $CURRENT_DEBUG_MODE);
    

    Add to any URL page ?debugmode=on to enable debug (if it’s disabled) and ?debugmode=off to disable it