I’m trying to extend the media modal, but I cant’ find any documentation / tutorials about it. I’m not a master of backbone too 😉
I want to add a select box for each taxonomy that is attached to the attachment post type. At the moment only one select box is shown.
So this is what I came up with. It works great except that it replaces the default toolbar.
Code
/**
* Extended Filters dropdown with taxonomy term selection values
*/
jQuery.each(mediaTaxonomies,function(key,label){
media.view.AttachmentFilters[key] = media.view.AttachmentFilters.extend({
className: key,
createFilters: function() {
var filters = {};
_.each( mediaTerms[key] || {}, function( term ) {
var query = {};
query[key] = {
taxonomy: key,
term_id: parseInt( term.id, 10 ),
term_slug: term.slug
};
filters[ term.slug ] = {
text: term.label,
props: query
};
});
this.filters = filters;
}
});
/**
* Replace the media-toolbar with our own
*/
media.view.AttachmentsBrowser = media.view.AttachmentsBrowser.extend({
createToolbar: function() {
media.model.Query.defaultArgs.filterSource = 'filter-media-taxonomies';
this.toolbar = new media.view.Toolbar({
controller: this.controller
});
this.views.add( this.toolbar );
this.toolbar.set( 'terms', new media.view.AttachmentFilters[key]({
controller: this.controller,
model: this.collection.props,
priority: -80
}).render() );
}
});
});
Original
My result
What I want
The wonderful world of Backbone.js and WP (of which I know barely anything).
I think the problem is you are just calling the same default
media.view
, instead I believe you need to initialize a new one.For example:
Would give you something like below (I did not do any thorough error checking but it does work).
You should also consider doing this with
media.view.AttachmentFilters
and anything custom with regards towindow.wp.media;
.