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 kellenmace/f23ae82f8d6142cebcc0a6d7b710c61e to your computer and use it in GitHub Desktop.
Save kellenmace/f23ae82f8d6142cebcc0a6d7b710c61e to your computer and use it in GitHub Desktop.
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