2

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');
    })
})  
6

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.

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.