WordPress 编码标准

WordPress 编码规范

💡 云策文档标注

概述

WordPress 编码规范旨在为 WordPress 开源项目及社区(涵盖核心代码、主题和插件)的协作与代码审查建立统一基线。这些规范由 WordPress 社区共同制定,是开发者和核心贡献者推荐遵循的最佳实践之一。遵循规范有助于避免常见编码错误、提升代码可读性并简化后续修改。

关键要点

  • 编码规范的目的是确保项目内文件风格统一,让任何人都能理解并修改代码,而不受代码编写时间或作者影响。
  • 计划向 WordPress 核心提交代码的开发者必须熟悉这些规范,任何提交的代码都需要符合规范要求。
  • 语言特定规范包括:CSS 编码规范、HTML 编码规范、JavaScript 编码规范、PHP 编码规范。
  • 无障碍标准:WordPress 承诺所有新增和更新的代码满足 WCAG 2.0 AA 级要求,并为常见无障碍问题提供快速指南,便于在编写补丁或功能插件时参考。
  • 不适用场景:第三方库不受这些规范约束,即使它们被集成到主项目中(例如 WordPress 核心代码库中引入的多个第三方库)。

📄 原文内容

The purpose of the WordPress Coding Standards is to create a baseline for collaboration and review within various aspects of the WordPress open source project and community, from core code to themes to plugins.

The WordPress community developed the standards contained in this section of the handbook, and those standards are part of the best practices that developers and core contributors are recommended to follow.

Why have coding standards?

Coding standards help avoid common coding errors, improve the readability of code, and simplify modification. They ensure that files within the project appear as if they were created by a single person.

Following the standards means anyone will be able to understand a section of code and modify it, if needed, without regard to when it was written or by whom.

If you are planning to contribute to WordPress core, you need to familiarize yourself with these standards, as any code you submit will need to comply with them.

Language-specific Standards

Accessibility Standards

WordPress is committed to meeting the Web Content Accessibility Guidelines (WCAG) at level AA for all new and updated code. We’ve provided a quick guide to common accessibility issues you should be aware of when creating patches or feature plug-ins.

Where do the coding standards _not_ apply?

Third-party libraries are not subject to these standards, even when integrated with the primary project. This includes instances like WordPress core, where multiple third-party libraries are incorporated into its codebase.