capital_P_dangit()
云策文档标注
概述
capital_P_dangit() 是一个 WordPress 核心函数,用于将文本中的 "Wordpress" 错误拼写自动更正为 "WordPress"。它通过钩子应用于标题、内容和评论等场景,确保品牌名称的一致性。
关键要点
- 函数功能:将 "Wordpress" 替换为 "WordPress",处理不同上下文(如标题、引号、括号内)的拼写错误。
- 参数:接受一个字符串参数 $text,返回修改后的字符串。
- 钩子应用:默认通过 the_title、the_content、comment_text 等过滤器钩子自动执行。
- 相关函数:使用 current_filter() 检测当前钩子,_x() 获取本地化引号字符。
代码示例
function capital_P_dangit( $text ) {
// Simple replacement for titles.
$current_filter = current_filter();
if ( 'the_title' === $current_filter || 'wp_title' === $current_filter ) {
return str_replace( 'Wordpress', 'WordPress', $text );
}
// Still here? Use the more judicious replacement.
static $dblq = false;
if ( false === $dblq ) {
$dblq = _x( '“', 'opening curly double quote' );
}
return str_replace(
array( ' WordPress', '‘Wordpress', $dblq . 'Wordpress', '>Wordpress', '(WordPress' ),
array( ' WordPress', '‘WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
$text
);
}注意事项
- 开发者可以通过 remove_filter() 移除该函数的自动应用,例如在特定场景下禁用更正功能。
- 示例代码展示了如何从 the_title、the_content 和 comment_text 钩子中移除 capital_P_dangit() 过滤器。
原文内容
Forever eliminate “Wordpress” from the planet (or at least the little bit we can influence).
Description
Violating our coding standards for a good function name.
Parameters
$textstringrequired-
The text to be modified.
Source
function capital_P_dangit( $text ) {
// Simple replacement for titles.
$current_filter = current_filter();
if ( 'the_title' === $current_filter || 'wp_title' === $current_filter ) {
return str_replace( 'Wordpress', 'WordPress', $text );
}
// Still here? Use the more judicious replacement.
static $dblq = false;
if ( false === $dblq ) {
$dblq = _x( '“', 'opening curly double quote' );
}
return str_replace(
array( ' WordPress', '‘Wordpress', $dblq . 'Wordpress', '>Wordpress', '(WordPress' ),
array( ' WordPress', '‘WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
$text
);
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 2 content
Codex
Example
If you prefer not to use these filters, here’s how you can remove them:
remove_filter( 'the_title', 'capital_P_dangit', 11 ); remove_filter( 'the_content', 'capital_P_dangit', 11 ); remove_filter( 'comment_text', 'capital_P_dangit', 31 );