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
} 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.
[easyazon-block asin=”0814474578″ align=”center”]