在 WordPress Stack Exchange 上有精彩的问答,很多知识渊博的人解释了他们的调试技术:How do you debug plugins?
在 Javascript 领域,您基本上需要<script>console.log('the value is' + variable);</script>。并使用Google Chrome inspector 和/或Firebug。
在 PHP 中,这取决于事情发生的位置或您想要输出的位置。
Codex 中的官方文档。
用于调试的示例wp-config.php
// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );
// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );
// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );
将信息打印到日志文件
以下使用OSX/Unix/Linux系统路径,针对Windows进行调整。
/* Log to File
* Description: Log into system php error log, usefull for Ajax and stuff that FirePHP doesn't catch
*/
function my_log_file( $msg, $name = '' )
{
// Print the name of the calling function if $name is left empty
$trace=debug_backtrace();
$name = ( '' == $name ) ? $trace[1]['function'] : $name;
$error_dir = '/Applications/MAMP/logs/php_error.log';
$msg = print_r( $msg, true );
$log = $name . " | " . $msg . "\n";
error_log( $log, 3, $error_dir );
}
然后,在你的代码中调用函数my_log_file( $post, 'The post contents are:' );
直接在渲染的Html中打印
/* Echo variable
* Description: Uses <pre> and print_r to display a variable in formated fashion
*/
function echo_log( $what )
{
echo '<pre>'.print_r( $what, true ).'</pre>';
}
在需要的地方使用它,例如:echo_log( $post );。
此扩展程序将直接在浏览器控制台中记录信息。请参阅 WordPress Answers 中的以下问答:How to use WP-FirePHP extension?。