Wordpress Media Uploader Use in a Plugin

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Wordpress Media Uploader Use in a Plugin

BenderisGreat
I am having some trouble using the Media Library in a plugin.  I have a meta box in the new post area, and it allows for selection of a picture. The issue is I dont see how to allow selection of more than one image, but more importantly the image is returned at full size.  The attachment var returns only the full size.  I am using this jQuery :

jQuery(document).ready(function($){
      var custom_uploader;
    $('#upload_image_button').click(function(e) {
          e.preventDefault();
          //If the uploader object has already been created, reopen the dialog
          if (custom_uploader) {
              custom_uploader.open();
              return;
          }
          //Extend the wp.media object
          custom_uploader = wp.media({
              title: 'Choose Image',
              button: {
                  text: 'Choose Image',
              },
              multiple: false,
             // If you pretent a function only for images you can limit the media elements only to images
             library : { type : 'image' }
         });

         //When close, grab the url of the image where needed
        custom_uploader.on('close', function() {
            attachment = custom_uploader.state().get('selection').first().toJSON();
            console.log(attachment); ## This only returns the full size url, no thumbnails.
            $('#upload_image').val(attachment.url);
            $('.custom_preview_image').attr("src", attachment.url);
        });

       //Open the uploader dialog
       custom_uploader.open();
   });

    $('#clear_images').click(function() {  
        var defaultImage = jQuery(this).parent().siblings('.custom_default_image').text();  
                $('#upload_image').val('');
        $('img.custom_preview_image').attr('src', '');
        return false;  
    });  
});

Is there something I am doing wrong?  Or is returning the full size media file the only available option?  (I followed most of this tutorial to get where I am guys -- http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/ )
Reply | Threaded
Open this post in threaded view
|

Re: Wordpress Media Uploader Use in a Plugin

Eric Andrew Lewis
In the options hash you pass to wp.media, set `multiple` to `true` for
multi-select.

In your on `close` event handler, you have full access to an attachment
model's attributes, including the `size` attribute, which includes various
image sizes.

Eric Andrew Lewis
ericandrewlewis.com
610.715.8560


On Mon, Jul 7, 2014 at 5:18 PM, BenderisGreat <[hidden email]>
wrote:

> I am having some trouble using the Media Library in a plugin.  I have a
> meta
> box in the new post area, and it allows for selection of a picture. The
> issue is I dont see how to allow selection of more than one image, but more
> importantly the image is returned at full size.  The attachment var returns
> only the full size.  I am using this jQuery :
>
> jQuery(document).ready(function($){
>       var custom_uploader;
>     $('#upload_image_button').click(function(e) {
>           e.preventDefault();
>           //If the uploader object has already been created, reopen the
> dialog
>           if (custom_uploader) {
>               custom_uploader.open();
>               return;
>           }
>           //Extend the wp.media object
>           custom_uploader = wp.media({
>               title: 'Choose Image',
>               button: {
>                   text: 'Choose Image',
>               },
>               multiple: false,
>              // If you pretent a function only for images you can limit the
> media elements only to images
>              library : { type : 'image' }
>          });
>
>          //When close, grab the url of the image where needed
>         custom_uploader.on('close', function() {
>             attachment =
> custom_uploader.state().get('selection').first().toJSON();
>             console.log(attachment); ## This only returns the full size
> url,
> no thumbnails.
>             $('#upload_image').val(attachment.url);
>             $('.custom_preview_image').attr("src", attachment.url);
>         });
>
>        //Open the uploader dialog
>        custom_uploader.open();
>    });
>
>     $('#clear_images').click(function() {
>         var defaultImage =
> jQuery(this).parent().siblings('.custom_default_image').text();
>                 $('#upload_image').val('');
>         $('img.custom_preview_image').attr('src', '');
>         return false;
>     });
> });
>
> Is there something I am doing wrong?  Or is returning the full size media
> file the only available option?  (I followed most of this tutorial to get
> where I am guys --
>
> http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/
> )
>
>
>
> --
> View this message in context:
> http://wordpress-hackers.1065353.n5.nabble.com/Wordpress-Media-Uploader-Use-in-a-Plugin-tp44096.html
> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers