I’ve inserted a WP 3.5 media uploader into a widget by running this JavaScript when a button is clicked on:
var frame = wp.media( {
title : 'Widget Uploader',
multiple : false,
library : { type : 'image' },
button : { text : 'Select Image' }
} );
frame.on( 'close', function() {
var attachments = frame.state().get( 'selection' ).toJSON();
imageWidget.render( widget_id, widget_id_string, attachments[0] );
} );
frame.open();
return false;
That gives me a modal that has the “Upload Files” and “Media Library” tabs, but I also want it to have the “Insert From URL” tab that you get when you click on the “Add Media” button while editing a post/page.
I’ve spent a couple hours digging around the web, reading through the source code and and watching Daryl Koopersmith’s presentation on the uploader’s backend, but haven’t been able to figure it out.
Can someone point me in the right direction? Is there a parameter I can pass to wp.media() to include it, or should I use one of the built-in views/models that includes it?
I’ve been digging through the source code for a similar reason; I’d like to add the “Attachment Display Settings” to the default “select” frame. As far as I can tell, this can’t be done by passing parameters to wp.media(), as we would all like. wp.media currently has the two frames (“post” and “select”), and the views that accompany them are preset.
The approach I’m looking at now is to extend media.view.mediaFrame to create a new frame (based on the “select” frame) that includes the parts of the view I need. If I get this working I’ll post the code.
EDIT:
Ian, I got the feature I wanted working and took some time to figure out yours. wp.media() is not quite as modular as it could be. It only accepts the values ‘select’ and ‘post’ for frame, with ‘select’ being the default, so you can’t create a new frame object. Instead, you need to extend one of the two frame objects (all this is in /wp-includes/js/media-views.js), which is also kind of clunky. Adding part of the UI is a several-step process. You could start with Select and add on, but for yours I chose to start with the code in the Post frame and take away the gallery stuff. Here’s my code, working but not heavily tested. Probably some room for streamlining, too.
This combines the code from wp.media.view.MediaFrame.Post with that from media.view.MediaFrame.Select, adjusting for the fact that this is executed outside of the original scope. The values for text are the various buttons, and you can reference your own localization object if you want. The ‘filterable’ value in the Library constructor (in createStates()) determines which media types will be supported.
Once you have extended the Select object with this approach, just instantiate it the same way you currently are and add your custom handler for when an image is selected. The Insert from Url might fire a different event than when picking from uploaded media. It would probably be better to instantiate your frame first, actually, and then extend it, so that any other media frames on the page would be unaffected, but I haven’t tried this.
Hope that helps-
From examinig the source code it seems the generic media modal does not support “Insert from URL”. The one way I was able to get that tab was to specify the “post” frame type:
The downside is the specified modal’s title is ignored.
The point is that that tab returns an external URL to be directly inserted in the post, while the widget is supposed to return a media ID. Basically the external image needs to be transloaded onto the server.
See how/whether the plugin Grab & Save does what you want. (via)