[wp-hackers] get attachment ID when hijacking Media Library send_to_editor

Rafael Ehlers rafaehlers at gmail.com
Mon Mar 21 18:58:03 UTC 2011


You are saying something like this:

$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
$img = wp_get_attachment_thumb_url( $thumbnail_id );
echo "<img src=\"$img\">";

or

$thumb = wp_get_attachment_image( $thumbnail_id, 'thumbnail', true );

or

$attachments = get_children( array('post_parent' => $post->ID, 'post_type'
=> 'attachment', 'post_mime_type' => 'image') );
if ($attachments) {
$which = 'attach';
foreach ( $attachments as $attachment_id => $attachment ) {
 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail', true );
}
}

?

On Mon, Mar 21, 2011 at 3:52 PM, Steve Taylor <steve at sltaylor.co.uk> wrote:

> I'm using code from Brad Williams, Ozh Richard and Justin Tadlock's
> excellent new book on plugin development (pp. 84 - 89; boj-meta-box in
>
> http://media.wiley.com/product_ancillary/22/04709162/DOWNLOAD/Chapter%2004%20Code.zip
> ).
>
> The idea is to hijack the Media Library interface for choosing images
> or files for custom fields (whether in post meta boxes, custom
> settings pages, widgets, etc.).
>
> What I'm trying to do is to return the attachment ID instead of the
> URL. The standard send_to_editor function is the way the code I'm
> building on gets into things - replacing it with another function
> which takes the HTML on its way into the editor and grabs the URL from
> the image SRC.
>
> Getting the image ID is doable - it's included as part of a class in
> the HTML generated. But what about other files? The HTML generated is
> just a link, without any indication of the ID. You could take the URL
> and maybe match on the guid column in the posts table - but what if
> the Link field is empty? Only the file title is returned, and this
> isn't necessarily unique.
>
> Has anyone figured out how to get the ID of the selected file more
> directly?
>
> cheers,
>
> Steve Taylor
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list