-2

I need to dont save new post from custom post type if any meta box empty.

So we must check the meta if empty, we mus show me

" You must fill all meta box value before save post"

else if all meta box was fill, we can save post.

How can do that

  • Any code, research or something that you have tried? At least the metabox and custom field code would be useful to may be answer the question. – cybmeta Jan 22 '15 at 8:07
  • i need to save post if all custom meta box was inter :) – Khalid Ahmed Jan 22 '15 at 8:49
  • You can hook the wp_insert_post_data filter and add a default for that field if you wish? Or use some JS to detect when the Save/Publish button is clicked and prevent it working if a certain field is empty? – David Gard Jan 22 '15 at 14:54
  • can you get me example or edit in my code above ? – Khalid Ahmed Jan 22 '15 at 15:02
0

The easiest solution is to use jQuery. Add the below code to any JS file that you include in the admin area.

As you haven't given very much information above I don't know a couple of things, so you'll need to amend this example slightly -

  • Change your-post-type to the slug of the post type for which this metabox exists. If it's the default Post then it'll just be post.

  • Change '#field-name' to the correct ID of your field. To find this out, check the source of your page.

The code -

jQuery(document).ready(function($){

    var post_type = $('#post_type').val();      

    if(post_type === 'your-post-type'){

        $('input#publish').on('click', function(e){

            var myVal = $('#field-name').val();

            if(yourMetaField === ''){
                e.preventDefault();
                alert('You must fill all meta box value before save post');
            }

        });

    }

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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