Loading...
Loading...
Compare original and translation side by side
snake_casePascal_CaseMy_Plugin_Admin| Element | Convention | Example |
|---|---|---|
| Functions | | |
| Variables | | |
| Classes | | |
| Constants | | |
| Files | | |
| Hook names | | |
| Post type slugs | | |
class-wordpresswp_phppostpageattachmentwp_snake_casePascal_CaseMy_Plugin_Admin| 元素 | 规范 | 示例 |
|---|---|---|
| 函数 | | |
| 变量 | | |
| 类 | 带下划线的 | |
| 常量 | | |
| 文件 | 全小写连字符分隔 | |
| 钩子名称 | 全小写加下划线 | |
| 自定义文章类型别名 | 全小写,仅含 | |
class-wordpresswp_phppostpageattachmentwp_==!====!==truein_array()array_search()array_keys()| Function | Why | Alternative |
|---|---|---|
| Pollutes local scope unpredictably | Destructure manually |
| Hides errors | Check return values; use |
| Breaks interoperability | Use WP constants ( |
var_dumpvar_exportprint_rerror_logtrigger_errorphpinfo| PHP Native | WordPress Alternative |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
file_get_contents()ABSPATHWP_CONTENT_DIRplugin_dir_path()(int)(float)(bool)(string)(integer)(real)(unset)==!====!==in_array()array_search()array_keys()true| 函数 | 原因 | 替代方案 |
|---|---|---|
| 不可预测地污染局部作用域 | 手动解构 |
| 隐藏错误 | 检查返回值;使用 |
| 破坏互操作性 | 使用WP常量(如 |
var_dumpvar_exportprint_rerror_logtrigger_errorphpinfo| PHP原生函数 | WordPress替代函数 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
ABSPATHWP_CONTENT_DIRplugin_dir_path()file_get_contents()(int)(float)(bool)(string)(integer)(real)(unset)// 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' );// 动作:处理副作用(发送邮件、保存数据、入队资源)
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' );// 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;
}// 默认优先级为10,默认接受参数数量为1
add_filter( 'the_title', 'acme_modify_title', 10, 2 );
function acme_modify_title( $title, $post_id ) {
// 必须声明正确数量的参数
return $title;
}// 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' ) );// 移除函数回调
remove_action( 'wp_head', 'wp_generator' );
// 匹配优先级移除
remove_filter( 'the_content', 'acme_append_cta', 10 );
// 移除对象方法(必须是同一实例)
remove_action( 'init', array( $instance, 'init' ) );// 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 );// 插件钩子需加前缀并使用下划线
do_action( 'acme_before_render', $context );
$value = apply_filters( 'acme_output_format', $default, $post );
// 动态钩子:前缀为静态部分
do_action( "acme_process_{$type}", $data );| Function | Purpose |
|---|---|
| Return translated string |
| Echo translated string |
| Return with disambiguation context |
| Echo with disambiguation context |
| Pluralization |
| Plural + context |
| Return translated + HTML-escaped |
| Echo translated + HTML-escaped |
| Return translated + attribute-escaped |
| Echo translated + attribute-escaped |
| Return translated + escaped + context |
| Return translated + escaped + context |
| 函数 | 用途 |
|---|---|
| 返回翻译后的字符串 |
| 输出翻译后的字符串 |
| 返回带消歧上下文的翻译字符串 |
| 输出带消歧上下文的翻译字符串 |
| 复数处理 |
| 复数处理+上下文 |
| 返回翻译并经过HTML转义的字符串 |
| 输出翻译并经过HTML转义的字符串 |
| 返回翻译并经过属性转义的字符串 |
| 输出翻译并经过属性转义的字符串 |
| 返回翻译、转义并带上下文的字符串 |
| 返回翻译、转义并带上下文的字符串 |
// BAD - separate escape and translate
echo esc_html( __( 'Hello', 'acme-plugin' ) );
// GOOD - combined function
echo esc_html__( 'Hello', 'acme-plugin' );esc_html()esc_attr()esc_html__()esc_attr__()// 错误示例:分开转义和翻译
echo esc_html( __( 'Hello', 'acme-plugin' ) );
// 正确示例:使用组合函数
echo esc_html__( 'Hello', 'acme-plugin' );esc_html()esc_attr()esc_html__()esc_attr__()// 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 );// 错误示例
printf( __( '%s: %s', 'acme-plugin' ), $label, $value );
// 正确示例
/* translators: 1: 字段标签, 2: 字段值 */
printf( __( '%1$s: %2$s', 'acme-plugin' ), $label, $value );%1$s%2$s%s%s%1$s%s%1$s%2$s%s%s%1$s%s// 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' );// 错误示例:直接输出
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' );| Parameter | Required? | Notes |
|---|---|---|
| Yes | Unique identifier |
| Conditional | Omit only when registering dependency-only handle |
| Recommended | Array of dependency handles |
| Yes (when src given) | Must be non-false; use explicit version string. |
| Yes | Explicitly set |
| Optional | Default |
// BAD - missing version, missing in_footer
wp_enqueue_script( 'acme-main', $url );
// GOOD
wp_enqueue_script( 'acme-main', $url, array(), '1.0.0', true );| 参数 | 是否必填 | 说明 |
|---|---|---|
| 是 | 唯一标识符 |
| 可选 | 仅当注册仅作为依赖的句柄时可省略 |
| 推荐 | 依赖句柄数组 |
| 是(当提供$src时) | 必须为非false值;使用明确的版本字符串。 |
| 是 | 显式设置 |
| 可选 | 默认值为 |
// 错误示例:缺少版本和in_footer参数
wp_enqueue_script( 'acme-main', $url );
// 正确示例
wp_enqueue_script( 'acme-main', $url, array(), '1.0.0', true );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' );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' );current_user_can()posts_per_page-1$post$wp_query$singleget_post_meta()current_time( 'timestamp' )time()current_time( 'mysql' )manage_optionsedit_postsedit_others_postspublish_postsdelete_postsupload_filesedit_theme_optionsactivate_pluginsget_post_meta()$singleget_post_metaget_user_metaget_term_metaget_comment_metaget_site_metaget_metadataget_metadata_rawget_metadata_defaultcurrent_user_can()posts_per_page-1$post$wp_queryget_post_meta()$singlecurrent_time( 'timestamp' )time()current_time( 'mysql' )manage_optionsedit_postsedit_others_postspublish_postsdelete_postsupload_filesedit_theme_optionsactivate_plugins$singleget_post_metaget_user_metaget_term_metaget_comment_metaget_site_metaget_metadataget_metadata_rawget_metadata_default| Deprecated | Since | Replacement |
|---|---|---|
| 4.5 | |
| 6.2 | |
| 3.0 | |
| 3.0 | |
| 2.1 | |
| 3.0 | |
| 3.0 | |
| 3.0 | |
| 4.6 | |
| 4.0 | |
| 4.0 | |
| 4.4 | |
| 4.4 | |
| 5.7 | |
| 5.5 | |
| 5.5 | |
| 5.5 | |
| 6.3 | |
| 6.0.2 | |
| 5.9 | |
| 2.8 | |
| 2.8 | |
| 2.8 | |
| 3.0 | |
| 6.9 | |
| 6.7 | |
| 废弃函数 | 废弃版本 | 替代方案 |
|---|---|---|
| 4.5 | |
| 6.2 | |
| 3.0 | |
| 3.0 | |
| 2.1 | |
| 3.0 | |
| 3.0 | |
| 3.0 | |
| 4.6 | |
| 4.0 | |
| 4.0 | |
| 4.4 | |
| 4.4 | |
| 5.7 | |
| 5.5 | |
| 5.5 | |
| 5.5 | |
| 6.3 | |
| 6.0.2 | |
| 5.9 | |
| 2.8 | |
| 2.8 | |
| 2.8 | |
| 3.0 | |
| 6.9 | |
| 6.7 | |
if ( $x )foreach ( $arr as $v )=+.=>->?->(int) $valif ( $x )foreach ( $arr as $v )=+.=>->?->(int) $valWP_UnitTestCasecomposer require --dev phpunit/phpunitwp scaffold plugin-teststests/test-class-{name}.php@wordpress/scriptsnpx wp-scripts test-unit-js@wordpress/e2e-test-utilsvendor/bin/phpcs --standard=WordPress src/npx wp-scripts lint-jsnpx wp-scripts lint-styleWP_UnitTestCasecomposer require --dev phpunit/phpunitwp scaffold plugin-teststests/test-class-{name}.php@wordpress/scriptsnpx wp-scripts test-unit-js@wordpress/e2e-test-utilsvendor/bin/phpcs --standard=WordPress src/npx wp-scripts lint-jsnpx wp-scripts lint-style| Pattern | BAD | GOOD |
|---|---|---|
| if | | |
| elseif | | |
| foreach | | |
| for | | |
| switch | | |
| while | | |
| 模式 | 错误示例 | 正确示例 |
|---|---|---|
| if | | |
| elseif | | |
| foreach | | |
| for | | |
| switch | | |
| while | | |
| Element | Convention | Example |
|---|---|---|
| Functions | | |
| Variables | | |
| Classes | | |
| Constants | | |
| Files | | |
| Hook names | | |
| Post type slugs | | |
| 元素 | 规范 | 示例 |
|---|---|---|
| 函数 | | |
| 变量 | | |
| 类 | 带下划线的 | |
| 常量 | | |
| 文件 | 全小写连字符分隔 | |
| 钩子名称 | 全小写加下划线 | |
| 自定义文章类型别名 | 全小写,仅含 | |
| Need | Function |
|---|---|
| Return translated string | |
| Echo translated string | |
| Return + HTML escape | |
| Echo + HTML escape | |
| Return + attr escape | |
| Echo + attr escape | |
| With context | |
| Singular/plural | |
| Singular/plural + context | |
| 需求 | 函数 |
|---|---|
| 返回翻译后的字符串 | |
| 输出翻译后的字符串 | |
| 返回翻译并经过HTML转义的字符串 | |
| 输出翻译并经过HTML转义的字符串 | |
| 返回翻译并经过属性转义的字符串 | |
| 输出翻译并经过属性转义的字符串 | |
| 带上下文的翻译 | |
| 单复数处理 | |
| 单复数处理+上下文 | |