Is there a native wp.media way to detect who “opened” wp.media.frame

I added few custom fields via attachment_fields_to_edit and would like to show them only when certain theme or page builder image option opens the wp.media.iframe.

Currently they are visible regardless what button opens the uploader for example post “Add media” would not have any use for this option

enter image description here

So I am kinda trying to find a “hook” that would let me know what option is currently using the uploader.

What I am trying to stay away from is doc click events

$(document).on("click", '.some-button-class', function() {
    //  .some-button-class is clicked
});

and would rather use something like this if possible,

wp.media.frame.on( 'open', function(el) {

    //  some-button-class has opened
});

Is there a way to do this?

1 Answer
1

I think I got it ,

if (wp.media) {
    wp.media.view.Modal.prototype.on('open', function(data) {
        console.log(wp.media.frame.modal.clickedOpenerEl);
    });
}

let me know if there is better solution.

Leave a Comment