I'm very glad that there is such a dedicated website for WP.

My problem is data validation upon saving attachment fields. I have added many custom fields to attachment/media library dialog, most of them are marked as mandatory. (Ex. $form_fields["some_custom_field"]["required"] = true;) But this just adds the class, but no validation or checking is done on WP side, so I want to check them myself with functions attached to attachment_fields_to_save filter. I can check and so on, BUT THE QUESTION IS: How to validate these fields, so if validation do not pas - WP returns an error in it's regular style, just like in registration form if you fill bad email.

In registration process in register_post action you can do this $errors->add('some_error_code', "<strong>Klaida</strong>: Nenurodėte įmonės kodo"); and it will not save data and return to visitor an error. I want that in saving attachment form :D

  • @Tommix - please do not post answers in the question. Post it as an answer and then accept it (you will have to wait 24 hours to accept it...), thanks :) May 9 '12 at 10:07
  • I'll do that when it will allow me :)
    – Tommixoft
    May 9 '12 at 11:31


Well as it happens many times in my life - i found the answer myself. For people who struggles as me here the answer:

$post['errors']['field_name']['errors'][] = __('Error text here.');

if you do that - error text will be displayed at the bottom of that field. In my case this doesn't work because i'm using inline media uploader, but with standard WP media uploader (the popup version) it's working just fine.

BTW best article about custom fields is here: http://net.tutsplus.com/tutorials/wordpress/creating-custom-fields-for-attachments-in-wordpress/


I think this should help you: http://www.kevinleary.net/add-custom-meta-fields-media-attachments-wordpress/

  • Thanks for link but it doesn't help, there was nothing new to me. He did not do validation of data, which i need :)
    – Tommixoft
    May 9 '12 at 9:11

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.