Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

3 Answers

up vote 12 down vote accepted

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

share|improve this answer
    
I guess I'm just spoiled to seeing them on the screen :) but a log file does make more sense here. Will do a bit more research on my end, but this does appear to be the best solution I've come across thusfar. Thanks! –  Zach Oct 17 '12 at 14:42
4  
Note that native logging is hardcoded to log to web accessible file, which is not so good idea in production. It is better to configure private (out of web folder) log file location via PHP means for live sites. –  Rarst Oct 17 '12 at 14:51
add comment

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, for the sake of completeness:

functions.php:

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

wp-config.php:

if( isset( $_COOKIE['wp_debug'] ) && 'on' === $_COOKIE['wp_debug'] ) {
    define('WP_DEBUG', true);
} else {
    define('WP_DEBUG', false);
}
share|improve this answer
    
Andrew Nacin commented on that article, mentioning init is too late to have any effect. I also tried this and it did not work. –  Zach Oct 17 '12 at 14:45
    
Constants cannot be redefined. One could modify error reporting level without touching constant but it's not perfect. Also there is plenty of things happening before init that are of interest. –  Rarst Oct 17 '12 at 14:46
    
I think the accepted answer remains the most viable solution, nonetheless, for the sake of completeness, this new approach ought to do the job with on-screen debug notices. –  Johannes Pille Oct 17 '12 at 16:33
    
Ah very cool workaround - will definitely give this a shot –  Zach Oct 17 '12 at 20:53
    
+1 Just noticed this. I like that. –  s_ha_dum Mar 24 '13 at 21:22
show 1 more comment

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.

share|improve this answer
    
I actually like this idea as well - so if I did something like pastebin.com/m22KNakh that could... in theory work, correct? Running echo $_SERVER['REMOTE_ADDR'] yields ::1 that expected on localhost? It honestly sounds like a separate log file and this way (as home IPs seem to change all the time) might be a good idea. –  Zach Oct 17 '12 at 15:05
    
@Zach yeah, ::1 is just IPv6 version of 127.0.0.1. Dynamic IP makes is less convenient, but anyway I only treat this as temporary troubleshoot technique for live stuff. Doesn't replace proper local debug setup. –  Rarst Oct 17 '12 at 15:19
    
Ah thanks for letting me know. Definitely like both options here, I'm going to go with @s_ha_dum answer (in addition to your comment that I upvoted) which is also great. Thanks again, I really do appreciate it! –  Zach Oct 17 '12 at 15:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.