WordPress Stack Exchange 上有一个很棒的问答,许多知识渊博的人解释了他们的调试技术:你如何调试插件? https://wordpress.stackexchange.com/q/145/12615
In the JavaScript你基本上需要的竞技场<script>console.log('the value is' + variable);</script>
。并使用谷歌浏览器检查器 http://developer.chrome.com/extensions/tut_debugging.html and/or Firebug http://getfirebug.com/.
In PHP,这取决于事情发生的位置或您想要输出的位置。
在 WordPress 中调试 http://codex.wordpress.org/Debugging_in_WordPress
Codex 中的官方文档。
Example 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 );
.
FirePHP https://stackoverflow.com/search?q=firephp%20is:question
该扩展将直接在浏览器控制台中记录信息。请参阅 WordPress Answers 中的以下问答:如何使用WP-FirePHP扩展? https://wordpress.stackexchange.com/q/71569/12615.
查询监控器 https://wordpress.org/plugins/query-monitor/
这是调试工具包中必备的,该插件有很多功能,其中之一就是它Logs tab https://querymonitor.com/docs/logging-variables/,只需将其放入您的代码中并将其列在页面的插件界面中即可:
do_action( 'qm/debug', 'This happened!' );
do_action( 'qm/debug', $your_var );
do_action( 'qm/debug', [$var1, $var2] );