enter_title_here
云策文档标注
概述
enter_title_here 过滤器用于修改 WordPress 后台文章编辑页面中标题字段的占位符文本,适用于 Classic Editor 和 Gutenberg 编辑器,特别在自定义文章类型(CPT)开发中非常有用。
关键要点
- 过滤器名称:enter_title_here
- 参数:$text(占位符文本,默认为 'Add title')和 $post(WP_Post 对象)
- 用途:允许开发者根据文章类型动态更改标题字段的占位符提示文本
- 兼容性:支持 Classic Editor 和 Gutenberg 编辑器
- 应用场景:常用于自定义文章类型,以提供更贴切的用户界面提示
代码示例
if( is_admin() ) {
add_filter( 'enter_title_here', function( $input ) {
if( 'projects' === get_post_type() ) {
return __( 'Enter Project Name', 'textdomain' );
} else if ( 'testimonials' === get_post_type() ) {
return __( 'Enter Client's Name', 'textdomain' );
} else {
return $input;
}
} );
}注意事项
- 此过滤器自 WordPress 3.1.0 版本引入
- 建议在 is_admin() 条件下使用,以确保仅影响后台界面
- 代码示例中使用了匿名函数和 get_post_type() 来针对不同文章类型设置占位符
原文内容
Filters the title field placeholder text.
Parameters
$textstring-
Placeholder text. Default ‘Add title’.
$postWP_Post-
Post object.
Source
$title_placeholder = apply_filters( 'enter_title_here', __( 'Add title' ), $post );
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Skip to note 2 content
iSaumya
This filter is used to change the placeholder text of the Title field whether you are using the Classic Editor version or Gutenberg version. This works in both cases. This is really useful when you are creating a Custom Post Type (CPT).
Let’s say you have a CPT called
testimonialsto add client testimonials in your site. But when you make the CPT the title field place holder will say “Enter title” instead maybe you wanna show “Enter Client Name” there. so you can do the following:if( is_admin() ) { add_filter( 'enter_title_here', function( $input ) { if( 'projects' === get_post_type() ) { return __( 'Enter Project Name', 'textdomain' ); } else if ( 'testimonials' === get_post_type() ) { return __( 'Enter Client's Name', 'textdomain' ); } else { return $input; } } ); }