Created
May 16, 2014 12:54
-
-
Save kshaner/41de8d7c08f16724887a to your computer and use it in GitHub Desktop.
Require a wordpress taxonomy to be selected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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