5

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?

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.

1

Take a look at this http://sltaylor.co.uk/blog/hijacking-the-wordpress-media-library-overlay/

  • Hi mate, thanks for that. I've already tried SLT's plugin but its "Remove" upload functionality isn't clear to the user: it doesn't remove the preview image, it just clears the hidden input's value: // Wipe form values when remove checkboxes are checked $( '.slt-fs-button:first' ).parents( 'form' ).submit( function() { $( '.slt-fs-remove:checked' ).each( function() { $( this ).siblings( 'input.slt-fs-value' ).val( '' ); }); }); If you have any idea how to solve that, you'd be a lifesaver! – dunc Jul 2 '11 at 17:14
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-/, '');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.