The code below resides in my theme’s functions.php and creates a custom upload icon on top of the WordPress content editor, alongside the default upload icon.
Images uploaded via this icon get a special flag in wp_postmeta called _imageTop to differentiate them from standard attached images (to allow me to do special things to them as a separate collection of “attached” images).
I’ve got 3 problems occuring that I’m sure are simple fixes.
1) The attachment_fields_to_save filter does not get applied, even though I can see the echo’d text inside the media-upload.php window. I know this because the _imageTop meta only gets written to the database when I comment out the if(isset) check
2) After the images have been uploaded, I have to click on “Save all changes” to get the _imageTop meta to save to the database. Ideally, I’d like the data saved immediately after the upload, without having to click “Save all changes”. This is probably due to the fact that the attachment_fields_to_save handler only fires on the “Save all changes” hook. Nevertheless, I’d like to figure out how to get it to fire when the images have been uploaded.
3) I want to remove the “insert into post” link from the screen.
//Upload custom images
function my_customImages($initcontext)
{
global $post;
?>
<script type="text/javascript">
jQuery(document).ready(function() {
var fileInput = '';
jQuery('#wpe-uploadAttachments').click(function() {
fileInput = jQuery(this).prev('input');
formfield = jQuery('#upload_image').attr('name');
post_id = jQuery('#post_ID').val();
tb_show('my Product Images', 'media-upload.php?post_id='+post_id+'&type=image&my_idCustomAttachment=true&TB_iframe=true');
return false;
});
});
</script>
<?php
return $initcontext. '<input type="hidden" id="post_ID" value="'. $post->ID .'" /> Product Images:<a id="wpe-uploadAttachments" href="javascript:;" class="mceButton mceButtonEnabled" onmousedown="return false;" onclick="return false;" title="Click here to upload your product images for this post"><img src="'.get_bloginfo('template_directory') .'/img/upload-icon.gif"" /></a>';
}
add_filter('media_buttons_context', 'my_customImages');
function my_image_attachment_fields_to_save($post, $attachment) {
update_post_meta($post['ID'], '_imageTop', true);
return $post;
}
if(isset($_GET['my_idCustomAttachment'])){
echo "This is true";
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null , 2);
}
Try to exchange $_GET with $_POST and see if it works. If so, you need to check for $_POST as well.
If it does not get fired, the only thing you can do is to fire it your own, e.g. call the hook function directly. If that is not possible (e.g. you have not code that is executed so you can’t execute your additional code then), you need to look for another action you can hook into, e.g. one that get’s fired after the upload. There probably is some hook when the image get’s saved in the media library. You could then store your custom post field as well (!) into the database. Just to have it saved. I dunno if WP is able to handle that with it’s revision system, so you need to try.
I do not know how this can be done from the top of my head, you should check the core code if there is something you can override with/by a hook.