WordCamp SF 2011: Debugging in WordPress
Upcoming SlideShare
Loading in...5

WordCamp SF 2011: Debugging in WordPress



The slides for my Debugging in WordPress talk at WordCamp San Francisco, on August 13, 2011.

The slides for my Debugging in WordPress talk at WordCamp San Francisco, on August 13, 2011.



Total Views
Views on SlideShare
Embed Views



23 Embeds 1,175

http://www.ilovecolors.com.ar 489
http://planetwordpress.planetozh.com 274
http://pushkarcreations.com 99
http://developingdreams.org 96
http://feeds.feedburner.com 81
http://mukundthanki.me 54
http://twitter.com 16
https://twitter.com 12
http://localhost 10
http://lanyrd.com 9
http://us-w1.rockmelt.com 5
http://mundo-powerpoints.blogspot.com 5
http://www.onlydoo.com 4
https://si0.twimg.com 4
http://a0.twimg.com 3
http://wpspeciali.st 3
http://www.twylah.com 2
http://www.zhuaxia.com 2 2
http://nuevospowerpoints.blogspot.com 2
http://planetewordpress.planetozh.com 1
http://debugggg.wordpress.com 1
http://feedproxy.google.com 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • @batmoo Ive used something similar that adds a few extra options http://wordpress.org/extend/plugins/debugwp/
    Are you sure you want to
    Your message goes here
  • The Debug Bar Extender plugin is also a must: http://wordpress.org/extend/plugins/debug-bar-extender/
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

WordCamp SF 2011: Debugging in WordPress WordCamp SF 2011: Debugging in WordPress Presentation Transcript

  • Debugging in WordPressWordCamp San Francisco 2011August 13, 2011
  • Andrew NacinCore Developer of WordPressTech Ninja at Audrey Capitalnacin@wordpress.org@nacin on Twitter
  • var_dump( $data ); die( ); // fin.
  • “I love suppressing errormessages!” — no one, ever.
  • So lets start withWP_DEBUGdefine( WP_DEBUG, true );
  • What WP_DEBUG does:— Sets the error reporting level toinclude PHP notices— Bypasses some error suppression— Triggers notices for deprecatedusage (more on that later)
  • You can change its output:// A default of true forces on display_errors.define( WP_DEBUG_DISPLAY, true );// Setting it to false will let PHP (php.ini) decide.define( WP_DEBUG_DISPLAY, false );// So, if your php.ini has display_errors on, youll also need to turn it off:ini_set( display_errors, 0 );
  • Likely being simplified in 3.3:// A default of true forces on display_errors.define( WP_DEBUG_DISPLAY, true );// False forces off display_errors.define( WP_DEBUG_DISPLAY, false );// Null will let PHP (php.ini) decide.define( WP_DEBUG_DISPLAY, null );
  • You can specify an error log:// wp-content/debug.logdefine( WP_DEBUG_LOG, true );// New in 3.3! (Likely.)define( WP_DEBUG_LOG, /tmp/php-errors );// Follow ticket #18391, created last night.
  • Debug admin CSS & JS withSCRIPT_DEBUGdefine( SCRIPT_DEBUG, true );
  • SCRIPT_DEBUG preventsminification and concatenation ofcore scripts and styles.load-scripts.php?c=1&load=global,wp-admin,ms…versus global.dev.css, wp-admin.dev.css, ms.dev.css
  • SCRIPT_DEBUG is good for:— Finding bugs in your admin screens— Studying the admin theme— Contributing to core
  • Debug queries withSAVEQUERIESdefine( SAVEQUERIES, true );
  • SAVEQUERIES stores query data in$wpdb->queries.(OMG BBQ:This is NOT for production.)
  • Query: SELECT * FROM wp_usersWHERE user_login = nacinBacktrace: WP->init,wp_get_current_user,get_currentuserinfo,wp_validate_auth_cookie,get_user_byExecution time: 0.4ms
  • On using these in production:WP_DEBUG— Dont display errors— Dont log to wp-content/error.logSCRIPT_DEBUG— Bad idea.SAVEQUERIES— Barry will hunt you down.
  • Plugins
  • Your new best friend:The Debug BarIt’s like Firebug for your WordPress.
  • Debug Bar ConsoleA Firebug-like console forPHP and MySQL (really)
  • Whats stopping you frombuilding your own DebugBar extension?Nothing.
  • Log Deprecated Notices— Logs the usage of deprecated files,functions, and function arguments.— Pinpoints where the deprecatedfunctionality is being used.— NEVER use in production.
  • Theme Check— Test your theme for the latestWordPress standards and practices.— Required for the theme directory.
  • Common functions whendebugging— error_log and var_export / print_r— var_dump and sometimes die— debug_backtrace
  • Tracking it down
  • Step 1Disable pluginsSwitch to default theme
  • What might be left behind?Cron, Rewrites,Roles/Capabilities
  • Drop-ins— advanced-cache.php— db.php— object-cache.php— mu-plugins
  • Whats going on?Query vars set properly?Main query SQL look right?Rewrite rule matched?
  • Funny redirect?Confirm its canonical.remove_action( template_redirect, redirect_canonical );
  • Testing multisite?I leave this in my config:// define( MULTISITE, true );
  • Dig into the codebase.Your friends are phpxref, grep.Learn the stack.Learn what things call what.
  • Some common traps
  • Step 1Is your code even running?(Spell the hook right?)die( wtf ); or error_log( )
  • White screen on thefrontend, no errors?Appearance > Themes
  • User interface doesnt work?Check the browsers JS consolefor an error, usually a conflict.
  • Internal server errors?Infinite redirects. Check:— home and siteurl— then .htaccess— then canonical
  • Widgets got moved around?Do the sidebars have IDs?Otherwise, its the order in whichregister_sidebar() is called.
  • Lots of pages? Slow site?My next Q: Whats yourpermalink structure?No longer an issue in 3.3!
  • Weird Bug #1Somewhere deep in style.css:“Template: home.php”Miscalculated as a multisite bug:activated on single site beforestyle.css had the headers.
  • Weird Bug #2The admin looked weird
  • Weird Bug #2The admin looked weirdTrigger: Upgrade from 2.5 to 3.1
  • Weird Bug #2The admin looked weirdTrigger: Upgrade from 2.5 to 3.1Problem: No MySQL ALTERpermissions
  • Weird Bug #3Some bbPress rewritesfailed after activation
  • Weird Bug #3Some bbPress rewritesfailed after activationProblem: Custom post typerewrites arent registered onactivation
  • // Say you have:add_action( generate_rewrite_rules, function( $wp_rewrite ) { … } );// And:register_activation_hook( __FILE__, function() { flush_rewrite_rules( );} );// But! This wont work for CPTs. Try this:add_action( init, my_register_post_types );register_activation_hook( __FILE__, function( ) { my_register_post_types( ); flush_rewrite_rules( );} );
  • Local Development
  • /etc/hosts127.0.0.1 andrewnacin.comConfigure virtual hostsInstall local WordPress
  • Replace links with anoutput buffer fordevelopment or stagingenvironments
  • // Run this on development or staging.// A development wp-config.php or local-config.php is fine.ob_start( nacin_dev_urls );function nacin_dev_urls( $buffer ) { $live = http://andrewnacin.com; $dev = http://dev.andrewnacin.com; return str_replace( $live, $dev, $buffer );}
  • URLs are more portablewhen theyre absolute.Really.*The serialized stuff is lame though.
  • Start of a conversationXdebug (backtraces, profiling)KCacheGrind (visualization)Using an IDEUnit testing
  • And finally, remember:Friends dont let friendsdevelop withoutWP_DEBUG.
  • Thanks! Questions?@nacin