如何调试WordPress插件? [英] How do I debug a WordPress plugin?
问题描述
我真的只需要一种方法来创建弹出窗口或登录到控制台。
在WordPress Stack Exchange中有这么好的Q& A,许多知识渊博的人解释他们的调试技巧:如何调试插件?
在 JavaScript arena你基本上需要< script> console.log('的值是'+变量);< / script>
。并使用 Google Chrome检查器和/或 Firebug 。
在 PHP 中,这取决于发生的情况或你想要输出的地方。
在WordPress中调试
食典文件。
示例 wp-config.php
调试
//启用WP_DEBUG模式
define('WP_DEBUG',true);
//启用Debug记录到/wp-content/debug.log文件
define('WP_DEBUG_LOG',true);
//禁止显示错误和警告
define('WP_DEBUG_DISPLAY',false);
@ini_set('display_errors',0);
//使用核心JS和CSS文件的开发版本(仅在您修改这些核心文件时才需要)
define('SCRIPT_DEBUG',true);
将信息打印到日志文件
以下内容使用OSX / Unix / Linux系统路径,适用于Windows。
/ *记录到文件
*说明:登录到系统php错误日志,对于Ajax和firePHP不捕获的东西,useFull
* /
函数my_log_file($ msg,$ name = '')
{
//如果$ name为空,则打印调用函数的名称
$ 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。 \\\
;
error_log($ log,3,$ error_dir);
}
然后,在代码中调用函数 my_log_file $ post,'帖子内容是:');
Html
/ *回声变量
*说明:使用< pre> print_r以形式化的方式显示变量
* /
函数echo_log($ what)
{
echo'< pre>'。print_r($ what,true) '< / pre>';
}
无论何时需要使用它像: echo_log($帖子);
。
FirePHP
此扩展将直接在浏览器控制台中记录信息。请参阅WordPress中的以下问答答案:如何使用WP-FirePHP扩展?
I've recently inherited a WordPress plugin that has a few bugs in it. My problem is that I'm also new to WordPress and I don't know how to log debug messages so that I can figure out what's going on.
I really just need a way to create a popup or log to a console.
There's this excellent Q&A at WordPress Stack Exchange, lots of knowledgeable folks explaining their debugging techniques: How do you debug plugins?
In the Javascript arena you basically need <script>console.log('the value is' + variable);</script>
. And use Google Chrome inspector and/or Firebug.
In PHP, it depends on where things are happening or where you want the output.
Debugging in WordPress
Oficial documentation in the Codex.
Example wp-config.php
for Debugging
// 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 );
Printing information to a log file
The following uses an OSX/Unix/Linux system path, adjust for 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 );
}
Then, in you code call the function my_log_file( $post, 'The post contents are:' );
Print directly in the rendered 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>';
}
And wherever needed use it like: echo_log( $post );
.
FirePHP
This extension will log information directly in the browser console. Refer to the following Q&A at WordPress Answers: How to use WP-FirePHP extension?.
这篇关于如何调试WordPress插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!