[wp-trac] [WordPress Trac] #22868: broken compat attachment attributes save

WordPress Trac noreply at wordpress.org
Sat Dec 15 15:27:32 UTC 2012


#22868: broken compat attachment attributes save
--------------------------+------------------------------
 Reporter:  geminorum     |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Media         |     Version:  3.5
 Severity:  normal        |  Resolution:
 Keywords:                |
--------------------------+------------------------------

Comment (by crocro):

 I'm experiencing this problem as well : this occurs because of the
 checkboxes and jQuery's serializeArray function.

 Further explanation : when the checkbox goes unchecked, the serializeArray
 function (at line 4139 of media-views.js) won't take it into account (the
 function is designed to work like a browser : when the checkbox is
 unchecked, no info is sent via HTTP POST requests).

 As a consequence, when wordpress receives the AJAX request, the post
 parameters will look like that :

 {{{
 action  save-attachment-compat
 id      34872
 nonce   a1b5113007
 post_id 34865
 }}}


 Whereas it's expecting something like this to work (just like when a
 classic text input is empty) :

 {{{
 action  save-attachment-compat
 id      34872
 custom_checkbox_field   (empty)
 nonce   a1b5113007
 post_id 34865
 }}}

 Therefore, it won't even bother calling "attachment_fields_to_save" filter
 and returns an error right away because it can't guess which field has
 been edited.

 The best (and cleanest) patch I could imagine of would be changing
 serializeArray to a function that takes into account empty checkboxes as
 well.
 Sending the "custom_checkbox_field      (empty)" additional post parameter
 will be enough to make it work correctly.

 I can work on a patch but I'm not very familiar with Wordpress core code.
 However, editing the media-views.js file by adding :
 {{{
 jQuery('input[type=checkbox]:not(:checked)', this.$el).map(function() {
     data[this.name] = '';
 });
 }}}
 at line 4201 would be enough to make it work correctly.

 Notice for whom may use this patch : when you add a custom field thanks to
 "attachment_fields_to_edit" filter, and if you use 'input' => 'html'
 option for your custom field, you've to name it
 "attachments[$attachment->ID][name_of_your_custom_field]" (where
 $attachment->ID is the attachment ID and name_of_your_custom_field is a
 field name) in order for Wordpress to recognize it through the AJAX post
 request.

 Regards

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/22868#comment:5>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list