0

I have a custom post type with a bunch of custom fields. I want to validate the meta box input with jQuery as soon as you press the Publish button. Here's what I have (I left out all my validation stuff):

$("input#publish").click(function(e){
    e.preventDefault();
    $("#ajax-loading").show();
    $('form#post').submit();
});

As you can see I am only interrupting the form submit at this point, nothing else going on. However, posts are now saved as drafts only and I can not manually set them to published (I have administrator priviliges). As soon as I remove above code, all is back to normal.

Is there any way to get this type of client side validation working properly without using plugins? I'm on Wordpress 3.6.1.

3
  • Check #1705 and #42013
    – brasofilo
    Commented Oct 23, 2013 at 14:38
  • Not really seeing the solution in those posts: the first one describes the same way I do it, and the second one mostly involves server side validation (I am looking for a client side solution). Although in that second post someone does mention the same problem I have with posts being saved as drafts, but no solution is given.
    – Marc
    Commented Oct 23, 2013 at 14:47
  • 1
    possible duplicate of don't publish custom post type post if a meta data field isn't valid
    – brasofilo
    Commented Oct 23, 2013 at 14:58

2 Answers 2

0

So here's what I ended up doing: I hide the publish button, replace it with my own publish button (one that does not submit the form) and when you click that button the validation script is executed. When there are no errors, a click event is registered on the original publish button. That way, no more drafts and problem solved.

0

I was having the same problem.

Do not use e.preventDefault();

simple return false or true at the end of your Javascript.

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.