Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active October 14, 2022 10:08
Show Gist options
  • Save damiencarbery/87276e72aaad82accd226a917bda958a to your computer and use it in GitHub Desktop.
Save damiencarbery/87276e72aaad82accd226a917bda958a to your computer and use it in GitHub Desktop.
Debugging with WordPress - enable WP_DEBUG, with sample usage to investigate wc_get_template()
<?php
// Only call error_log() when in debug mode.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log('WP_DEBUG is enabled');
}
<?php
/*
Plugin Name: Debug Demo
Plugin URI: http://www.damiencarbery.com/2017/01/debugging-with-wordpress-and-being-secure/
Description: Demonstrate error_log().
Author: Damien Carbery
Version: 0.1
*/
add_action('wp_head', 'dc_debug_demo');
function dc_debug_demo() {
global $post;
// Add this to html as a comment.
echo '<!-- Post type: ' . $post->post_type . '; Post name: ' . $post->post_name . ' -->';
// Write to the wp-content/debug.log file.
error_log('Post type: ' . $post->post_type . '; Post name: ' . $post->post_name);
}
[20-Jan-2017 13:35:57 UTC] wc_get_template: located:/var/www/web/wp-content/plugins/woocommerce/templates/notices/notice.php; template_name:notices/notice.php; template_path:; default_path:; args:array (
'messages' =>
array (
0 => 'Have a coupon? <a href="#" class="showcoupon">Click here to enter your code</a>',
),
)
[20-Jan-2017 12:56:53 UTC] Post type: post; Post name: hello-world
[20-Jan-2017 12:56:56 UTC] Post type: page; Post name: sample-page
<Files debug.log>
order deny,allow
deny from all
allow from 12.34.56.78
</Files>
<?php
/*
Plugin Name: Log Template Searches in WooCommerce
Plugin URI: http://www.damiencarbery.com
Description: Log calls to the WooCommerce template functions to help determine how to intercept the calls.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'wc_get_template', 'lts_get_template', 20, 5 );
function lts_get_template( $located, $template_name, $args, $template_path, $default_path ) {
error_log( sprintf('wc_get_template: located:%s; template_name:%s; template_path:%s; default_path:%s; args:%s', $located, $template_name, $template_path, $default_path, var_export( $args, true ) ) );
return $located;
}
<?php
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
if ($_SERVER['REMOTE_ADDR'] == '12.34.56.78') { // Only enable for the specified IP address
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true); // error_log() will write to wp-content/debug.log
//define('WP_DEBUG_LOG', '/maybe/path/outside/htdocs/debug.log'); // You can specify a path for the log file.
define('WP_DEBUG_DISPLAY', false); // Write notices/warnings/errors to debug.log instead of the html
@ini_set('display_errors', 0);
} else {
define('WP_DEBUG', false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment