21

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!

1
  • This (Yoast's) workaround is extremely useful for on the fly debugging. I also enabled the logging which works well. I slightly modified my code: if ( isset( $_GET['bug'] ) ) so I visit link/?bug to see debug :) Commented Mar 3, 2017 at 3:13

6 Answers 6

17

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

3
  • 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
    Commented Oct 17, 2012 at 14:42
  • 6
    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
    Commented Oct 17, 2012 at 14:51
  • @Rarst +1!!! And as of 5.1 we can now specify the location very easily, define('WP_DEBUG_LOG', '/server/path/here/debug.log');
    – deflime
    Commented Jun 10, 2022 at 0:16
10

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 );
}
5
  • 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
    Commented Oct 17, 2012 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
    Commented Oct 17, 2012 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. Commented Oct 17, 2012 at 16:33
  • Ah very cool workaround - will definitely give this a shot
    – Zach
    Commented Oct 17, 2012 at 20:53
  • 1
    @s_ha_dum: It's not like one remembers every answer - at least I don't (I've googled my own answer before). This one I do remember though. I was the first to answer and had written absolute crap. Didn't apply at all. I follow a policy to not answer unless I am at least 99.5% certain I am qualified to (I assume about the same goes for you) - here I had been way off target. That bothered the living sh*t outta me, so a few hours later, after this had gotten an accepted answer, I was still thinking about this one and came up with the above. I too think it's pretty slick - thanks for noticing. Commented Mar 25, 2013 at 0:10
4

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.

3
  • 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
    Commented Oct 17, 2012 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
    Commented Oct 17, 2012 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
    Commented Oct 17, 2012 at 15:21
3

If you have static IP, you can do that:

if ('YOUR_IP_ADDRESS' == $_SERVER['REMOTE_ADDR']) {
    define('WP_DEBUG', true);
} else {
    define('WP_DEBUG', false);   
}

Source: DEBUGGING WORDPRESS – HOW TO USE WP_DEBUG ON PRODUCTION SITE

1

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.

0
0

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

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