WordPress wp.media Featured Image ID

How can I hook into an existing wp.media object and grab the attachment ID when the “Set Featured Image” button is clicked?

The wp.media tutorials I’ve been looking at all seem to start by creating a new wp.media frame, but I just want to listen for events coming from an existing one (rendered by wp_editor() function), particularly the “Set Featured Image” event.

Related posts

Leave a Reply

1 comment

  1. Try using the wp.media.featuredImage object, and more specifically its frame() and get() methods:

    // on featured image selection...
    wp.media.featuredImage.frame().on( 'select', function(){
    
        // ...get the attachment ID
        var attachment_id = wp.media.featuredImage.get();
    
        console.log( attachment_id );
    
    });