Make Categories and Tags required in admin

Hello i started from Bainternet solution in this post: don’t publish custom post type post if a meta data field isn’t valid

I made a plugin to make categories and tags required.

The validation works great and the popup appear correctly, but when the
categories and tags are selected, the post is not published…just nothing happen.

/* set category and tag as required fields */

add_action('admin_head','my_publish_admin_hook');

function my_publish_admin_hook(){
if(is_admin())
{
    echo"               
    <script language=\"javascript\" type=\"text/javascript\">
        jQuery(document).ready(function() {
            jQuery('#post').submit(function() {

                var form_data = jQuery('#post').serializeArray();
                form_data = jQuery.param(form_data);
                var data = {
                    action: 'my_pre_submit_validation',
                    security: '";echo wp_create_nonce( 'pre_publish_validation' ); echo"',
                    form_data: form_data
                };
                jQuery.post(ajaxurl, data, function(response) {
                    if (response=='true') {
                        jQuery('#ajax-loading').hide();
                        jQuery('#publish').removeClass('button-primary-disabled');
                        return true;
                    }else{
                        alert('Correggi i seguenti errori: ' + response);
                        jQuery('#ajax-loading').hide();
                        jQuery('#publish').removeClass('button-primary-disabled');
                        return false;
                    }
                });
                return false;
            });
        });
    </script>
    ";
    }
}

add_action('wp_ajax_my_pre_submit_validation', 'pre_submit_validation');

function pre_submit_validation(){
//simple Security check
check_ajax_referer( 'pre_publish_validation', 'security' );

$error=null;

/* check categories */    

$post_data=str_replace("%5D","]",$_POST['form_data']);
$post_data=str_replace("%5B","[",$post_data);

$post_data=substr($post_data,strpos($post_data,"&post_category")+18);   
$post_data=substr($post_data,0,strpos($post_data,"&newcategory"));  

$categories_array=array();
if(strlen($post_data)>0)
{
    $categories_array=explode("&post_category[]=",$post_data);
}   
if(count($categories_array)>0)
{
    // categories setted
}
else $error="you must select categories";

/* check tag */ 

    $post_data=str_replace("%2C",",",$_POST['form_data']);
$post_data=str_replace("%5D","]", $post_data);
$post_data=str_replace("%5B","[",$post_data);   

      $post_data=substr($post_data,strpos($post_data,"tax_input[post_tag]=")+20);
   $post_data=substr($post_data,0,strpos($post_data,"&"));

if(strlen($post_data)==0) $error="you must select tags";   

//print validation response   

   if($error==null)
   {
        echo'true'; 
    die();
   }
   else
   {
    echo $error; 
    die();
   }
}

2 Answers
2

The JavaScript function is a bit wrong, its catching the form submit instead of the button click change it to this:

function my_publish_admin_hook(){
if(is_admin())
{
    echo"               
    <script language=\"javascript\" type=\"text/javascript\">
        jQuery(document).ready(function() {
            jQuery('#publish').click(function() {

                var form_data = jQuery('#post').serializeArray();
                form_data = jQuery.param(form_data);
                var data = {
                    action: 'my_pre_submit_validation',
                    security: '";echo wp_create_nonce( 'pre_publish_validation' ); echo"',
                    form_data: form_data
                };
                jQuery.post(ajaxurl, data, function(response) {
                    if (response=='true') {
                        jQuery('#ajax-loading').hide();
                        jQuery('#publish').removeClass('button-primary-disabled');
            jQuery('#post').submit();
                    }else{
                        alert('Correggi i seguenti errori: ' + response);
                        jQuery('#ajax-loading').hide();
                        jQuery('#publish').removeClass('button-primary-disabled');
                        return false;
                    }
                });
                return false;
            });
        });
    </script>
    ";
    }
}

Leave a Comment