I don’t want a consumer to have the ability to publish a publish if no class is chosen. This actual answer from an analogous query is strictly what i need to do:
jQuery(doc).prepared(operate ($) {
// Test for precisely one class earlier than publishing or updating
$('#publish').click on(checkForCategory);
});
operate checkForCategory()
{
var categoryBlock = doc.getElementById('categorychecklist');
if categoryBlock == null {
return true;
}
var catList = categoryBlock.getElementsByClassName('selectit');
var error = false;
var idx;
var selectedCnt = 0;
var uncategorized = false;
// Iterate by way of all the classes to rely the checked ones
for (idx = 0; idx < catList.size; idx++) {
inputElements = catList[idx].getElementsByTagName('enter');
if ((inputElements.size > 0) && (inputElements[0].checked)) {
selectedCnt++;
if (catList[idx].innerHTML.indexOf('Uncategorized') > 0) {
uncategorized = true;
}
}
}
if (uncategorized) {
alert("You should unselect the Uncategorized class earlier than publishing.");
error = true;
}
else if (selectedCnt == 0) {
alert("You should choose a class earlier than publishing.");
error = true;
}
else if (selectedCnt > 1) {
alert("You might solely choose one class when publishing.");
error = true;
}
return !error;
}
Nevertheless, the script doesn’t appear to work with WP6 or Gutenberg.
Any answer on the way to make the script work within the newest model of WP utilizing Gutenberg and the way to enqueue the script within the WP Admin?