Quick WP Tip: Simple WordPress debugging

After reading this post about the “WordPress white screen of death” I wanted to quickly share with you a quick hack I do in almost all WordPress installs I manage, that allows me to quickly switch on debug mode when needed.

In the file wp-config.php, usually found in your WordPress root, do the following:

if ( isset($_GET['debug']) && $_GET['debug'] == 'debug')
  define('WP_DEBUG', true);

Now I can open any page, and if something goes wrong there, like a white screen of death, I add ?debug=debug to its URL and see what’s causing the trouble. Pretty easy huh? :)

Now it might be wise to change that second ‘debug’ to a key of your own choosing, so not everyone out there can open debug mode on your blog.

Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

28 Responses

  1. digipBy digip on 17 August, 2010

    Could you add that to functions.php?

  2. SteveBy Steve on 17 August, 2010

    Brilliant! Sure could have used this before. Thanks Yoast!

  3. OzhBy Ozh on 17 August, 2010
  4. LeonBy Leon on 17 August, 2010

    Thanks. And just in case anyone doesn’t realise how to change that second ‘debug’ to a key of your own choosing, it’s the third debug in the line of code above – “== ‘debug’)”

  5. nexikBy nexik on 17 August, 2010

    It’s more secure to check if you are logged and have admin role. This way even if someone get your super secret key they won’t see anything until they are admins on the blog.

    global $current_user;
    get_currentuserinfo();
    
    if( isset( $_GET[ 'debug' ] ) && $_GET[ 'debug' ] == 'secret' && $current_user->user_level > 7  )
     define( 'WP_DEBUG', true );
    
  6. TheoBy Theo on 17 August, 2010

    Great, useful as always, thanks ! @nexik – neat idea.

  7. OzhBy Ozh on 18 August, 2010

    @nexik: when WP loads wp-config.php, functions such as the one getting info for the current user are not loaded.

    The proper way to implement what you suggest is with a simple plugin:

    add_action( 'init', 'my_debug');
    function my_debug() {
       if( current_user_can('update_plugins')
           && isset( $_GET['debug'] )
           && $_GET['debug'] == 'secret'
           && !defined( 'WP_DEBUG' )
       ) { define( 'WP_DEBUG', true ); }
    }
    

    (hope the comment formatting won’t crap out this)

    • Joost de ValkBy Joost de Valk on 18 August, 2010

      The comment formatting actually makes it look great ;)

    • Cristian O. BalanBy Cristian O. Balan on 25 August, 2010

      The Yoast suggestion on the wp-config file work fine but this code like a plugin don’t work. Some idea?

    • Andrew NacinBy Andrew Nacin on 28 August, 2010

      Unfortunately it’s too late here to define WP_DEBUG. Really, the only way to do this is to have a ‘secret’ as said.

      As long as you are logging WP_DEBUG instead of displaying it, you’re in better shape. Just don’t show them to the user.

  8. Pär ThernströmBy Pär Thernström on 18 August, 2010

    That’s so simple that it makes me angry that I did not think of it myself! :)
    Great tip, thanks.

  9. Tom HermansBy Tom Hermans on 18 August, 2010

    Yeah, this is pretty neat. Why didn’t I know about this when I was puzzled by al that WP code, functions etcetera.. giving me more whitescreens than my frustration could handle.. ;)

  10. ChrisBy Chris on 19 August, 2010

    Unfortunately I have the WSOD on the Dashboard and it doesn’t work there … I disabled the
    Dashboard:

    to avoid the WSOD, any ideas?

  11. ChrisBy Chris on 19 August, 2010

    ups … I meant

    >?php //wp_dashboard(); ?<

    • ChrisBy Chris on 19 August, 2010

      haha, stilt not quite … :-D

  12. RonBy Ron on 19 August, 2010

    Hi guys, im really a freaking noob here…hehe

    just one question though, can i just add the code directly to wp-config.php

    i was actually trying to have my blog on debug mode to check on something.,

    peace out

    • Joost de ValkBy Joost de Valk on 19 August, 2010

      Yes. :)

      • RonBy Ron on 19 August, 2010

        wow, thanks for the quick reply..:)

        peace out

  13. Tailleur d'ImagesBy Tailleur d'Images on 20 August, 2010

    hi !
    I’ve got a bug, but it doesn’t cause a white page, it create a big mess when I write a new post (i.e. it erases lines of text when i add a new line between 2 paragraphs, it put a whole paragraph in H2, asl…)

    so can i use this tip in order to fix it ? how ? i’ve been trying several things since now but it failed…

    thx for help !

  14. mikeBy mike on 27 August, 2010

    I copied/pasted both Yoost original code and nexik admin code in the wp-config.php file –
    and changed the third debug to my own secret word.
    (and tried with the original ?debug=debug too)

    but the only thing that happened was that the code-snippet was shown in the admin-page
    and in the pages on the blog. (on top of the page)
    and when pasting in the execution-snippet – nothing happens…
    is there something I am missing here…?

    regards

    mike

  15. Larry SwansonBy Larry Swanson on 31 August, 2010

    I’m having the same problem as Mike, and I notice that the link to the debug=true for this page is no longer working (I saw it a week or so ago). Perhaps something changed in a WP release?

    • Joost de ValkBy Joost de Valk on 31 August, 2010

      Yeah I disabled that on purpose :) has nothing to do with an upgrade :)

Trackbacks