問題描述
有沒有辦法改變自定義帖子型別的釋出按鈕的文字來說一些不同?例如,儲存而不是釋出。還要刪除草稿按鈕?
最佳解決方案
如果您檢視/wp-admin/edit-form-advanced.php,您會發現元框:
add_meta_box('submitdiv', __('Publish'), 'post_submit_meta_box', $post_type, 'side', 'core');
注意__('Publish') – 功能__()導致 translate()您得到過濾器'gettext'。
有兩種方法可以解決您的問題:1. 在一個專門的功能中處理字串 (確保匹配正確的文字域!) 或 2. 使用更通用的方法。
@Rarst 剛剛釋出了版本 1,所以我將新增版本 2. 🙂
<?php
/*
Plugin Name: Retranslate
Description: Adds translations.
Version: 0.1
Author: Thomas Scholz
Author URI: http://toscho.de
License: GPL v2
*/
class Toscho_Retrans {
// store the options
protected $params;
/**
* Set up basic information
*
* @param array $options
* @return void
*/
public function __construct( array $options )
{
$defaults = array (
'domain' => 'default'
, 'context' => 'backend'
, 'replacements' => array ()
, 'post_type' => array ( 'post' )
);
$this->params = array_merge( $defaults, $options );
// When to add the filter
$hook = 'backend' == $this->params['context']
? 'admin_head' : 'template_redirect';
add_action( $hook, array ( $this, 'register_filter' ) );
}
/**
* Conatiner for add_filter()
* @return void
*/
public function register_filter()
{
add_filter( 'gettext', array ( $this, 'translate' ), 10, 3 );
}
/**
* The real working code.
*
* @param string $translated
* @param string $original
* @param string $domain
* @return string
*/
public function translate( $translated, $original, $domain )
{
// exit early
if ( 'backend' == $this->params['context'] )
{
global $post_type;
if ( ! empty ( $post_type )
&& ! in_array( $post_type, $this->params['post_type'] ) )
{
return $translated;
}
}
if ( $this->params['domain'] !== $domain )
{
return $translated;
}
// Finally replace
return strtr( $original, $this->params['replacements'] );
}
}
// Sample code
// Replace 'Publish' with 'Save' and 'Preview' with 'Lurk' on pages and posts
$Toscho_Retrans = new Toscho_Retrans(
array (
'replacements' => array (
'Publish' => 'Save'
, 'Preview' => 'Lurk'
)
, 'post_type' => array ( 'page', 'post' )
)
);
您不需要將程式碼用作外掛。把它包含在你的主題的 functions.php 就夠了。
Update
要刪除原始的儲存按鈕 (不知道’draft’ 按鈕是什麼),請將以下程式碼新增到您的 functions.php /a 外掛中:
add_action( 'admin_print_footer_scripts', 'remove_save_button' );
function remove_save_button()
{
?>
<script>
jQuery(document).ready(function($){$('#save-post').remove();});
</script><?php
}
是的,這很醜陋
次佳解決方案
hakre 建議使用翻譯過濾器的程式碼將是這樣的:
add_filter( 'gettext', 'change_publish_button', 10, 2 );
function change_publish_button( $translation, $text ) {
if ( $text == 'Publish' )
return 'Save';
return $translation;
}
第三種解決方案
這不是一個完整的答案,而是一些方向:顯示的任何文字都超過了翻譯過濾器,因此可以在回撥函式 (hook) 中進行更改。所以如果掛鉤只是在你想要更改的頁面上註冊,那麼工作就完成了。
草稿按鈕可以透過 CSS 隱藏來進行”removed” 。 CSS 可以被注入到管理員的<head> 標籤中,另一個回撥。我認為這個過濾器叫做 admin_head 。按鈕仍然在那裡,例如如果使用者切換了 CSS,那就有些髒了。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。