我正在尝试使用gettext
过滤器(source):
function theme_change_comments_label( $translated_text, $untranslated_text, $domain ) {
if( stripos( $untranslated_text, \'comment\' !== FALSE ) ) {
$translated_text = str_ireplace( \'Comment\', \'Review\', $untranslated_text ) ;
}
return $translated_text;
}
is_admin() && add_filter( \'gettext\', \'theme_change_comments_label\', 99, 3 );
我希望它只对一个特定的职位类型(在管理)。所以我试过了
get_current_screen()
在功能范围内:
function theme_change_comments_label( $translated_text, $untranslated_text, $domain ) {
$screen = get_current_screen();
if( $screen->post_type == \'mycpt\' ) {
if( stripos( $untranslated_text, \'comment\' !== FALSE ) ) {
$translated_text = str_ireplace( \'Comment\', \'Review\', $untranslated_text ) ;
}
return $translated_text;
}
}
is_admin() && add_filter( \'gettext\', \'theme_change_comments_label\', 99, 3 );
但我有个错误:
Fatal error: 调用未定义函数get_current_screen()
经过几次测试,我明白了,gettext
不是触发get_current_screen()
作用
那么,我如何才能做到这一点,只针对我的自定义帖子类型?
最合适的回答,由SO网友:cybmeta 整理而成
相符合的with the codex, get_current_screen()
必须晚于admin_init
钩经过几次测试,似乎最安全的方法是使用current_screen
动作挂钩代替get_current_screen()
:
add_action(\'current_screen\', \'current_screen_callback\');
function current_screen_callback($screen) {
if( is_object($screen) && $screen->post_type == \'mycpt\' ) {
add_filter( \'gettext\', \'theme_change_comments_label\', 99, 3 );
}
}
function theme_change_comments_label( $translated_text, $untranslated_text, $domain ) {
if( stripos( $untranslated_text, \'comment\' ) !== FALSE ) {
$translated_text = str_ireplace( \'Comment\', \'Review\', $untranslated_text ) ;
}
return $translated_text;
}
如果需要,可以重用过滤器,例如在“mycpt”存档的前端:
add_action(\'init\', function() {
if( is_post_type_archive( \'mycpt\' ) ) {
add_filter( \'gettext\', \'theme_change_comments_label\', 99, 3 );
}
});
SO网友:bonger
get_current_screen()
是一种痛苦,我使用以下代码来避免/包装它:
/*
* Convenience function to tell if we\'re on a specified page.
*/
function theme_is_current_screen( $base = null, $post_type = null ) {
if ( ! $base && ! $post_type ) {
return false;
}
$screen = function_exists( \'get_current_screen\' ) ? get_current_screen() : null;
if ( ! $screen ) {
// Fake it.
$screen = new StdClass;
$screen->post_type = $screen->base = \'\';
global $pagenow;
if ( $pagenow == \'admin-ajax.php\' ) {
if ( isset( $_REQUEST[\'action\'] ) ) {
$screen->base = $_REQUEST[\'action\'];
}
} else {
$screen->post_type = isset( $_REQUEST[\'post_type\'] ) ? $_REQUEST[\'post_type\'] : \'\';
if ( $pagenow == \'post.php\' || $pagenow == \'post-new.php\' || $pagenow == \'edit.php\' ) {
$screen->base = preg_replace( \'/[^a-z].+$/\', \'\', $pagenow );
if ( ! $screen->post_type ) {
$screen->post_type = get_post_type( theme_get_post_id() );
}
} else {
$page_hook = \'\';
global $plugin_page;
if ( ! empty( $plugin_page ) ) {
if ( $screen->post_type ) {
$the_parent = $pagenow . \'?post_type=\' . $screen->post_type;
} else {
$the_parent = $pagenow;
}
if ( ! ( $page_hook = get_plugin_page_hook( $plugin_page, $the_parent ) ) ) {
$page_hook = get_plugin_page_hook( $plugin_page, $plugin_page );
}
}
$screen->base = $page_hook ? $page_hook : pathinfo( $pagenow, PATHINFO_FILENAME );
}
}
}
// The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped.
if ( $base ) {
if ( ! is_array( $base ) ) $base = array( $base );
if ( ! in_array( $screen->base, $base ) ) {
return false;
}
}
if ( $post_type ) {
if ( ! is_array( $post_type ) ) $post_type = array( $post_type );
if ( ! in_array( $screen->post_type, $post_type ) ) {
return false;
}
}
return true;
}
/*
* Attempt to determine post id in uncertain (admin) situations.
* Based on WPAlchemy_MetaBox::_get_post_id().
*/
function theme_get_post_id() {
global $post;
$ret = 0;
if ( ! empty( $post->ID ) ) {
$ret = $post->ID;
} elseif ( ! empty( $_GET[\'post\'] ) && ctype_digit( $_GET[\'post\'] ) ) {
$ret = $_GET[\'post\'];
} elseif ( ! empty( $_POST[\'post_ID\'] ) && ctype_digit( $_POST[\'post_ID\'] ) ) {
$ret = $_POST[\'post_ID\'];
}
return $ret;
}
您的功能将变成:
function theme_change_comments_label( $translated_text, $untranslated_text, $domain ) {
if( theme_is_current_screen( null, \'mycpt\' ) ) {
if( stripos( $untranslated_text, \'comment\' ) !== FALSE ) {
$translated_text = str_ireplace( \'Comment\', \'Review\', $untranslated_text ) ;
}
}
return $translated_text;
}
is_admin() && add_filter( \'gettext\', \'theme_change_comments_label\', 99, 3 );
它对于简化自定义类型也很有用
admin_init
s、 或者只在自己的设置页面上注册设置。