• Hello all.

    I have a front-end form where a user can upload photos. The photos are added as attachments to the post generated by the submission of the front-end form.

    The posts would then be pending admin approval. While reviewing the post I would like to get the attachments from said post and feed them into a slider plugin’s meta box in the back end.

    Here’s the question:

    Is there any way to limit the media files that show in the “media” window to those that have already been added to the post? When I open the media window, I’d like to only see the photos that have already been attached to the post.

    I have found the code below:

     //Chame the selector to fit your code
      jQuery('#manage-gallery-button').click(function(e) {
    
             e.preventDefault();
             var frame = wp.media({
                           title : 'Pick the images for the gallery of this entry',
                           frame: 'select',
                           multiple : true,
                           library : {
                                        type : 'image',
                                        //HERE IS THE MAGIC. Set your own post ID var
                                        uploadedTo : wp.media.view.settings.post.id
                                      },
                           button : { text : 'Insert' }
                       });
    
                       frame.on('close',function() {
                          // get selections and save to hidden input plus other AJAX stuff etc.
                          var selection = frame.state().get('selection');
                          var gallery_ids = new Array();
                          var my_index = 0;
                          selection.each(function(attachment) {
                             gallery_ids[my_index] = attachment['id'];
                             my_index++;
                          });
                          var ids = gallery_ids.join(",");
                          //Store ids in my hidden input
                          jQuery('#gallery-ids').val(ids);
                          Refresh_Gallery(ids);
                       });
    
                      frame.on('open',function() {
                        //Preselect attachements from my hidden input
                        var selection = frame.state().get('selection');
                        ids = jQuery('#gallery-ids').val().split(',');
                        ids.forEach(function(id) {
                          attachment = wp.media.attachment(id);
                          attachment.fetch();
                          selection.add( attachment ? [ attachment ] : [] );
                        });
    
                      });
    
                    frame.open();
     });

    And modified it to the selector of the media button in the slider’s meta box. The id for that box is #wp-igsp-gallery-imgs

    When I add the function I get a parse error on the second line of the code. Before and after modifying it I still get the same error.

    Something might have changed in the core as this code is from a 2013 post

    Thank you for your time.

  • The topic ‘Limit media to current post attachments’ is closed to new replies.