The code below allows me to retrieve the image string, when Insert to Post
has been pressed, but what would be the correct way to retrieve the ID of the image? So I can retrieve all information through wp_get_attachment_image_src
?
jQuery(document).ready(function() {
var form_field;
var upload_field_ID = '';
jQuery( '.upload_image_button' ).click( function() {
jQuery( 'html').addClass( 'Image' );
upload_field_ID = jQuery(this).prev('input');
form_field = upload_field_ID.attr( 'name' );
tb_show( '', 'media-upload.php?type=image&TB_iframe=true' );
return false;
});
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function( html ) {
if (form_field) {
var image_url = jQuery( 'img', html ).attr( 'src' );
upload_field_ID.val( image_url );
tb_remove();
jQuery( 'html').removeClass( 'Image' );
} else {
window.original_send_to_editor( html );
}
}
});
You can also add in a filter that can add the ID as a html5 data attribute to the returned HTML fragment from send_to_editor.
Then in your javascript handler for window.send_to_editor;
You can do the same thing for media uploads as well.
Above is an example mark-up of that button. Notice how
name
andid
fields both contain the image ID.So with jQuery you can read that.
check it out works find for me.
the thing is that the function tb_show need to know to who pitch the return. So you just go get the post_id and put it in the url pass to tb_show, if its a new one then you pass it like its a new one.
so i show you my solution just so you can understand better but hey.. easy when you know how