Search code examples
wordpresswordpress-theming

Wordpress Force Post to select category


I am using WordPress to create posts. Is there any plugin or function that will warn me when I don't select a category for the post. It's a headache everytime I post a category, I miss selecting the category sometimes as there is a lot of posts to be made for the category. And after sometime I see those posts in the uncategorized category.

Is there something that will make the category required and force me to select it?

Thanks in advance. Hope I can find a suitable answer.


Solution

  • hah.. its so funny but you can try as below in your functions.php file

    function force_post_categ_init() 
    {
      wp_enqueue_script('jquery');
    }
    function force_post_categ() 
    {
      echo "<script type='text/javascript'>\n";
      echo "
      jQuery('#publish').click(function() 
      {
        var cats = jQuery('[id^=\"taxonomy\"]')
          .find('.selectit')
          .find('input');
        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('You have not selected any category for the post. Please select post category.');
          setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
          jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
          setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
          return false;
        }
      });
      ";
       echo "</script>\n";
    }
    add_action('admin_init', 'force_post_categ_init');
    add_action('edit_form_advanced', 'force_post_categ');
    

    NOte:- javascript must be enable to run this