Enable Debugging And Logging For Live Site Usage

jaredwilli
Developer, designer, and writer of New2WP, and several other sites. I'm also the developer and magician behind one of the biggest giveaways of WP stuff ever and social awards site, 2010 WPHonors. Check it out!
jaredwilli
jaredwilli

Latest posts by jaredwilli (see all)

jaredwilli
You can get your own content published on this site as long as you have CommentLuv installed on your site.

Doing so means you get exposure to thousands and thousands of other CommentLuv users and your posts get sent out to the massive subscriber list.

Google loves this site and indexes it multiple times per day and posts always get lots of comments so you can be sure of some excellent exposure.

See the Write For Us page for more details

btw.. you can get this author box here

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.

 

Comments

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


  2. Twitter:
    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


  3. Twitter:
    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 recently posted..Video Broadcasting Equalizer- WebcamMax With UStreamtv and SkypeMy Profile


  4. Twitter:
    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 recently posted..Your Blog Is Your Baby Treat It Like OneMy Profile

  5. I already turned my blog into a do follow blog. I really like comment love and think it’s

    really cool but I’m having a problem with it on my site. Do you know if there is any

    support for this plugin. my recent post Commentluv Enabled,Enable Commentluv


  6. Twitter:
    Great tip man. They will definitely help me on a few of the project that I have been working on. It is always great to get a few take ways from an article. Keep up the GREAT posts!!
    coreyrab recently posted..LinkedIn Tips for Small BusinessMy Profile

  7. Came to this post via http://wordpress.stackexchange.com/questions/1567/best-collection-of-code-for-your-functions-php-file.

    Glad I did; now i know about comment love too – something that will be implemented once I start giving my own blog some design love and actually do something with it!
    Vics recently posted..Coping with stress the Rosevibe wayMy Profile


  8. Twitter:
    Thank you for the great insight. Coding can be extemely tricky, hence a backup is necessary at all times. It is so easy to get caught up in fixing errors, that we might end up changing something else. Saving backup on an external device would be a good idea.
    Jennifer Rai recently posted..Get Outside Organized for AutumnMy Profile


  9. Twitter:
    Hey pal, I’m totally new to all this stuff! I found it very interesting, especially because I’m a new user. I would love you to help me work on this one since it’ll be too difficult to do it all by myself. Thank you.
    Mario recently posted..Aweber Review. The Email SoftwareMy Profile

  10. Hi, thank you for this nifty little code. I works very well and it is none intrusive on a live site.

    I have used it already to test some of my testing sites.

    Thank you kindly

Speak Your Mind

*

CommentLuv badge
%d bloggers like this: