Good day !

Please help to get binding on WordPress Media Upload elements. When i click on any of gallery item need a custom function to fire up.

enter image description here

There is a BackBone collection called "Attachment" and event "selection:single", but i can't understand how to access them.

Thanks !

God bless the solver ! :)


1 Answer 1

(function ($) {
    "use strict";

    $(function () {

        var button = $('.upload-image');

        button.click(function (e) {


            // If the media frame already exists, reopen it.
            if (file_frame) {

            var btn = $(this),
                media = wp.media;

            // Create the media frame.
            var file_frame = media.frames.file_frame = media({
                title: jQuery(this).data('uploader_title'),
                button: {
                    text: jQuery(this).data('uploader_button_text')
                library: {
                    type: 'image'
                multiple: false

            // Finally, open the modal

            var selection = media.frame.state().get('selection');
            // The sidebar boxes get deleted and recreated on each select - hack into this to do the same.
            selection.on( 'selection:single', function ( event ) {
            } );

            selection.on( 'selection:unsingle', function ( event ) {
            } );



NOTICE: "selection:unsingle" event will fire up twice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.