如何调试WordPress插件?

时间:2013-01-26 21:57:39

标签: wordpress debugging

我最近继承了一个WordPress插件,里面有一些bug。我的问题是我也是WordPress的新手,我不知道如何记录调试消息,以便我可以弄清楚发生了什么。

我真的只需要一种方法来创建弹出窗口或登录到控制台。

6 个答案:

答案 0 :(得分:18)

在WordPress Stack Exchange上有这个优秀的Q& A,很多知识渊博的人都在解释他们的调试技巧:How do you debug plugins?

Javascript 领域,您基本上需要<script>console.log('the value is' + variable);</script>。并使用Google Chrome inspector和/或Firebug

PHP 中,它取决于事情的发生位置或输出的位置。


Debugging in WordPress

食典委的官方文件。

用于调试的示例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

此扩展程序将直接在浏览器控制台中记录信息。请参阅WordPress答案中的以下问答:How to use WP-FirePHP extension?

答案 1 :(得分:5)

  1. 没有调试就不能开发!
  2. 请阅读此内容:http://wp.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
  3. 祝您好运,您可以随时向我们提供最新信息。

答案 2 :(得分:2)

一般的PHP调试策略是使用print_r( $var )语句并刷新页面。简单易行。如果您想要进入代码,Xdebug就是您要安装的代码。

答案 3 :(得分:0)

根据您Much rather use a system where debug messages can be turned off and on in one place所在的your comment

可以在WordPress中完成。有一个名为WP_DEBUG的常量,您可以从WordPress文件夹中的true文件设置为falsewp-config.php(您添加数据库参数的文件) )。

所以,你可以使用:

if( WP_DEBUG ){
echo "<script> alert('Hello World!'); </script>"; 
}

仅当您将WP_DEBUG设置为true时(例如,在本地主机上的网站开发版本上),警报才会显示,而它不会显示在您的网站制作版本上(您只是必须将WP_DEBUG设置为false。

答案 4 :(得分:-1)

通过使用Visual Studio和开发工具php调试器扩展,您可以逐步调试它,而不是记录它们并尝试在运行时计算出变量值。也可能有其他替代方案。

PS:它不是免费的:(

答案 5 :(得分:-2)

我知道我的答案是在原始问题发布几年之后但是,由于我最近遇到了同样的问题并且仍然无法找到令人满意的解决方案,因此我编写了一个插件。

它专门解决OP需求:

  

我真的只需要一种方法来创建弹出窗口或登录到控制台。

希望它可以帮助所有插件/主题开发人员,他们正在寻找一种快速简便的方法来调试他们的代码https://wordpress.org/plugins/bugfu-console-debugger

只需安装它并从PHP代码中调用日志记录方法即可直接登录浏览器JavaScript控制台。

BugFu::log($your_string-array-object);
相关问题