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!
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.
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. ๐
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 totrue
for everyone logging in from the same browser on the same machine on the same day.in functions.php (or as a plugin):
See: Codex > Action Reference > wp_login
in wp-config.php:
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.
If you have static IP, you can do that:
Source: DEBUGGING WORDPRESS รขยย HOW TO USE WP_DEBUG ON PRODUCTION SITE
This is also possible trick, but you need to put this in your
wp-config.php
sinceWP_DEBUG
is defined in there:Add
?debugsecret=debugsecret
to the page URL you like to debug.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:
Add to any URL page
?debugmode=on
to enable debug (if it’s disabled) and?debugmode=off
to disable it