HWD
HWD

Reputation: 1629

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.

Upvotes: 5

Views: 1604

Answers (1)

d79
d79

Reputation: 3848

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 );

});

Upvotes: 5

Related Questions