Using the WordPress Media Library and returning the Attachment ID rather than URL?

I’m currently using some JS to pop open the WordPress Media Library in a thickbox container and return the URL of a selected image, then display it.

window.send_to_editor = function(html) {
    imgurl = jQuery('img',html).attr('src');
    jQuery('#upload_image-' + id_to_pass).val(imgurl);

    output_code = '<img class="media_display" src="' + imgurl + '" />';
    jQuery(output_code).appendTo('#media_display-' + id_to_pass);

    tb_remove();
}

I was wondering if it’s possible to return the attachment ID instead?

Read More

I would like to use the image’s description and caption fields, which I won’t be able to do (easily) if I can only access the image URL from the Media Library.

Related posts

Leave a Reply

2 comments

  1. It’s more a hack than a solution. You could use a regex to extract the attachment ID from the wp-image- class. Add the following snippet to your window.send_to_editor function.

    $classes = jQuery('img', html).attr('class');
    $id = $classes.replace(/(.*?)wp-image-/, '');