WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#33331 closed defect (bug) (duplicate)

Multiple input field values in Media Modal do not save collectively

Reported by: LindsayBSC Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: javascript Cc:

Description

Problem:
When adding custom fields that would typically pass an array value to $_POST in the WordPress media upload modal window such as multiple checkboxes or multiple select inputs, the 'save-attachment-compat' action that was triggered "onblur" of each field would only pass that specific value of that input and not the collective values of a multiple-input field.

For example:
A collection of checkboxes would send the input value for a checkbox group for each box checked individually even if the name attribute is set up to send it's value as an array using the format name="field1[]"

See results of $_POST:
http://cl.ly/image/0b2F3U3l1U22/Screen%20Shot%202015-08-10%20at%205.08.54%20PM.png

Replicate:
Use this code to see the problem replicated https://gist.github.com/Lindsayanng/dee0488120d436e5b43b

Fix:
In the 'save' function of 'AttachmentCompat' I was able to check if an input field has a name that ends with [] and if it does it combines all values from that collection of inputs in to a javascript array object to be passed to the $_POST variable at save time in the 'wp_ajax_save-attachment-compat' action.

For Developers:
To utilize multiple input field values your input must use [] in it's naming convention as is expected when passing an array of values from a form via PHP $_POST.

To store the value as a serialized array in your postmeta table you must use .serialize() on your $_POST value before doing update_post_meta

Attachments (1)

b5f7e356884b2cffdd859c11499225d0bd4a821b.diff (2.2 KB) - added by LindsayBSC 5 years ago.
media-views.js file patch

Download all attachments as: .zip

Change History (5)

@LindsayBSC
5 years ago

media-views.js file patch

#1 follow-up: @ocean90
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version 4.2.4 deleted

Hello LindsayBSC, welcome to Trac!

Thanks for your detailed report and the patch. Looking at your patch and it seems like the indentation went wrong and you accidentally removed some lines.

We're already tracking this issue in #28053. Would you like to refresh your patch and upload it to #28053?
Please note, that media-view.js is auto-generated and the source for the compat code is in trunk/src/wp-includes/js/media/views/attachment-compat.js.

#2 in reply to: ↑ 1 @LindsayBSC
5 years ago

Ah yep. You're right. My patch definitely did go wrong.

Also, I feel like I searched for DAYS! I can't believe I did not come across that ticket! I am aware that media-views.js is auto-generated. I don't have the attachment-compat.js in the release version of WP. Should I use the trunk file and submit it as a patch to trunk instead?

Replying to ocean90:

Hello LindsayBSC, welcome to Trac!

Thanks for your detailed report and the patch. Looking at your patch and it seems like the indentation went wrong and you accidentally removed some lines.

We're already tracking this issue in #28053. Would you like to refresh your patch and upload it to #28053?
Please note, that media-view.js is auto-generated and the source for the compat code is in trunk/src/wp-includes/js/media/views/attachment-compat.js.

This ticket was mentioned in Slack in #core by lindsaymac. View the logs.


5 years ago

This ticket was mentioned in Slack in #core by lindsaymac. View the logs.


5 years ago

Note: See TracTickets for help on using tickets.