cwdg
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: WordPress Inject element into wp.element.createElementThis is dealing with code that is new and there is no documentation or plugins that do what I am trying to accomplish as this is for Gutenberg. If there was, I would have taken that direction. I cant find any documentation on wp.element.createElement. I programmed the code above from the rough example provided by the gutenberg plugin documentation and hours of research and testing.
I provided the creation of the element and the collection of the data as well as the loop I am using. I was simply looking for direction, documentation or description of what needs to be done. If someone provides some small code examples I would be happier but from where I am sitting there is no solution in site.
In the code I left comments of what I was looking to accomplish. Simply put I need to insert more elements into the one above the loop. Is there a proper method for inserting elements of that type such as creating an object, array or function?
- This reply was modified 7 years, 7 months ago by cwdg.
I was able to fix the issue using the following code to override the default function.
add_action( 'admin_print_footer_scripts', 'mediaArrayFix', 11 ); function mediaArrayFix() { // Make sure the media-views script has been enqueued. if(!did_action( 'wp_enqueue_media' ) ){ return; } ?> <script> wp.media.view.AttachmentCompat.prototype.save = function( event ) { var data = {}; if ( event ) { event.preventDefault(); } _.each( this.$el.serializeArray(), function( pair ) { if ( /\[\]$/.test( pair.name ) ) { if ( undefined === data[ pair.name ] ) { data[ pair.name ] = []; } data[ pair.name ].push( pair.value ); }else{ data[ pair.name ] = pair.value; } }); this.controller.trigger( 'attachment:compat:waiting', ['waiting'] ); this.model.saveCompat( data ).always( _.bind( this.postSave, this ) ); }; </script> <?php }I also created a commit to github to add this minor change to the function to allow for members to use array values.
I was able to solve the issue by replacing your code with the following. It resolves all the issues with both Multi-Select boxes and Checkbox Groups.
_.each( this.$el.serializeArray(), function( pair ) { if ( /\[\]$/.test( pair.name ) ) { if ( undefined === data[ pair.name ] ) { data[ pair.name ] = []; } data[ pair.name ].push( pair.value ); } else { data[ pair.name ] = pair.value; } });I also created a commit on github
https://github.com/WordPress/WordPress/commit/35ea1fa5c579d76d08798003463fe92fb9a23519- This reply was modified 7 years, 8 months ago by cwdg.
I finally found the stupid lines of code causing this issue and it is in the WordPress JS.
Open wp-includes/js/media-views.js
Line: 8353_.each( this.$el.serializeArray(), function( pair ) { data[ pair.name ] = pair.value; });This section removes the ability for an array to be submitted. It simply removes anything that is stored in an array format. Thus removing the ability to use multi select fields and checkbox arrays.
Now the fun part is trying to figure out how I am going to remove those without manually editing WP core files.
- This reply was modified 7 years, 8 months ago by cwdg.
Obviously you have not tried this yourself as I did. A var dump only returns an array the last item. If you watch the admin-ajax post variables in console you will see it does not send it to admin-ajax. It is an issue in the way WordPress collects the form variables.
Array ( [id] => 1299 [nonce] => 85b1a39cc9 [post_id] => 0 [attachments] => Array ( [1299] => Array ( [menu_order] => 0 ) ) [action] => save-attachment-comp [test] => Array ( [testing] => Array ( [0] => 4 ) ) )- This reply was modified 7 years, 8 months ago by cwdg.
Please keep in mind this is gallery Grid not List.
I got rid of ACF for the purposes of testing and to remove any thoughts of their system being the problem. I created the filters and added the following code to the media form.
<ul class="no-bullet"> <li><label><input type="checkbox" name="Test[Testing][]" value="1"> Option 1</label></li> <li><label><input type="checkbox" name="Test[Testing][]" value="2"> Option 2</label></li> <li><label><input type="checkbox" name="Test[Testing][]" value="3"> Option 3</label></li> <li><label><input type="checkbox" name="Test[Testing][]" value="4"> Option 4</label></li> </ul>I checked all the boxes and then looked at the headers sent by the last click.
Test[Testing][]: 4This means that the code handling the submission of the form is not properly collecting the array.
I also downloaded a couple of plugins made by others that add tags to attachments and they are all having the same issue.
As I said Im working in the Media system and it is ignoring multiselect and checkbox groups. The code below is what I have tested with and both systems return the last value selected in the group/select. I am guessing that there is a flaw in the click function associated with the admin-ajax. I wish it was as easy as you posted.
<input type="checkbox" name="acf[field_5bc3f249f009c][]" value="1"> Artwork<br> <input type="checkbox" name="acf[field_5bc3f249f009c][]" value="2"> Colors<br> <input type="checkbox" name="acf[field_5bc3f249f009c][]" value="3"> Samples<br<select name="acf[field_5bc3f249f009c]" multiple> <option value="1">Artwork</option> <option value="2">Color</option> <option value="3">Samples</option> </select>