Skip to content

Instantly share code, notes, and snippets.

@bitfade
Last active April 3, 2017 07:58
Show Gist options
  • Save bitfade/4460948 to your computer and use it in GitHub Desktop.
Save bitfade/4460948 to your computer and use it in GitHub Desktop.

Revisions

  1. bitfade revised this gist Jan 5, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ function select() {
    }

    function reset() {
    // called when user media uploaded is closed via "close" button / "ESC" key
    // called when dialog is closed by "close" button / "ESC" key
    // use the following line unbind the event
    // workflow.off("select");
    }
  2. bitfade revised this gist Jan 5, 2013. No changes.
  3. bitfade created this gist Jan 5, 2013.
    36 changes: 36 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var workflow = wp.media({
    title: 'Select the images',
    // use multiple: false to disable multiple selection
    multiple: 'add',
    button: {
    text: 'Add selected images'
    },
    library: {
    type: 'image'
    }
    });

    function getAttachment(attachment) {
    attachment = attachment.toJSON();
    return {id:attachment.id,url:attachment.url};
    }

    function select() {
    // use this to unbind the event
    // workflow.off("select");
    // get all selected images (id/url attributes only)
    console.log(workflow.state().get('selection').map(getAttachment));
    }

    function reset() {
    // called when user media uploaded is closed via "close" button / "ESC" key
    // use the following line unbind the event
    // workflow.off("select");
    }

    // bind event handlers
    workflow.on("select",select);
    workflow.on("escape",reset);

    // open the dialog
    workflow.open();