Skip to content

Instantly share code, notes, and snippets.

@dnaber-de
Created October 21, 2015 17:05
Show Gist options
  • Save dnaber-de/dad36926d171c6a476ea to your computer and use it in GitHub Desktop.
Save dnaber-de/dad36926d171c6a476ea to your computer and use it in GitHub Desktop.
Play around with the wp.media API
myGlobalFrame = null;
/**
* @param wp wp namespace
* @param Backbone
* @param $ jQuery
*/
;( function( wp, Backbone, $ ) {
'use strict';
var myLibrary = wp.media.controller.FeaturedImage.extend(
{
defaults: _.defaults(
{
data : false,
multiple : false,
filterable : 'all',
selectedImageId : 0,
library : wp.media.query(
{
type : 'image',
uploadedTo: wp.media.view.settings.post.id,
}
)
},
wp.media.controller.Library.prototype.defaults
),
/**
* update the 'selected' attachment in the GUI
*/
updateSelection : function() {
var
selection = this.get( 'selection' ),
attachment;
if ( 1 > this.attributes.selectedImageId ) {
return;
}
attachment = wp.media.model.Attachment.get( this.attributes.selectedImageId );
attachment.fetch();
selection.reset( attachment ? [ attachment ] : [] );
}
}
);
var library = new myLibrary( {
title : 'Medienverwaltung',
//selectedImageId : 9
} );
var frame = myGlobalFrame = new wp.media.view.MediaFrame.Select(
{
title : 'Mediathek',
button : {
text : 'Bild Auswählen'
},
state : 'library',
states : [
//library
new wp.media.controller.Library({
//data : false,
//multiple : false,
filterable : 'all',
//selectedImageId : 0,
library : wp.media.query(
{
//type : 'image',
uploadedTo: wp.media.view.settings.post.id,
orderBy : 'menuOrder',
order : 'ASC'
}
)
})
]
}
);
frame.on(
'select',
function() {
var attachment = frame.state().get( 'selection' ).first();
library.attributes.selectedImageId = attachment.id;
}
);
$( document ).ready(
function() {
frame.open();
}
);
} )(
window.wp = window.wp || {},
window.Backbone = window.Backbone || {},
jQuery
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment