To expand: I’d like to utilize the same Modal code/appearance ( as used in wp.media.Modal, wp.media.FocusManager ) to open a modal of my own custom dialog, not the Media Editor. In the past, I’ve used thickbox for this sort of thing, but wp.media.Modal appears to be the way of the future for modals — Not to mention it looks cool.
I’ve poked at the JS source a bit and have come to a couple possible solutions:
- “Borrow” the code media-views.js and use it in my plugin.
- “Extend” wp.media.Modal ( it is a Backbone View, after all ).
- Create a custom implementation, jQueryUI, etc.
- Just give up and use thickbox.
Borrowing seems slightly less dangerous than using wp.media.Model.extend({}), but wasteful. I’m not a big fan of jQueryUI’s modals, but it would get the job done. At the same time, I could do a custom implementation of modals ( or base it off another lib ).
Feels like I’m missing something obvious: Has anyone else pulled this off or is the new media library modal code “too new” to allow for reuse?
Late answer & edit. Disclaimer: The following is no copy & paste-togo code.
Rough Sketch
As I never tried misusing the media modal for anything else, here’s a short overview, sketched by breaking out a part from a project I’m currently on. It’s not a ready to go example, but it should bring you close enough. Just read the comments carefully and implement the following PHP in your objects.
PHP
In our constructor we register our scripts, add meta boxes that hold info and a media button, filter in additional MIME Types (for e.g. ZIP) and care about saving the additional data:
Make sure that you abort if you don’t need that script on a particular page. This saves memory, request time and helps keeping your installation clean.
Then we add the meta box. Inside the function, we can rely on the
$post
objectspost_type
property, which will be set for new posts as well. As we already registered the callbacks in the constructor to the appropriate contextual hooks, we can simply take whatever post type comes along.Additional MIME Types
Simply throw in an array that either overrides or adds to the default MIME types of the Media Modal. You can as well add or override other settings. Just
var_dump( $settings );
to see what the callback provides. Also make sure we don’t intercept on the wrong post type.Render the content
Save the data
Finally we make sure our data gets saved properly and will be checked. Use all the
esc_*()
functions, typecasting, nonces and what not.Final note, before heading over to the JS example: The code is broken out of a current project. So it will – as already mentioned – not work by default! It’s only a guide and nothing else.
Javascript
The javascript itself is pretty straight forward. Not. But as you can see, I’m injecting both the jQuery as the custom localized script object into the function. From there on, you’ll have to add whatever logic you might need. The basic surroundings for different states and callbacks are provided and
console.log()
s are present.Tutorials
Dominik Schilling – the author of the WP 3.5 media manager – has written a set of demos for media modals. You can view them on GitHub.