wp-guidelines

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

WordPress Coding Standards & Conventions

WordPress编码标准与规范

Quick-reference for WordPress PHP development. Rules are distilled from the official WordPress Coding Standards (WPCS) sniffs and WordPress core documentation.

WordPress PHP开发速查指南。规则提炼自官方WordPress编码标准(WPCS)检测规则及WordPress核心文档。

1. Naming Conventions

1. 命名规范

All names use
snake_case
for functions, variables, and properties. Classes use
Pascal_Case
with underscores (
My_Plugin_Admin
). Hook names are all-lowercase with underscores, prefixed with your plugin slug.
ElementConventionExample
Functions
snake_case
acme_get_settings()
Variables
snake_case
$post_title
Classes
Pascal_Case
(underscored)
Acme_Plugin_Admin
Constants
UPPER_SNAKE_CASE
ACME_VERSION
Files
lowercase-hyphens
class-acme-admin.php
Hook names
lowercase_underscores
acme_after_init
Post type slugs
lowercase
,
a-z0-9_-
, max 20 chars
acme_book
File naming rules: All lowercase, hyphens as separators,
class-
prefix for single-class files.
Global prefix rule: ALL plugin/theme globals (functions, classes, constants, hooks, namespaces) must start with a unique prefix (min 4 chars). Blocked prefixes:
wordpress
,
wp
,
_
,
php
.
Post type slugs: Max 20 chars, no reserved names (
post
,
page
,
attachment
, etc.), no
wp_
prefix.
<!-- Detailed code examples: resources/naming-best-practices.md -->
函数、变量和属性全部使用
snake_case
命名法。类使用带下划线的
Pascal_Case
命名法(如
My_Plugin_Admin
)。钩子名称使用全小写加下划线,前缀为你的插件别名。
元素规范示例
函数
snake_case
acme_get_settings()
变量
snake_case
$post_title
带下划线的
Pascal_Case
Acme_Plugin_Admin
常量
UPPER_SNAKE_CASE
ACME_VERSION
文件全小写连字符分隔
class-acme-admin.php
钩子名称全小写加下划线
acme_after_init
自定义文章类型别名全小写,仅含
a-z0-9_-
,最多20字符
acme_book
文件命名规则: 全部使用小写,用连字符分隔,单类文件需加
class-
前缀。
全局前缀规则: 所有插件/主题的全局内容(函数、类、常量、钩子、命名空间)必须以唯一前缀开头(至少4个字符)。禁止使用的前缀:
wordpress
wp
_
php
自定义文章类型别名: 最多20字符,不能使用保留名称(
post
page
attachment
等),不能加
wp_
前缀。
<!-- 详细代码示例:resources/naming-best-practices.md -->

2. PHP Best Practices (WordPress-Specific)

2. WordPress专属PHP最佳实践

Yoda conditions: Place literals on the LEFT side of
==
,
!=
,
===
,
!==
comparisons.
Strict comparisons: Always pass
true
as third arg to
in_array()
,
array_search()
,
array_keys()
.
Forbidden functions:
FunctionWhyAlternative
extract()
Pollutes local scope unpredictablyDestructure manually
@
(error suppression)
Hides errorsCheck return values; use
wp_safe_*
ini_set()
Breaks interoperabilityUse WP constants (
WP_DEBUG
, etc.)
Development-only functions (remove before shipping):
var_dump
,
var_export
,
print_r
,
error_log
,
trigger_error
,
phpinfo
, and related debug functions.
Use WordPress functions over PHP natives:
PHP NativeWordPress Alternative
json_encode()
wp_json_encode()
parse_url()
wp_parse_url()
curl_*()
wp_remote_get()
/
wp_remote_post()
file_get_contents()
(remote)
wp_remote_get()
unlink()
wp_delete_file()
strip_tags()
wp_strip_all_tags()
rand()
/
mt_rand()
wp_rand()
file_put_contents()
,
fopen()
WP_Filesystem
methods
Note:
file_get_contents()
for local files (using
ABSPATH
,
WP_CONTENT_DIR
,
plugin_dir_path()
) is acceptable.
Type casts: Use short forms:
(int)
,
(float)
,
(bool)
,
(string)
. Never
(integer)
,
(real)
, or
(unset)
.
<!-- Detailed code examples: resources/naming-best-practices.md -->
Yoda条件:
==
!=
===
!==
比较中,将字面量放在左侧。
严格比较: 调用
in_array()
array_search()
array_keys()
时,第三个参数必须传入
true
禁用函数:
函数原因替代方案
extract()
不可预测地污染局部作用域手动解构
@
(错误抑制符)
隐藏错误检查返回值;使用
wp_safe_*
系列函数
ini_set()
破坏互操作性使用WP常量(如
WP_DEBUG
等)
仅开发环境可用函数(发布前需移除):
var_dump
var_export
print_r
error_log
trigger_error
phpinfo
及相关调试函数。
优先使用WordPress函数而非PHP原生函数:
PHP原生函数WordPress替代函数
json_encode()
wp_json_encode()
parse_url()
wp_parse_url()
curl_*()
wp_remote_get()
/
wp_remote_post()
file_get_contents()
(远程文件)
wp_remote_get()
unlink()
wp_delete_file()
strip_tags()
wp_strip_all_tags()
rand()
/
mt_rand()
wp_rand()
file_put_contents()
fopen()
WP_Filesystem
方法
注意:使用
ABSPATH
WP_CONTENT_DIR
plugin_dir_path()
读取本地文件时,
file_get_contents()
是可接受的。
类型转换: 使用短格式:
(int)
(float)
(bool)
(string)
。禁止使用
(integer)
(real)
(unset)
<!-- 详细代码示例:resources/naming-best-practices.md -->

3. Hooks & Actions

3. 钩子与动作

3.1 Registration Patterns

3.1 注册模式

php
// Actions: side effects (send email, save data, enqueue assets)
add_action( 'init', 'acme_register_post_types' );
add_action( 'wp_enqueue_scripts', 'acme_enqueue_assets' );
add_action( 'admin_menu', 'acme_add_admin_page' );

// Filters: modify and return a value
add_filter( 'the_content', 'acme_append_cta' );
add_filter( 'excerpt_length', 'acme_custom_excerpt_length' );
php
// 动作:处理副作用(发送邮件、保存数据、入队资源)
add_action( 'init', 'acme_register_post_types' );
add_action( 'wp_enqueue_scripts', 'acme_enqueue_assets' );
add_action( 'admin_menu', 'acme_add_admin_page' );

// 过滤器:修改并返回值
add_filter( 'the_content', 'acme_append_cta' );
add_filter( 'excerpt_length', 'acme_custom_excerpt_length' );

3.2 Priority and Argument Count

3.2 优先级与参数数量

php
// Default priority is 10, default accepted args is 1
add_filter( 'the_title', 'acme_modify_title', 10, 2 );

function acme_modify_title( $title, $post_id ) {
    // Always declare the correct number of parameters
    return $title;
}
php
// 默认优先级为10,默认接受参数数量为1
add_filter( 'the_title', 'acme_modify_title', 10, 2 );

function acme_modify_title( $title, $post_id ) {
    // 必须声明正确数量的参数
    return $title;
}

3.3 Removing Hooks

3.3 移除钩子

Must match the exact callback, priority, and (for objects) the same instance:
php
// Remove a function callback
remove_action( 'wp_head', 'wp_generator' );

// Remove with matching priority
remove_filter( 'the_content', 'acme_append_cta', 10 );

// Remove an object method (must be same instance)
remove_action( 'init', array( $instance, 'init' ) );
必须匹配完全相同的回调函数、优先级,以及(针对对象)同一实例:
php
// 移除函数回调
remove_action( 'wp_head', 'wp_generator' );

// 匹配优先级移除
remove_filter( 'the_content', 'acme_append_cta', 10 );

// 移除对象方法(必须是同一实例)
remove_action( 'init', array( $instance, 'init' ) );

3.4 Hook Naming Conventions

3.4 钩子命名规范

php
// Plugin hooks should be prefixed and use underscores
do_action( 'acme_before_render', $context );
$value = apply_filters( 'acme_output_format', $default, $post );

// Dynamic hooks: prefix the static part
do_action( "acme_process_{$type}", $data );

php
// 插件钩子需加前缀并使用下划线
do_action( 'acme_before_render', $context );
$value = apply_filters( 'acme_output_format', $default, $post );

// 动态钩子:前缀为静态部分
do_action( "acme_process_{$type}", $data );

4. Internationalization (i18n)

4. 国际化(i18n)

4.1 Core Functions

4.1 核心函数

FunctionPurpose
__( $text, $domain )
Return translated string
_e( $text, $domain )
Echo translated string
_x( $text, $context, $domain )
Return with disambiguation context
_ex( $text, $context, $domain )
Echo with disambiguation context
_n( $single, $plural, $number, $domain )
Pluralization
_nx( $single, $plural, $number, $context, $domain )
Plural + context
esc_html__( $text, $domain )
Return translated + HTML-escaped
esc_html_e( $text, $domain )
Echo translated + HTML-escaped
esc_attr__( $text, $domain )
Return translated + attribute-escaped
esc_attr_e( $text, $domain )
Echo translated + attribute-escaped
esc_html_x( $text, $context, $domain )
Return translated + escaped + context
esc_attr_x( $text, $context, $domain )
Return translated + escaped + context
函数用途
__( $text, $domain )
返回翻译后的字符串
_e( $text, $domain )
输出翻译后的字符串
_x( $text, $context, $domain )
返回带消歧上下文的翻译字符串
_ex( $text, $context, $domain )
输出带消歧上下文的翻译字符串
_n( $single, $plural, $number, $domain )
复数处理
_nx( $single, $plural, $number, $context, $domain )
复数处理+上下文
esc_html__( $text, $domain )
返回翻译并经过HTML转义的字符串
esc_html_e( $text, $domain )
输出翻译并经过HTML转义的字符串
esc_attr__( $text, $domain )
返回翻译并经过属性转义的字符串
esc_attr_e( $text, $domain )
输出翻译并经过属性转义的字符串
esc_html_x( $text, $context, $domain )
返回翻译、转义并带上下文的字符串
esc_attr_x( $text, $context, $domain )
返回翻译、转义并带上下文的字符串

4.2 Rules

4.2 规则

  • Text domain must match your plugin/theme slug exactly.
  • All user-facing strings must be wrapped in a translation function.
  • Prefer combined escape+translate over separate calls:
php
// BAD - separate escape and translate
echo esc_html( __( 'Hello', 'acme-plugin' ) );

// GOOD - combined function
echo esc_html__( 'Hello', 'acme-plugin' );
If you pass two parameters to
esc_html()
or
esc_attr()
, you probably meant
esc_html__()
/
esc_attr__()
.
  • 文本域必须与你的插件/主题别名完全一致。
  • 所有面向用户的字符串必须包裹在翻译函数中。
  • 优先使用转义+翻译的组合函数,而非分开调用:
php
// 错误示例:分开转义和翻译
echo esc_html( __( 'Hello', 'acme-plugin' ) );

// 正确示例:使用组合函数
echo esc_html__( 'Hello', 'acme-plugin' );
如果向
esc_html()
esc_attr()
传递两个参数,你可能实际需要的是
esc_html__()
/
esc_attr__()

4.3 Translator Comments

4.3 译者注释

Add translator comments for ambiguous strings, sprintf placeholders, or context:
php
// BAD
printf( __( '%s: %s', 'acme-plugin' ), $label, $value );

// GOOD
/* translators: 1: field label, 2: field value */
printf( __( '%1$s: %2$s', 'acme-plugin' ), $label, $value );
对于歧义字符串、sprintf占位符或需要上下文的内容,添加译者注释:
php
// 错误示例
printf( __( '%s: %s', 'acme-plugin' ), $label, $value );

// 正确示例
/* translators: 1: 字段标签, 2: 字段值 */
printf( __( '%1$s: %2$s', 'acme-plugin' ), $label, $value );

4.4 sprintf Placeholder Rules

4.4 sprintf占位符规则

  • With 2+ placeholders, use ordered placeholders:
    %1$s
    ,
    %2$s
    (not
    %s
    ,
    %s
    ).
  • Do NOT use
    %1$s
    if there is only one placeholder (use
    %s
    ).
  • The number of placeholders must match the number of arguments.

  • 当有2个及以上占位符时,使用有序占位符:
    %1$s
    %2$s
    (而非
    %s
    %s
    )。
  • 若只有一个占位符,禁止使用
    %1$s
    (使用
    %s
    即可)。
  • 占位符数量必须与参数数量匹配。

5. Enqueuing Assets

5. 资源入队

5.1 Never Use Direct Tags

5.1 禁止直接输出标签

php
// BAD - direct output
echo '<script src="my-script.js"></script>';
echo '<link rel="stylesheet" href="my-style.css">';

// GOOD - proper enqueue
function acme_enqueue_assets() {
    wp_enqueue_script(
        'acme-main',
        plugin_dir_url( __FILE__ ) . 'js/main.js',
        array( 'jquery' ),
        '1.2.0',
        true  // in_footer
    );
    wp_enqueue_style(
        'acme-styles',
        plugin_dir_url( __FILE__ ) . 'css/styles.css',
        array(),
        '1.2.0'
    );
}
add_action( 'wp_enqueue_scripts', 'acme_enqueue_assets' );
php
// 错误示例:直接输出
echo '<script src="my-script.js"></script>';
echo '<link rel="stylesheet" href="my-style.css">';

// 正确示例:使用标准入队方式
function acme_enqueue_assets() {
    wp_enqueue_script(
        'acme-main',
        plugin_dir_url( __FILE__ ) . 'js/main.js',
        array( 'jquery' ),
        '1.2.0',
        true  // 在页脚加载
    );
    wp_enqueue_style(
        'acme-styles',
        plugin_dir_url( __FILE__ ) . 'css/styles.css',
        array(),
        '1.2.0'
    );
}
add_action( 'wp_enqueue_scripts', 'acme_enqueue_assets' );

5.2 Required Parameters

5.2 必填参数

ParameterRequired?Notes
$handle
YesUnique identifier
$src
ConditionalOmit only when registering dependency-only handle
$deps
RecommendedArray of dependency handles
$ver
Yes (when src given)Must be non-false; use explicit version string.
false
= WP core version (bad for cache busting).
null
= no version query string (also discouraged).
$in_footer
(scripts)
YesExplicitly set
true
(footer) or
false
(header). Defaults to header if omitted.
$media
(styles)
OptionalDefault
'all'
php
// BAD - missing version, missing in_footer
wp_enqueue_script( 'acme-main', $url );

// GOOD
wp_enqueue_script( 'acme-main', $url, array(), '1.0.0', true );
参数是否必填说明
$handle
唯一标识符
$src
可选仅当注册仅作为依赖的句柄时可省略
$deps
推荐依赖句柄数组
$ver
是(当提供$src时)必须为非false值;使用明确的版本字符串。
false
表示使用WP核心版本(不利于缓存刷新)。
null
表示不添加版本查询字符串(同样不推荐)。
$in_footer
(脚本)
显式设置
true
(页脚)或
false
(页眉)。省略时默认在页眉加载。
$media
(样式)
可选默认值为
'all'
php
// 错误示例:缺少版本和in_footer参数
wp_enqueue_script( 'acme-main', $url );

// 正确示例
wp_enqueue_script( 'acme-main', $url, array(), '1.0.0', true );

5.3 Conditional Loading

5.3 条件加载

Load assets only where needed:
php
function acme_admin_assets( $hook ) {
    if ( 'toplevel_page_acme-settings' !== $hook ) {
        return;
    }
    wp_enqueue_style( 'acme-admin', ... );
}
add_action( 'admin_enqueue_scripts', 'acme_admin_assets' );

function acme_frontend_assets() {
    if ( ! is_singular( 'acme_portfolio' ) ) {
        return;
    }
    wp_enqueue_script( 'acme-portfolio', ... );
}
add_action( 'wp_enqueue_scripts', 'acme_frontend_assets' );

仅在需要的场景加载资源:
php
function acme_admin_assets( $hook ) {
    if ( 'toplevel_page_acme-settings' !== $hook ) {
        return;
    }
    wp_enqueue_style( 'acme-admin', ... );
}
add_action( 'admin_enqueue_scripts', 'acme_admin_assets' );

function acme_frontend_assets() {
    if ( ! is_singular( 'acme_portfolio' ) ) {
        return;
    }
    wp_enqueue_script( 'acme-portfolio', ... );
}
add_action( 'wp_enqueue_scripts', 'acme_frontend_assets' );

6. WordPress API Usage

6. WordPress API使用规则

Key rules for WordPress API calls. Use capabilities (not roles) in
current_user_can()
. Cron intervals must be 15+ minutes. Limit
posts_per_page
(no
-1
). Never overwrite WP globals (
$post
,
$wp_query
). Always pass
$single
to
get_post_meta()
. Avoid
current_time( 'timestamp' )
-- use
time()
for UTC or
current_time( 'mysql' )
for formatted local time.
Common capabilities:
manage_options
,
edit_posts
,
edit_others_posts
,
publish_posts
,
delete_posts
,
upload_files
,
edit_theme_options
,
activate_plugins
.
get_post_meta()
$single
applies to:
get_post_meta
,
get_user_meta
,
get_term_meta
,
get_comment_meta
,
get_site_meta
,
get_metadata
,
get_metadata_raw
,
get_metadata_default
.
<!-- Detailed code examples: resources/api-deprecated-formatting.md -->
WordPress API调用的核心规则。在
current_user_can()
中使用权限(而非角色)。Cron任务间隔必须不少于15分钟。限制
posts_per_page
(禁止使用
-1
)。禁止覆盖WP全局变量(如
$post
$wp_query
)。调用
get_post_meta()
时必须传入
$single
参数。避免使用
current_time( 'timestamp' )
——使用
time()
获取UTC时间,或
current_time( 'mysql' )
获取格式化的本地时间。
常用权限:
manage_options
edit_posts
edit_others_posts
publish_posts
delete_posts
upload_files
edit_theme_options
activate_plugins
$single
参数适用于以下函数:
get_post_meta
get_user_meta
get_term_meta
get_comment_meta
get_site_meta
get_metadata
get_metadata_raw
get_metadata_default
<!-- 详细代码示例:resources/api-deprecated-formatting.md -->

7. Deprecated Functions (Common Replacements)

7. 废弃函数(常见替代方案)

Common deprecated functions and their replacements. WPCS flags usage as an error if the function was deprecated before your configured minimum WP version, and a warning otherwise.
DeprecatedSinceReplacement
get_currentuserinfo()
4.5
wp_get_current_user()
get_page_by_title()
6.2
WP_Query
is_taxonomy()
3.0
taxonomy_exists()
is_term()
3.0
term_exists()
get_settings()
2.1
get_option()
get_usermeta()
3.0
get_user_meta()
update_usermeta()
3.0
update_user_meta()
delete_usermeta()
3.0
delete_user_meta()
wp_get_sites()
4.6
get_sites()
like_escape()
4.0
$wpdb->esc_like()
get_all_category_ids()
4.0
get_terms()
post_permalink()
4.4
get_permalink()
force_ssl_login()
4.4
force_ssl_admin()
wp_no_robots()
5.7
wp_robots_no_robots()
wp_make_content_images_responsive()
5.5
wp_filter_content_tags()
add_option_whitelist()
5.5
add_allowed_options()
remove_option_whitelist()
5.5
remove_allowed_options()
wp_get_loading_attr_default()
6.3
wp_get_loading_optimization_attributes()
the_meta()
6.0.2
get_post_meta()
readonly()
5.9
wp_readonly()
attribute_escape()
2.8
esc_attr()
wp_specialchars()
2.8
esc_html()
js_escape()
2.8
esc_js()
clean_url()
3.0
esc_url()
seems_utf8()
6.9
wp_is_valid_utf8()
current_user_can_for_blog()
6.7
current_user_can_for_site()
<!-- Full table also in: resources/api-deprecated-formatting.md -->
常见的废弃函数及其替代方案。如果函数在你配置的最低WP版本之前已被废弃,WPCS会将其标记为错误;否则标记为警告。
废弃函数废弃版本替代方案
get_currentuserinfo()
4.5
wp_get_current_user()
get_page_by_title()
6.2
WP_Query
is_taxonomy()
3.0
taxonomy_exists()
is_term()
3.0
term_exists()
get_settings()
2.1
get_option()
get_usermeta()
3.0
get_user_meta()
update_usermeta()
3.0
update_user_meta()
delete_usermeta()
3.0
delete_user_meta()
wp_get_sites()
4.6
get_sites()
like_escape()
4.0
$wpdb->esc_like()
get_all_category_ids()
4.0
get_terms()
post_permalink()
4.4
get_permalink()
force_ssl_login()
4.4
force_ssl_admin()
wp_no_robots()
5.7
wp_robots_no_robots()
wp_make_content_images_responsive()
5.5
wp_filter_content_tags()
add_option_whitelist()
5.5
add_allowed_options()
remove_option_whitelist()
5.5
remove_allowed_options()
wp_get_loading_attr_default()
6.3
wp_get_loading_optimization_attributes()
the_meta()
6.0.2
get_post_meta()
readonly()
5.9
wp_readonly()
attribute_escape()
2.8
esc_attr()
wp_specialchars()
2.8
esc_html()
js_escape()
2.8
esc_js()
clean_url()
3.0
esc_url()
seems_utf8()
6.9
wp_is_valid_utf8()
current_user_can_for_blog()
6.7
current_user_can_for_site()
<!-- 完整表格见:resources/api-deprecated-formatting.md -->

8. Formatting

8. 格式规范

Spacing: WordPress uses spaces inside parentheses, brackets, and around operators. Control structures always have spaces after keywords and inside parens:
if ( $x )
,
foreach ( $arr as $v )
.
Indentation: Use tabs, not spaces. Multi-line arrays: one item per line, trailing comma.
Operator spacing: Spaces around
=
,
+
,
.
,
=>
, etc. No spaces around
->
or
?->
.
Cast spacing: Space after cast, no space inside:
(int) $val
.
<!-- Detailed code examples: resources/api-deprecated-formatting.md -->
空格: WordPress要求在括号、方括号内以及运算符两侧添加空格。控制结构的关键字后必须加空格,且括号内也要加空格:
if ( $x )
foreach ( $arr as $v )
缩进: 使用制表符,而非空格。多行数组:每行一个元素,末尾加逗号。
运算符空格:
=
+
.
=>
等运算符两侧加空格。
->
?->
两侧不加空格。
类型转换空格: 类型转换后加空格,括号内不加空格:
(int) $val
<!-- 详细代码示例:resources/api-deprecated-formatting.md -->

9. Testing

9. 测试

PHP: Use PHPUnit with
WP_UnitTestCase
. Install via
composer require --dev phpunit/phpunit
or
wp scaffold plugin-tests
. Test files in
tests/
, named
test-class-{name}.php
.
JavaScript: Use
@wordpress/scripts
(bundles Jest):
npx wp-scripts test-unit-js
. E2E via
@wordpress/e2e-test-utils
.
Linting:
vendor/bin/phpcs --standard=WordPress src/
for PHP.
npx wp-scripts lint-js
and
npx wp-scripts lint-style
for JS/CSS.
<!-- Detailed code examples: resources/testing-patterns.md -->
PHP: 使用PHPUnit搭配
WP_UnitTestCase
。可通过
composer require --dev phpunit/phpunit
wp scaffold plugin-tests
安装。测试文件放在
tests/
目录下,命名格式为
test-class-{name}.php
JavaScript: 使用
@wordpress/scripts
(内置Jest):
npx wp-scripts test-unit-js
。端到端测试使用
@wordpress/e2e-test-utils
代码检查: PHP使用
vendor/bin/phpcs --standard=WordPress src/
。JS/CSS使用
npx wp-scripts lint-js
npx wp-scripts lint-style
<!-- 详细代码示例:resources/testing-patterns.md -->

10. Quick Reference Tables

10. 速查表

Control Structure Spacing

控制结构空格

PatternBADGOOD
if
if($x)
if ( $x )
elseif
elseif($x)
elseif ( $x )
foreach
foreach($a as $b)
foreach ( $a as $b )
for
for($i=0;$i<10;$i++)
for ( $i = 0; $i < 10; $i++ )
switch
switch($x)
switch ( $x )
while
while($x)
while ( $x )
模式错误示例正确示例
if
if($x)
if ( $x )
elseif
elseif($x)
elseif ( $x )
foreach
foreach($a as $b)
foreach ( $a as $b )
for
for($i=0;$i<10;$i++)
for ( $i = 0; $i < 10; $i++ )
switch
switch($x)
switch ( $x )
while
while($x)
while ( $x )

Naming Quick Reference

命名速查

ElementConventionExample
Functions
snake_case
acme_get_settings()
Variables
snake_case
$post_title
Classes
Pascal_Case
(underscored)
Acme_Plugin_Admin
Constants
UPPER_SNAKE_CASE
ACME_VERSION
Files
lowercase-hyphens
class-acme-admin.php
Hook names
lowercase_underscores
acme_after_init
Post type slugs
lowercase
,
a-z0-9_-
acme_book
元素规范示例
函数
snake_case
acme_get_settings()
变量
snake_case
$post_title
带下划线的
Pascal_Case
Acme_Plugin_Admin
常量
UPPER_SNAKE_CASE
ACME_VERSION
文件全小写连字符分隔
class-acme-admin.php
钩子名称全小写加下划线
acme_after_init
自定义文章类型别名全小写,仅含
a-z0-9_-
acme_book

WordPress i18n Functions at a Glance

WordPress国际化函数速查

NeedFunction
Return translated string
__()
Echo translated string
_e()
Return + HTML escape
esc_html__()
Echo + HTML escape
esc_html_e()
Return + attr escape
esc_attr__()
Echo + attr escape
esc_attr_e()
With context
_x()
,
esc_html_x()
,
esc_attr_x()
Singular/plural
_n()
Singular/plural + context
_nx()
需求函数
返回翻译后的字符串
__()
输出翻译后的字符串
_e()
返回翻译并经过HTML转义的字符串
esc_html__()
输出翻译并经过HTML转义的字符串
esc_html_e()
返回翻译并经过属性转义的字符串
esc_attr__()
输出翻译并经过属性转义的字符串
esc_attr_e()
带上下文的翻译
_x()
esc_html_x()
esc_attr_x()
单复数处理
_n()
单复数处理+上下文
_nx()