Skip to content

Instantly share code, notes, and snippets.

@andrejIka
Forked from dnaber-de/wp-media-playground.js
Created August 25, 2020 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrejIka/522302c00c8fa4b250f9b10159717acc to your computer and use it in GitHub Desktop.
Save andrejIka/522302c00c8fa4b250f9b10159717acc 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