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
  • Save andrejIka/522302c00c8fa4b250f9b10159717acc to your computer and use it in GitHub Desktop.
Save andrejIka/522302c00c8fa4b250f9b10159717acc to your computer and use it in GitHub Desktop.

Revisions

  1. @dnaber-de dnaber-de created this gist Oct 21, 2015.
    94 changes: 94 additions & 0 deletions wp-media-playground.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    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
    );