Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
HWD Avatar asked May 06 '15 19:05

HWD


People also ask

How do I find the featured image ID in WordPress?

Using the built-in WordPress function get_the_post_thumbnail() to display the featured image of a post in a <img> tag. This is the easiest way to display a post's featured image in a WordPress loop.

Where are WordPress featured images stored?

The specific folder where the image files are stored in WordPress is called the uploads folder located inside the /wp-content/ folder. Inside the uploads folder, your media files are stored by year and month folders. Additionally, you'll also see folders created by your WordPress plugins to save other uploads.


1 Answers

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

});
like image 178
d79 Avatar answered Sep 30 '22 18:09

d79