我为你的第一个问题想出了一个解决方案。i、 e税务元框,仅建议现有条款列表中的条款,但不允许添加新条款。该解决方案基于jQuery,并修改标记(即非继承人分类法)元框的默认行为。
Limitation: 目前,它只允许一次添加一个术语,即不能将多个现有术语添加为逗号分隔的值。
代码也可以作为github的gist.
我可能会在下周末为分类法做菜单编辑器,比如metabox
下面的解决方案可以用作插件,也可以用于您的函数。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\');
?>
UPDATE: 根据@mike的评论更新代码以处理返回键。