Force Category Selection Plugin

Sometimes, our enthusiasm gets the best of us and we forget to select a Category when publishing a post, and when viewing the archive page or widgets the post is not displayed or listed. Then we may wonder what happened to the post and soon realize the post was automatically placed into the default WordPress Category, usually the “uncategorized” category.

Here’s a useful function that forces a user to select a Category prior to actually publishing the post.

If the Publish button is pressed and no Category has been selected this happens:

  • Displays an alert “Please select a Category”.
  • Returns focus to a highlighted Categories Meta Box.

Download:  Force Category Plugin.

<?php
/*
Plugin Name: Force Post Category Selection
Plugin URI: http://atconet.com/force-category-selection-plugin
Description: Forces user to assign a category to a post before publishing 
Author: Tony Turrisi
Author URI: http://atconet.com
Version: 1.1
License: No restrictions. free
*/ 
function force_post_cat_init() {
	wp_enqueue_script('jquery');
}

function force_post_cat() {
	echo "<script type='text/javascript'>\n";
	echo "
	jQuery('#publish').click(function() {
		var cats = jQuery('[id^=\"taxonomy\"]')
		.find('.selectit')
		.find('input');
			if(cats.length) {
			category_selected=false;
				for (counter=0; counter<cats.length; counter++) {
					if (cats.get(counter).checked==true) {
					category_selected=true;
					break;
					}
				}
			if(category_selected==false) {
				alert('Please select a Category.');
				setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
				jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#ffffc0');
				setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
				return false;
			}
		}
	});
	";
   echo "</script>\n";
}
add_action('admin_init', 'force_post_cat_init');
add_action('edit_form_advanced', 'force_post_cat');
?>