search
HomeBackend DevelopmentPHP Tutorial讲解WordPress开发中一些常用的debug技巧_PHP

在开发过程中,调试代码是非常重要的工作,而掌握一些 WordPress 的调试技巧,可以更好的进行调试。比如,在本地开发环境,你可能需要把全部的代码警告信息全部输出出来,方便修改代码不合理的地方。

本文将讲解 WordPress 的所有调试方法,如果你是开发者,一定要掌握这些技巧,可以大大提高效率。

调试模式的开启需要在根目录的 wp-config.php 文件添加一些常量,所以下边介绍的所有代码都添加到根目录的 wp-config.php 文件。

WP_DEBUG

WP_DEBUG 是一个 WordPress 的常量,把他设为 True 之后,WordPress 会进入开发者模式,它会把所有有关开发的提示都输出到屏幕上。

我非常建议本地的开发环境里启用 WP_DEBUG,方便开发。

//启用开发者模式
define( 'WP_DEBUG', true );

WP_DEBUG_LOG

如果你想把 WordPress 运行中出现的错误全部记录下来,可以开启 WP_DEBUG_LOG,开启后,所有的错误都会被记录下来,存储到 wp-content/debug.log 文件。

记录错误的前提是需要开启 WP_DEBUG 模式,只有开启 WP_DEBUG 才会产生错误。

//开启错误记录
define( 'WP_DEBUG_LOG', true );

20151218160135277.png (955×128)

WP_DEBUG_DISPLAY

如果你想把 WP_DEBUG 模式产生的错误只通过 WP_DEBUG_LOG 记录而不显示在屏幕上,可以在开启 WP_DEBUG_LOG 和 WP_DEBUG 的时候再开启 WP_DEBUG_DISPLAY。

//阻止错误显示在屏幕上
define( 'WP_DEBUG_DISPLAY', true );
SCRIPT_DEBUG

默认情况下,WordPress 后台会使用压缩并合并后的 JS 和 CSS 文件。

有时候为了调试,我们可能不想让后台使用压缩的 CSS 和 JS 文件,这时可以把 SCRIPT_DEBUG 设置成 True。

//禁用压缩的 CSS 和 JS 文件
define( 'SCRIPT_DEBUG', true );

SAVEQUERIES

如果你要优化数据库查询次数,SAVEQUERIES 是一个非常重要的东西,把 SAVEQUERIES 设为 True,WordPress 会记录每一次数据库查询的 Sql 语句和花费的时间。

//记录数据库查询
define( 'SAVEQUERIES', true );

开启记录之后,可以使用 $wpdb 的 queries 变量来获取所有数据查询,把下边的代码放到主题的 footer.php 文件里即可看到所有数据库查询。

<pre class="brush:php;toolbar:false"><&#63;php var_dump( $GLOBALS['wpdb']->queries ); &#63;>

20151218160154013.png (516×261)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.