Have you ever been doing something on your live, publicly accessible site, that has enough visitors and traffic that disabling it to fix a Php bug would be too costly to do?
I’m sure that any of you who have done any kind of development on a WordPress site know how it can be to port your locally hosted site, theme, or plugin, over to your live production server, and not experience any errors, or Php problems of any kind at all. I’m going to show you how to make debugging your code easier.
1. Make A Backup Of Your Config File
This is a very important thing to do. I can’t tell you how many times I’ve over-written files that I find to be broken, and am left with no backup copy to restore to. I’ve spent hours just re-writing files so they work properly again.
Save a copy of your wp-config.php file
The wp-config.php file is in the root directory of your WordPress site, where the folders /wp-content, /wp-includes, and /wp-admin are located. Copy the config file somewhere safe where you won’t be tempted to edit and coincidently save over it.
Next, open your working copy of the config in a text editor, Dreamweaver, Notepad, whatever. Look for the line of code that says
define('WP_DEBUG', false);
If it doesn’t exist for some reason, that’s fine, if it does, just comment it out for now by adding slashes before it: // define(‘WP_DEBUG’,….). We will be replacing that useless line of code with the new hottness.
2. Getting Url Parameters If They Exist And Do Stuff
We need to create an IF/ELSE statement, which will check if the debug parameter is set, and if yes, do stuff based on its value, otherwise ignore it.
if ( isset($_GET['debug']) && $_GET['debug'] == '1' ) { // enable the reporting of notices during development - E_ALL define('WP_DEBUG', true);
The first part of the conditional: isset($_GET['debug']) checks if the current URL has a parameter of ?debug attached to it. AND if also, if the value of it is equal to 1, ?debug=1. Within the brackets is what should run if these things are both true. Setting WP_DEBUG to true will enable reporting of Notices.
Another Parameter Value To Force Errors To Display
Same as above only we set the debug value to $_GET['debug'] == ’2′, and have added the WP_DEBUG_DISPLAY constant set to true. This will force errors to be displayed.
} elseif ( isset($_GET['debug']) && $_GET['debug'] == '2' ) { // must be true for WP_DEBUG_DISPLAY to work define('WP_DEBUG', true); // disable the display of errors define('WP_DEBUG_DISPLAY', true);
Add A Parameter To Log The Errors
Adding this piece enables you to pass the number 3 in the URL parameters to save a log file of all errors to the directory /wp-content/debug.log. It won’t show anything different on the page from either of the other two previous things, it just saves a file.
} elseif ( isset($_GET['debug']) && $_GET['debug'] == '3' ) { // must be true for WP_DEBUG_LOG to work define('WP_DEBUG', true); // log errors to debug.log in the wp-content directory define('WP_DEBUG_LOG', true); }
You should understand that the second and third must have define(‘WP_DEBUG’, true); before the WP_DEBUG_DISPLAY and W_DEBUG_LOG, otherwise they will not do anything.
4. Here’s The Entire Code
Just replace the one line in your wp-config.php file where WP_DEBUG is set to false, with the following:
/** * @wp-config.php replaces WP_DEBUG constant * Enable WP debugging for usage on a live site * http://core.trac.wordpress.org/browser/trunk/wp-includes/load.php#L230 * Pass the '?debug=#' parameter at the end of any url on site * * http://example.com/?debug=1, /?debug=2, /?debug=3 */ if ( isset($_GET['debug']) && $_GET['debug'] == '1' ) { // enable the reporting of notices during development - E_ALL define('WP_DEBUG', true); } elseif ( isset($_GET['debug']) && $_GET['debug'] == '2' ) { // must be true for WP_DEBUG_DISPLAY to work define('WP_DEBUG', true); // disable the display of errors define('WP_DEBUG_DISPLAY', true); } elseif ( isset($_GET['debug']) && $_GET['debug'] == '3' ) { // must be true for WP_DEBUG_LOG to work define('WP_DEBUG', true); // log errors to debug.log in the wp-content directory define('WP_DEBUG_LOG', true); }
Then on any page of your site, add this to the end: ?debug=1 or 2 or 3
http://example.com/?debug=1
http://example.com/?debug=2
http://example.com/?debug=3
If you have any problems after adding this code, you can always revert back to using that safely stored backup copy you were supposed to make before doing anything. You can also post a comment below, of course, and I will try to help with anything you may be unclear about.
Hope this helps make your debugging much easier.
Twitter: commentluv
says:
great tip Jared!
It certainly will help with some development sites I am working on. Perhaps with a secret password though to prevent just anyone from peaking inside my blog innards…
Twitter: new2wp
says:
Yeah that might be a good idea. I wouldn’t go around telling people you use it, or sharing the url for showing it. That could possibly even be a security risk, if you had a bad piece of code erroring out, or showing a Notice.
I’ll try to revise it with a if (current_user_can(‘manage_options’)) thing, or something. so only admins logged in can use it.
jaredwilli´s recent blog post ..WordPress 30 Basic – A Theme Framework Released By New2WP
Twitter: lavenderuses
says:
Thanks Jared
Have a techie visiting me tomorrow. Will get him to check this out and make sure everything is in order.
Patricia Perth Australia
Patricia´s recent blog post ..A Dental visit with a difference…where’s the lavender
Twitter: new2wp
says:
Please do, and be sure to let me know if either of you have any feedback on doing this. I’d love to know, and improve it anyway I can (aside from making it viewable by just anyone).
jaredwilli´s recent blog post ..WordPress 30 Basic – A Theme Framework Released By New2WP
Twitter: CoachNotesBlog
says:
Hi Jared – Thanks for sharing this. I’m working on optimizing my sites now to get rid of out-of-memory errors and those dreaded Internal 500 server errors. Having done the first line work, I’m ready to take a second pass. Your code will make debugging much easier. I’ll look for your modifications where only logged in admins can use it. Will you post that here as an update or should we just check your site for it?
Vernessa Taylor´s recent blog post ..Video Broadcasting Equalizer- WebcamMax With UStreamtv and Skype
Twitter: Ileane
says:
Hi Jared, I’m totally new to all these PHP tricks, so this one I’ll save for later. Ironically I was on your site yesterday because I was looking for an Author Bio Box plugin. It looks like a pretty good plugin but I didn’t have a chance to install it yet. Now that I’m getting to know you better through this guest post, I’ll head over and give it a try.
By the way, when will the submissions for the WPHonors award come to an end?
Thanks Jared.
Ileane Smith´s recent blog post ..Your Blog Is Your Baby Treat It Like One