如何调试 WordPress 插件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14541989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:09:18  来源:igfitidea点击:

How do I debug a WordPress plugin?

wordpressdebugging

提问by spinlock

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.

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

I really just need a way to create a popup or log to a console.

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

采纳答案by brasofilo

There's this excellent Q&A at WordPress Stack Exchange, lots of knowledgeable folks explaining their debugging techniques: How do you debug plugins?

在 WordPress Stack Exchange 上有一个很好的问答,很多知识渊博的人解释了他们的调试技术:你如何调试插件?

In the Javascriptarena you basically need <script>console.log('the value is' + variable);</script>. And use Google Chrome inspectorand/or Firebug.

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

In PHP, it depends on where things are happening or where you want the output.

PHP 中,这取决于事情发生的位置或您想要输出的位置。



Debugging in WordPress

在 WordPress 中调试

Oficial documentation in the Codex.

Codex 中的官方文件。

Example wp-config.phpfor Debugging

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 );


Printing information to a log file

将信息打印到日志文件

The following uses an OSX/Unix/Linux system path, adjust for Windows.

以下使用 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 );
}

Then, in you code call the function my_log_file( $post, 'The post contents are:' );

然后,在你的代码中调用函数 my_log_file( $post, 'The post contents are:' );



Print directly in the rendered Html

直接在渲染的 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 );.

在需要的地方使用它,例如:echo_log( $post );



FirePHP

火PHP

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 Answers 中的以下问答:如何使用 WP-FirePHP 扩展?.

回答by Grávuj Miklós Henrich

  1. Don't develop without debugging!
  2. Read this please: http://wp.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
  1. 不调试就不要开发!
  2. 请阅读:http: //wp.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/

Good luck and you may keep us updated.

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

回答by Chris DaMour

The general PHP debugging strategy is to use print_r( $var )statements and refresh the page. Simple and easy. If you want to step into the code Xdebugis what you want to install.

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

回答by RRikesh

According to your commentwhere you Much rather use a system where debug messages can be turned off and on in one place:

根据您的评论,您Much rather use a system where debug messages can be turned off and on in one place

It can be done in WordPress. There is a constant called WP_DEBUGthat you can set to trueor falsefrom your wp-config.phpfile in your WordPress folder (that file where you add database parameters ).

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

So, you can use:

因此,您可以使用:

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

The alert will show only when you have WP_DEBUG set to true(for example on a development version of the website like on your localhost) while it won't show on your production version of the website(You just have to set WP_DEBUGto false there).

警报只会在您将 WP_DEBUG 设置为true(例如在您的本地主机上的网站开发版本上)时显示,而不会在您的网站生产版本上显示(您只需在那里设置WP_DEBUG为 false)。

回答by curiousBoy

By using Visual Studio and a dev tools php debugger extension, you can actually debug it step by step rather than logging them and trying to figure out the variable values during the run time. There might be other alternatives as well.

通过使用 Visual Studio 和开发工具 php 调试器扩展,您实际上可以逐步调试它,而不是记录它们并尝试在运行时找出变量值。可能还有其他选择。

PS: It is not free :(

PS:它不是免费的:(

回答by fedeandri

I'm aware that my answer comes several years after the original question but, since I recently had the same exact problem and still couldn't find a satisfying solution, I coded a plugin for that.

我知道我的答案是在最初的问题几年后提出的,但是由于我最近遇到了完全相同的问题并且仍然找不到令人满意的解决方案,因此我为此编写了一个插件。

It specifically addresses the OP need:

它专门解决了 OP 需求:

I really just need a way to create a popup or log to a console.

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

Hope it helps all the plugin/theme developers out there, who are looking for a quick and easy way to debug their code https://wordpress.org/plugins/bugfu-console-debugger

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

Just install it and call the logging method from your PHP code to log straight to the browser JavaScript console.

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

BugFu::log($your_string-array-object);