wordcamp sf 2011: debugging in wordpress
DESCRIPTION
The slides for my Debugging in WordPress talk at WordCamp San Francisco, on August 13, 2011.TRANSCRIPT
![Page 1: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/1.jpg)
Debugging in WordPress
WordCamp San Francisco 2011 August 13, 2011
![Page 2: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/2.jpg)
Andrew Nacin Core Developer of WordPress Tech Ninja at Audrey Capital [email protected] @nacin on Twitter
![Page 3: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/3.jpg)
var_dump( $data ); die( ); // f in.
![Page 4: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/4.jpg)
“I love suppressing error messages!” — no one, ever.
![Page 5: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/5.jpg)
So let's start with WP_DEBUG define( 'WP_DEBUG', true );
![Page 6: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/6.jpg)
What WP_DEBUG does:
— Sets the error reporting level to include PHP notices
— Bypasses some error suppression
— Triggers notices for deprecated usage (more on that later)
![Page 7: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/7.jpg)
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, you'll also need to turn it off: ini_set( 'display_errors', 0 );
![Page 8: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/8.jpg)
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 );
![Page 9: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/9.jpg)
You can specify an error log:
// wp-content/debug.log define( 'WP_DEBUG_LOG', true ); // New in 3.3! (Likely.) define( 'WP_DEBUG_LOG', '/tmp/php-errors' ); // Follow ticket #18391, created last night.
![Page 10: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/10.jpg)
Debug admin CSS & JS with SCRIPT_DEBUG define( 'SCRIPT_DEBUG', true );
![Page 11: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/11.jpg)
SCRIPT_DEBUG prevents minification and concatenation of core scripts and styles. load-scripts.php?c=1&load=global,wp-admin,ms… versus global.dev.css, wp-admin.dev.css, ms.dev.css
![Page 12: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/12.jpg)
SCRIPT_DEBUG is good for:
— Finding bugs in your admin screens
— Studying the admin theme
— Contributing to core
![Page 13: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/13.jpg)
Debug queries with SAVEQUERIES define( 'SAVEQUERIES', true );
![Page 14: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/14.jpg)
SAVEQUERIES stores query data in $wpdb->queries. (OMG BBQ: This is NOT for production.)
![Page 15: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/15.jpg)
Query: SELECT * FROM wp_users WHERE user_login = 'nacin'
Backtrace: WP->init, wp_get_current_user, get_currentuserinfo, wp_validate_auth_cookie, get_user_by
Execution time: 0.4ms
![Page 16: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/16.jpg)
On using these in production:
WP_DEBUG — Don't display errors — Don't log to wp-content/error.log
SCRIPT_DEBUG — Bad idea.
SAVEQUERIES — Barry will hunt you down.
![Page 17: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/17.jpg)
Plugins
![Page 18: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/18.jpg)
Your new best friend: The Debug Bar It’s like Firebug for your WordPress.
![Page 19: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/19.jpg)
Debug Bar Console A Firebug-like console for PHP and MySQL (really)
![Page 20: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/20.jpg)
What's stopping you from building your own Debug Bar extension? Nothing.
![Page 21: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/21.jpg)
Log Deprecated Notices — Logs the usage of deprecated files, functions, and function arguments. — Pinpoints where the deprecated functionality is being used. — NEVER use in production.
![Page 22: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/22.jpg)
Theme Check — Test your theme for the latest WordPress standards and practices. — Required for the theme directory.
![Page 23: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/23.jpg)
Common functions when debugging — error_log and var_export / print_r — var_dump and sometimes die — debug_backtrace
![Page 24: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/24.jpg)
Tracking it down
![Page 25: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/25.jpg)
Step 1 Disable plugins Switch to default theme
![Page 26: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/26.jpg)
What might be left behind? Cron, Rewrites, Roles/Capabilities
![Page 27: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/27.jpg)
Drop-ins — advanced-cache.php — db.php — object-cache.php — mu-plugins
![Page 28: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/28.jpg)
What's going on? Query vars set properly? Main query SQL look right? Rewrite rule matched?
![Page 29: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/29.jpg)
Funny redirect? Confirm it's canonical.
remove_action( 'template_redirect', 'redirect_canonical' );
![Page 30: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/30.jpg)
Testing multisite?
I leave this in my config: // define( 'MULTISITE', true );
![Page 31: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/31.jpg)
Dig into the codebase. Your friends are phpxref, grep. Learn the stack. Learn what things call what.
![Page 32: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/32.jpg)
Some common traps
![Page 33: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/33.jpg)
Step 1 Is your code even running? (Spell the hook right?)
die( 'wtf' ); or error_log( )
![Page 34: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/34.jpg)
White screen on the frontend, no errors? Appearance > Themes
![Page 35: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/35.jpg)
User interface doesn't work? Check the browser's JS console for an error, usually a conflict.
![Page 36: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/36.jpg)
Internal server errors? Infinite redirects. Check: — home and siteurl — then .htaccess — then canonical
![Page 37: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/37.jpg)
Widgets got moved around? Do the sidebars have IDs? Otherwise, it's the order in which register_sidebar() is called.
![Page 38: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/38.jpg)
Lots of pages? Slow site? My next Q: What's your permalink structure? No longer an issue in 3.3!
![Page 39: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/39.jpg)
Weird Bug #1 Somewhere deep in style.css: “Template: home.php” Miscalculated as a multisite bug: activated on single site before style.css had the headers.
![Page 40: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/40.jpg)
Weird Bug #2 The admin looked weird
![Page 41: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/41.jpg)
Weird Bug #2 The admin looked weird
Trigger: Upgrade from 2.5 to 3.1
![Page 42: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/42.jpg)
Weird Bug #2 The admin looked weird
Trigger: Upgrade from 2.5 to 3.1 Problem: No MySQL ALTER permissions
![Page 43: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/43.jpg)
Weird Bug #3 Some bbPress rewrites failed after activation
![Page 44: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/44.jpg)
Weird Bug #3 Some bbPress rewrites failed after activation Problem: Custom post type rewrites aren't registered on activation
![Page 45: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/45.jpg)
// Say you have: add_action( 'generate_rewrite_rules',
function( $wp_rewrite ) { … } ); // And: register_activation_hook( __FILE__, function() { flush_rewrite_rules( ); } ); // But! This won't work for CPTs. Try this: add_action( 'init', 'my_register_post_types' ); register_activation_hook( __FILE__, function( ) { my_register_post_types( ); flush_rewrite_rules( ); } );
![Page 46: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/46.jpg)
Local Development
![Page 47: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/47.jpg)
/etc/hosts 127.0.0.1 andrewnacin.com Configure virtual hosts Install local WordPress
![Page 48: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/48.jpg)
Replace links with an output buffer for development or staging environments
![Page 49: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/49.jpg)
// 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 ); }
![Page 50: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/50.jpg)
URLs are more portable when they're absolute.
Really.* The serialized stuff is lame though.
![Page 51: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/51.jpg)
Start of a conversation Xdebug (backtraces, profiling) KCacheGrind (visualization) Using an IDE Unit testing
![Page 52: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/52.jpg)
And finally, remember: Friends don't let friends develop without WP_DEBUG.
![Page 53: WordCamp SF 2011: Debugging in WordPress](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554fb232b4c905ad218b52e9/html5/thumbnails/53.jpg)
Thanks! Questions?
@nacin