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.
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.
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.
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 );
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With