Instantly share code, notes, and snippets.

Embed
What would you like to do?
require-checkbox-is-checked-on-wordpress-post-save.js
( function ( $ ) {
$( document ).ready( function () {
// Require that one of the checkboxes is checked when publishing/updating posts
$( 'body' ).on( 'submit.edit-post', '#post', function () {
// If all three checkboxes are not checked,
if ( ! $('#checkbox-one').attr('checked') && ! $('#checkbox-two').attr('checked') && ! $('#checkbox-three').attr('checked') ) {
// Show the alert.
window.alert( 'Checking one of the checkboxes is required.' );
// Hide the spinner.
$( '#major-publishing-actions .spinner' ).hide();
// The buttons get "disabled" added to them on submit. Remove that class.
$( '#major-publishing-actions' ).find( ':button, :submit, a.submitdelete, #post-preview' ).removeClass( 'disabled' );
// Focus on the first checkbox field.
$( "#checkboxOne" ).focus();
// Prevent the create/update post form from being submitted.
return false;
}
});
});
}( jQuery ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment