Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Modifies the behavior of the taxonomy box, forbids user from selecting terms that don't belong to taxonomy.

View no-new-terms-taxonomy-meta-box.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?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.
*/
// currently works only with single taxonomy which should be defined here
define('CTM_TAXONOMY_NAME', 'mamade');
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;
};
jQuery(document).ready(function() {
// unbiind the click event from the taxonomy box
jQuery('#tagsdiv-<?php echo CTM_TAXONOMY_NAME; ?> input.tagadd').unbind('click');
// 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);
});
</script>
 
<?php
}
add_action('admin_footer', 'ctm_custom_tax_js');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.