Skip to content

Instantly share code, notes, and snippets.

@m-miller
Created September 26, 2017 06:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-miller/9ead2c871b9d27613b868a2dbfd9d7e5 to your computer and use it in GitHub Desktop.
Save m-miller/9ead2c871b9d27613b868a2dbfd9d7e5 to your computer and use it in GitHub Desktop.
Wordpress conditional metabox on checked category
jQuery(document).ready(function() {
function metabox_category() {
// conditional show/hide metaboxes on checked category
var label = null;
var onemeta = jQuery('#id_of_custommetabox.postbox');
// get checked checkboxes
var ischecked = jQuery('#taxonomy_name_categorychecklist input[type="checkbox"]:checked');
var label = ischecked.parent().text().trim();
//console.log(label);
//console.log(ischecked.length);
// hide metaboxes if nothing checked
if (ischecked.length == 0){
onemeta.hide();
}
// if needed label is in label string
if (~label.indexOf("label_of_checkbox")){ // javascript tilde operator
onemeta.show('slow');
} else {
onemeta.hide('slow');
}
}
jQuery('#taxonomy_name_categorychecklist input[type="checkbox"]').live('click', metabox_category); // calls the function on click of category checkbox
metabox_category(); // calls the function on load
});
@m-miller
Copy link
Author

show/hide a metabox based on a category or custom taxonomy in WordPress admin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment