wordpress 调试_调试WordPress:11个强大的技巧和技术

wordpress 调试

Debugging is an essential skill for any developer. This tutorial will show you 11 powerful ways to debug WordPress and PHP errors.

调试对于任何开发人员都是必不可少的技能。 本教程将向您展示11种调试WordPress和PHP错误的强大方法。

The first item in the list is the famous WP_Debug, then we’ll jump into some more advanced methods.

列表中的第一项是著名的WP_Debug,然后我们将跳入一些更高级的方法。

Debugging WordPress

First, let’s list the types of common PHP errors:

首先,让我们列出常见PHP错误类型:

A – Notice: This is the least important error message you will see with PHP. It does not necessarily mean something is wrong, but there is possible improvement suggested.

A –注意:这是您将在PHP中看到的最不重要的错误消息。 这并不一定意味着出了什么问题,但是建议可以进行改进。

Example: null element passed to a function that expect a string.

示例:传递给期望字符串的函数的null元素。

B – Warning: It’s a more severe error, but does not cause script termination.

B –警告:这是一个更严重的错误,但不会导致脚本终止。

Example: giving include() a file that does not exist.

示例:给include()一个不存在的文件。

C – Fatal error: This is a dangerous indicator of something going really wrong, and the script terminated.

C –致命错误:这是某些错误的危险指示,脚本已终止。

Example: calling a non-existing function.

示例:调用一个不存在的函数。

1 – WP_DEBUG (1 – WP_DEBUG)

WordPress has a global constant to specify the needed level of debugging: WP_DEBUG. Along with this constant, comes two another important ones: WP_DEUBG_DISPLAY, and WP_DEBUG_LOG.

WordPress有一个全局常量来指定所需的调试级别:WP_DEBUG。 除了这个常量,还有两个重要的常量:WP_DEUBG_DISPLAY和WP_DEBUG_LOG。

WP_DEBUG is used to set the debugging mode on or off. WP_DEUBG_DISPLAY shows errors or hides them. Finally, WP_DEBUG_LOG saves your error messages in wp-content/debug.log.

WP_DEBUG用于将调试模式设置为打开或关闭。 WP_DEUBG_DISPLAY显示错误或隐藏错误。 最后,WP_DEBUG_LOG将您的错误消息保存在wp-content / debug.log中。

The three global constants can be set to true or false in wp-config.php, like this:

可以在wp-config.php中将这三个全局常量设置为true或false,如下所示:

define("WP_DEBUG", true);

define("WP_DEBUG_DISPLAY", true);

define("WP_DEBUG_LOG", true);

2 – is_wp_error() (2 – is_wp_error())

Another WordPress built-in tool for debugging is is_wp_error();. It is a method to check if a certain result is of type WP_Error. WP_Error is actually the returned object you should receive if a WordPress method fails.

另一个用于调试的WordPress内置工具是is_wp_error();。 这是一种检查特定结果是否为WP_Error类型的方法。 WP_Error实际上是在WordPress方法失败时您应该收到的返回对象。

Example:

例:

$post = array(
    'post_title'    => 'Test post',
    'post_content'  => 'This is my post.',
    'post_status'   => 'publish',
    'post_author'   => 1
);
$result = wp_insert_post( $my_post );

if(is_wp_error($result)){
    echo $return->get_error_message();
}

The above code will try adding a new post using wp_insert_post(). If this method fails, it will return WP_Error object. You can then catch and get the error message.

上面的代码将尝试使用wp_insert_post()添加新帖子。 如果此方法失败,它将返回WP_Error对象。 然后,您可以捕获并获取错误消息。

3 –调试栏 (3 – Debug Bar)

Another useful tool for debugging WordPress errors is the Debug Bar. It is a very handy tool for getting useful information about every page in your website.

调试WordPress错误的另一个有用工具是“ 调试栏” 。 这是一个非常方便的工具,可用于获取有关网站中每个页面的有用信息。

After installing it, you will find a new Debug button. When you click on it, an analysis of the queries, templates, PHP installation, and many other useful information.

安装后,您会发现一个新的“调试”按钮。 当您单击它时,将对查询,模板,PHP安装和许多其他有用信息进行分析。

4 –测试网站 (4 – Test Website)

It is of the utmost importance to separate the live website from your tests and development. I usually have two installs of WordPress on my websites. This is important because you don’t want your scripts terminating when you turn on error reporting.

将实时网站与您的测试和开发分开是至关重要的。 通常,我的网站上有两套WordPress。 这很重要,因为在打开错误报告时,您不希望脚本终止。

5 –简单地显示钩子 (5 – Simply Show Hooks)

Simply Show Hooks is a nice plugin for showing every hook that is running on any page. If you encounter a situation where all error reporting ideas are not working, and you will, then comes the time for fetching out every and all running hooks.

Simply Show Hooks是一个不错的插件,用于显示任何页面上正在运行的每个钩子。 如果遇到无法报告所有错误报告的想法的情况,那么您就该抽出所有正在运行的钩子了。

This plugin will tell you what action or filter hooks that run on any page. You can then start analyzing and debugging each hook code. You can also see the attached methods to every hook. And even find out the priority of each.

该插件将告诉您在任何页面上运行的操作或筛选器挂钩。 然后,您可以开始分析和调试每个挂钩代码。 您还可以看到每个钩子附带的方法。 甚至找出每个优先级。

6 – WPDB错误报告 (6 – WPDB Error Reporting)

If you use the wpdb class for dealing with your database, then you will always need error reporting. Whether it is for making sure that your queries run correctly, or for showing up error messages for debugging.

如果使用wpdb类处理数据库,则将始终需要错误报告。 是为了确保查询正确运行,还是为了显示调试错误消息。

Example:

例:

global $wpdb;
// Before running your query:
$wpdb->show_errors = TRUE;
$result = $wpdb->get_results("SELECT field_value FROM table_name");
if(! $result){
    $wpdb->print_error();
    // Or you can choose to show the last tried query.
    echo $wpdb->last_query;
}

7 –服务器错误日志 (7 – Server Error Logs)

At some points, neither WordPress or PHP will be able to catch some coding errors. For example, if your script exceeded the maximum allowed run time, you won’t get a PHP error message. Rather, Apache (or your server installed system) will pop up something like ‘Internal Server Error.’

在某些时候,WordPress或PHP都无法捕获一些编码错误。 例如,如果您的脚本超出了允许的最大运行时间,则不会收到PHP错误消息。 相反,Apache(或服务器安装的系统)将弹出类似“内部服务器错误”的信息。

This is the time when you should go to your error log, and see what if your PHP code, or a certain part of your WordPress installation did something wrong.

这是您应该进入错误日志并查看您PHP代码或WordPress安装的某些部分出了什么问题的时候。

You can consult your web hosting provided on where the logging is stored. Usually it is something under logs folder.

您可以查询有关日志记录存储位置的Web托管。 通常它是在日志文件夹下的东西。

8 – PHP错误记录 (8 – PHP Error Logging)

PHP has its own level of error reporting to store the issues resolving beyond WordPress. This is extremely useful, especially if something outside of WordPress running and causing some problem.

PHP具有自己的错误报告级别,以存储已解决的问题(超出WordPress)。 这是非常有用的,尤其是在WordPress之外的某些程序正在运行并引起某些问题时。

You can start by configuring your php.ini file to turn on error reporting, and then choosing where to store such messages.

您可以首先配置php.ini文件以打开错误报告,然后选择将此类消息存储在何处。

error_reporting = E_ALL | E_STRICT 
error_log = /var/log/php_error.log

Setting the above two lines will turn on error reporting, and set the errors to be saved at the specified path.

设置以上两行将打开错误报告,并设置要保存在指定路径的错误。

Also, you can run phpinfo(); and check the error_log option.

另外,您可以运行phpinfo();。 并检查error_log选项。

9 – PHP语法检查器 (9 – PHP Syntax Checker)

If your hosting provider limits access to php.ini file, or you can’t access error logs, things can be a bit harder. But, there are many tools to overcome a situation when you get only a blank page with no error message. One quick tool is PHP Code Checker.

如果您的托管服务提供商限制了对php.ini文件的访问,或者您无法访问错误日志,那么事情可能会有些困难。 但是,有许多工具可以克服这种情况,当您只得到空白页而没有错误消息时。 一种快速的工具是PHP Code Checker

PHP Code Checker is a syntax errors checking tool. It is really useful if you missed a semi-colon or a curly brace, and can’t find where did you miss it.

PHP Code Checker是一种语法错误检查工具。 如果您错过了分号或花括号,却找不到在哪里错过了,这真的很有用。

10 – PHP IDE (10 – PHP IDE)

If PHP Code Checker didn’t find a syntax error, then you will need a more powerful tool. A powerful IDE like PhpStorm will be the answer for more advanced debugging, and breaking down your code into parts.

如果PHP Code Checker找不到语法错误,那么您将需要一个功能更强大的工具。 像PhpStorm这样功能强大的IDE将为更高级的调试以及将您的代码分解成多个部分的答案。

In a situation like, when you store a string in a variable, and try printing it, but nothing happens. Using an IDE can find out you are doing something wrong like, overriding this variable later in your code. This is why it is highly recommended to rely on a powerful IDE like PhpStorm, Eclipse, or any tool you prefer.

在类似的情况下,当您将字符串存储在变量中并尝试打印时,什么也没有发生。 使用IDE可以发现您做错了什么,例如稍后在代码中覆盖此变量。 这就是为什么强烈建议依赖功能强大的IDE(如PhpStorm,Eclipse或您喜欢的任何工具)的原因。

11 –禁用浏览器缓存 (11 – Disabling Browser Cache)

If you are working on the front-end side of your website, then you need to disable browser cache. This is necessary because if you are working on your JavaScript code, and it’s not updating, or even worse, updating but not showing new errors, then you need to disable cache.

如果您在网站的前端工作,则需要禁用浏览器缓存。 这是必要的,因为如果您正在使用JavaScript代码,并且没有更新,或者更糟的是更新但没有显示新错误,则需要禁用缓存。

Disabling the cache means you ask your browser to stop working on old stored files of your website. This includes CSS and JavaScript files.

禁用缓存意味着您要求浏览器停止处理网站的旧存储文件。 这包括CSS和JavaScript文件。

To disable Google Chrome browser cache, open the DevTools by right-clicking of any web element. Then, click on Network tab in toolbar. Finally, check the Disable Cache checkbox at the top.

要禁用Google Chrome浏览器缓存,请右键单击任何Web元素以打开DevTools。 然后,单击工具栏中的“网络”选项卡。 最后,选中顶部的“禁用缓存”复选框。

结论 (Conclusion)

The above 11 tips can be your guide for the debugging process. Although debugging can be tedious at many times, but the above tricks can make the whole process much easier.

以上11个技巧可以作为调试过程的指南。 尽管调试在很多时候可能很乏味,但是上述技巧可以使整个过程变得更加容易。

Let me know if you have any WordPress debugging tips that you’d like to share, please do so in the comments below!

如果您有任何要共享的WordPress调试提示,请告诉我,请在下面的评论中进行!

翻译自: https://www.sitepoint.com/debugging-wordpress/

wordpress 调试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值