問題描述
我想知道如何在一個類似於”tags” 類 (帶有搜尋欄位和自動建議) 的後期編輯頁面上設定一個分類學的元代謝,但沒有許可權新增一個新的術語。
所以例如,如果我打字,我可以從現有的術語列表中獲得我可以使用的術語的建議,但是如果我輸入一個不存在的詞,那麼它不會將這些術語新增到列表中。
編輯
實際上,我正在尋找的就是選單編輯器中”Search” 功能的行為:
由於這是一個核心的 WP 行為,有沒有辦法在後期編輯頁面上使用它?我想知道,因為這些塊看起來完全一樣,都有”view all” 和”most recent” 選項卡,但”search” 只存在於選單編輯器中。
最佳解決方案
我想出了你的第一個問題的解決方案。即僅代表現有條款清單中的條款但不允許您新增新條款的代理變通稅。解決方案是基於 jQuery 並修改標籤的預設行為 (即非分類分類法) 元框。
限制:目前,它只允許一次新增 1 個術語,那就是您不能新增多個現有術語作為逗號分隔的值。
該程式碼也可以作為 github 的 gist 。
我可能會在下週末做菜譜編輯器,例如分類學的 metabox 。 😉
下面的解決方案可以用作外掛,也可以在你的 function.php 檔案中使用。
<?php
/*
Plugin Name: No new terms taxonomy meta box
Plugin URI: https://gist.github.com/1074801
Description: Modifies the behavior of the taxonomy box, forbids user from selecting terms that don't belong to taxonomy.
Author: Hameedullah Khan
Author URI: http://hameedullah.com
Version: 0.1
License: Do what ever you like, but don't publish it under your name without improving it.
*/
/*
* For more information: http://wordpress.stackexchange.com/questions/20921/
*/
// currently works only with single taxonomy which should be defined here
// default is the built-in post_tag
define('CTM_TAXONOMY_NAME', 'post_tag');
function ctm_custom_tax_js() {
// taxonomy name not defined or set to empty value
if ( !defined('CTM_TAXONOMY_NAME') || !CTM_TAXONOMY_NAME ) {
return;
}
?>
<script type="text/javascript">
function ctm_custom_termadd_handler(event){
var tax = '<?php echo CTM_TAXONOMY_NAME; ?>';
var input = jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> input.newtag');
var q = input.val().split(',');
// if there are more then two values, just add the first one
// NOTE: because this solution does not support inserting multiple terms
if (q.length > 1) {
q = jQuery.trim(q[0]);
// as we don't support multiple terms
// set the value of input box to the first term
input.val(q);
}
jQuery.get( ajaxurl + '?action=ajax-tag-search&tax=' + tax + '&q=' + q, function(results) {
var tokens = results.split('n');
for (var i=0; i < tokens.length; i++) {
token = jQuery.trim(tokens[i]);
if ( token && token == q ) {
(function($){
tagBox.flushTags( $('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?>') );
})(jQuery);
return true;
}
}
} );
event.stopImmediatePropagation();
return false;
}
function ctm_custom_key_handler(event) {
if (13 == event.which) {
ctm_custom_termadd_handler(event);
return false;
}
return true;
}
jQuery(document).ready(function() {
// unbiind the click event from the taxonomy box
jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> input.tagadd').unbind('click');
jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> input.newtag').unbind('keyup');
// hide the howto text for inserting multiple terms
// NOTE: because this solution does not support inserting multiple terms
jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> p.howto').hide();
// bind our custom handler
jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> input.tagadd').click(ctm_custom_termadd_handler);
jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> input.newtag').keyup(ctm_custom_key_handler);
});
</script>
<?php
}
add_action('admin_footer-post-new.php', 'ctm_custom_tax_js');
add_action('admin_footer-post.php', 'ctm_custom_tax_js');
?>
更新:程式碼更新,以按照 @ mike 的註釋處理返回鍵。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
