5

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();
   }
}
6
  • If you pack the result into a nice function (or class), I'd ask you if you want to post it to our WPSE Plugin Repository :)
    – kaiser
    Commented Sep 1, 2011 at 12:27
  • i'll release the plugin in the main repository, if you also want this, that's ok
    – Michele
    Commented Sep 1, 2011 at 14:30
  • Well, [here](wordpress.stackexchange.com/questions/18459/wpse-plugin-repository-the-plugins-that-came-out-of-wp-answers) goes the link. Hm... linking not working. Guess you have to copy/paste the link. Sry :)
    – kaiser
    Commented Sep 1, 2011 at 14:36
  • 1
    i'll check it once i fix the function and publish the plugins....thanks
    – Michele
    Commented Sep 1, 2011 at 15:39
  • Btw: A much nicer way then to alert(); on fail is to simply use console.log(); your error so you see it in ex. your firebug or chrome dev bar console.
    – kaiser
    Commented Sep 1, 2011 at 18:05

2 Answers 2

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>
    ";
    }
}
2
  • it contains a problem....the post is not published in the standard way. For example after the submit the buttons says "publish" and not "update". And the message says the article is edited and not published...do you know how to fix it?
    – Michele
    Commented Sep 1, 2011 at 15:08
  • I'll take a look when i get home.
    – Bainternet
    Commented Sep 1, 2011 at 17:51
0

I know this post is old, but here is a nice plugin that has a ton of different posting requirements options - WyPiekacz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.