Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshaner/41de8d7c08f16724887a to your computer and use it in GitHub Desktop.
Save kshaner/41de8d7c08f16724887a to your computer and use it in GitHub Desktop.
Require a wordpress taxonomy to be selected
/*
* Enqueue this function anywhere in the wordpress admin to require a category selection on the array of post types
*/
$(function() {
var post = document.getElementById('post'),
post_types = ['post'],
post_type = false,
errormsg = function(str) {
msgdiv = document.getElementById('message');
if (msgdiv) {
msgdiv.innerHTML = '<p>' + str + '</p>';
if (!$(msgdiv).hasClass('error')) $(msgdiv).addClass('error');
} else {
msgdiv = document.createElement('div');
msgdiv.id = 'message';
msgdiv.className = 'error';
msgdiv.innerHTML = '<p>' + str + '</p>';
post.parentNode.insertBefore(msgdiv, post);
}
};
for(var i=0; i<post_types.length; i++) {
if ($(document.body).hasClass('post-type-'+post_types[i])) {
post_type = post_types[i];
break;
}
}
if (post && post_type) {
$(post).on('submit', function(e) {
if (document.getElementById('categorychecklist') && $('#categorychecklist').find("input:checked").length < 1)
e.preventDefault();
$('#categorydiv').addClass('error');
errormsg('Please select a category');
return;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment