Prevent post from being published if no category selected

I do not want a user to be able to publish a post if no category is selected. I don’t mind throwing a JS alert or handling this on the server side as well.

Anyway, how can I ensure this?

Note: “uncategorized” should not be chosen as well.

2 Answers
2

You can do this easily with jQuery:

/* Checks if cat is selected when publish button is clicked */
jQuery( '#submitdiv' ).on( 'click', '#publish', function( e ) {
    var $checked = jQuery( '#category-all li input:checked' );

    //Checks if cat is selected
    if( $checked.length <= 0 ) {
        alert( "Please Select atleast one category" );
        return false;
    } else { 
        return true;
    }
} );

The above code will show an alert box if no category is selected. You can use dialog box instead if you’d like.

Leave a Comment