My popular talk on Debugging WordPress, presented at WordCamp London, WordCamp Norrkoping, Software University and WPBGUG
Video: http://wordpress.tv/2014/05/23/mario-peshev-debugging-wordpress/
2. Mario Peshev
• @no_fear_inc
• WordPress Ambassador at
• WordPress Engineer and
Consultant
• Open Source Addict
• Cofficer
3. What is Debugging?
According to Wikipedia:
“Debugging is a methodical process of
finding and reducing the number of bugs, or
defects, in a computer program or a piece of
electronic hardware, thus making it behave as
expected.”
http://en.wikipedia.org/wiki/Debugging
4. Why Debug?
Friends don’t let friends work in a messed-up
environment.
There are enough tools doing the heavy lifting.
9. PHP logging
Set some error reporting rules:
ini_set('log_errors',TRUE);
ini_set('error_reporting',E_ALL);
ini_set('error_log', '/tmp/wp_error_log.txt');
Get frontend feedback on local sites:
ini_set('display_errors', TRUE);
10. Xdebug
“Xdebug is a PHP extension which provides
debugging and profiling capabilities.”
Official Xdebug Website
18. Debugging AJAX (2)
WordPress has built-in AJAX helpers
• Such as wp_send_json
• The wp JS object
Use these hooks for your AJAX callbacks:
wp_ajax_{$someaction}
wp_ajax_nopriv_{$someaction}
Mind your JavaScript
25. WP_DEBUG
Global switch for debugging capabilities
define( 'WP_DEBUG', true );
Store log messages in debug.log file in wp-content/
define( 'WP_DEBUG_LOG', true );
Display log messages on the site
define( 'WP_DEBUG_DISPLAY', true );
26. SCRIPT_DEBUG
“SCRIPT_DEBUG is a related constant that
will force WordPress to use the “dev”
versions of core CSS and Javascript files
rather than the minified versions that are
normally loaded. This is useful when you are
testing modifications to any built-in .js or .css
files. Default is false.”
define( 'SCRIPT_DEBUG', true );
27. SAVEQUERIES
“The SAVEQUERIES definition saves the database queries to an
array and that array can be displayed to help analyze those
queries. The information saves each query, what function called
it, and how long that query took to execute.”
define( 'SAVEQUERIES', true );
You can list $wpdb errors from a global array:
global $EZSQL_ERROR; // array with errors
Also $wpdb->show_errors()
30. The ‘all’ hook
add_action( 'all', 'hacky_all_hooks' );
function hacky_all_hooks() {
var_dump( current_filter() );
}
Outputs all hooks
You can disable hooks or add functions with
configurable priority
40. WP-CLI
As per the WP-CLI website:
“WP-CLI is a set of command-line tools for
managing WordPress installations. You can update
plugins, set up multisite installs and much more, without
using a web browser.”
Testing, debugging and automation are much easier
42. Host resolution
Mapping domain names to local websites
Add a VirtualHost with Apache
Map the hostname at the hosts file accordingly
127.0.0.1 wp2.me
Once ready, deploy to server
http://en.wikipedia.org/wiki/Hosts_(file)