如何调试WordPress插件?

spi*_*ock 31 debugging wordpress

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

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

bra*_*ilo 20

在WordPress Stack Exchange上有这个优秀的问答,很多知识渊博的人都在解释他们的调试技巧:你如何调试插件?

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

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


在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 );
Run Code Online (Sandbox Code Playgroud)

将信息打印到日志文件

以下使用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 );
}
Run Code Online (Sandbox Code Playgroud)

然后,在你的代码中调用该函数 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>';
}
Run Code Online (Sandbox Code Playgroud)

不管走到哪里需要这样使用它:echo_log( $post );.


FirePHP

此扩展将直接在浏览器控制台中记录信息.请参阅WordPress上的以下问答答案:如何使用WP-FirePHP扩展?.


Grá*_*ich 5

  1. 没有调试就不开发!
  2. 请阅读此内容:http://wp.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/

祝你好运,你可以让我们更新.