Run script after clicking Set Featured Image in Media

I would like to run some script to get the featured image’s width and height after the user clicked ‘Use Featured Image’ button and use the data retrieved within the Post Edit screen. Is there such hook available for use in JavaScript? Otherwise, do you have other suggestions to achieve this?

Thanks for helping!

Edit : If there isn’t any hook, I tried using the code below but clicking ‘Set Featured Image’ wouldn’t trigger the event, presumably because it’s a modal pop-up? Any ideas?

jQuery("#set-post-thumbnail").on("click", function(){
    alert('clicked');

    jQuery(document).on("click", '.media-button-select', function(){
        alert('clicked button');
    })
})  

1 Answer
1

I think what you need is

(function(){
    var featuredImage = wp.media.featuredImage.frame();
    featuredImage.on('select', function(){
        var attachment = featuredImage.state().get('selection').first().toJSON();
        console.log(attachment);
    });
})();

The attachment object should have height and width properties.

Leave a Comment