Created
January 30, 2011 02:46
-
-
Save jaredwilli/802465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom Metabox for uploading post image attachments | |
* Created by Jared Williams - http://new2wp.com | |
*/ | |
add_action( 'admin_init', 'add_attachment' ); | |
add_action( 'save_post', 'update_attachment' ); | |
add_action( 'post_edit_form_tag', 'form_multipart_encoding' ); | |
//add_action( 'manage_posts_custom_column', 'product_custom_columns' ); | |
//add_filter( 'manage_edit-product_columns', 'product_edit_columns' ); | |
/* Add the metabox to the post type edit page */ | |
function add_attachment(){ | |
add_action( 'admin_head', 'metabox_styles' ); | |
add_action( 'admin_head', 'metabox_scripts' ); | |
add_meta_box( 'img-uploads', __( 'Add Attachments' ), 'add_attachments', 'post', 'normal', 'high' ); | |
} | |
/* Add the form multipart attribute for file uploads */ | |
function form_multipart_encoding() { | |
echo ' enctype="multipart/form-data"'; | |
} | |
/* Create the metabox and display any attachments as previews */ | |
function add_attachments(){ | |
global $post; | |
$attachments = get_posts( array( 'post_type' => 'attachment', 'post_parent' => $post->ID )); ?> | |
<div id="img_uploads"> | |
<?php | |
if( $attachments ) { | |
foreach( $attachments as $attachment ) { | |
$a_image = wp_get_attachment_url( $attachment->ID ); | |
var_dump($a_image); ?><br><br><?php | |
$t = wp_get_attachment_image_src( $attachment->ID, thumbnail ); | |
var_dump($t); ?> | |
<div id="att-<?php echo $attachment->ID; ?>" class="attchmt"> | |
<div class="prevImage"><?php the_attachment_link( $attachment->ID, false, array( 32, 32 )); ?></div> | |
<p><label><?php _e( 'Image:' );?><br /><input type="file" name="a_image" /></label></p> | |
<p><label><?php _e( 'Url:' );?><br /><input type="text" name="a_url" disabled="disabled" value="<?php echo $a_image; ?>" /></label></p> | |
<p><label><?php _e( 'Title:' );?><br /> | |
<input type="text" name="a_title" value="<?php echo apply_filters( 'the_title', $attachment->post_title ); ?>" /></label> | |
</p> | |
<label><?php _e( 'Description:' );?><br /> | |
<input type="text" name="a_desc" value="<?php echo apply_filters( 'the_content', $attachment->post_content ); ?>" /></label> | |
<a class="remImage" href="#"><?php _e( 'Remove' );?></a> | |
<input type="hidden" id="attID" name="attID" value="<?php echo $attachment->ID; ?>" /> | |
</div> | |
<?php | |
} | |
} ?> | |
<h4><a href="#" class="addImage"><?php _e( 'Add Another Image' ); ?></a></h4> | |
</div> | |
<?php | |
} | |
function delete_att( $attID ) { | |
return wp_delete_attachment( $attID, true ); | |
} | |
function metabox_scripts() { ?> | |
<script src="http://code.jquery.com/jquery-1.4.5.min.js"></script> | |
<script type="text/javascript"> | |
jQuery(function() { | |
function delete_att( attID ) { | |
var div = jQuery('#img_uploads'), | |
msg = $('div').html('<strong>Attachment Deleted!</strong>').fadeIn().delay(200).fadeOut().appendTo('div'); | |
return '<?php wp_delete_attachment( ' + attID + ', true ); ?>', msg; | |
} | |
var imgDiv = jQuery('#img_uploads'), | |
size = jQuery('#img_uploads .attchmt').size() + 1; | |
jQuery('.addImage').live('click', function() { | |
jQuery('<div id="att-' + size + '" class="attchmt"><p><label><?php _e( 'Title:' );?><br /><input type="text" name="a_title" value="" /></label></p><p><label><?php _e( 'Description:' ); ?><br /><input type="text" name="a_desc" value="" /></label> <a class="remImage" href="#"><?php _e( 'Remove' ); ?></a></p><p><label><?php _e( 'Image:' );?><br /><input type="file" name="a_image" /></label></p></div>') | |
.appendTo(imgDiv); | |
size++; | |
return false; | |
}); | |
jQuery('.remImage').live('click', function() { | |
if( size > 1 ) { | |
var postID = jQuery('#attID').val() | |
delete_att( postID ); | |
//jQuery(this).parents('.attchmt').find('#attID'); | |
jQuery(this).parents('.attchmt').detach(); | |
size--; | |
} | |
return false; | |
}); | |
}); | |
</script> | |
<?php | |
} | |
function metabox_styles() { ?> | |
<style type="text/css"> | |
.attchmt { border-bottom:31px solid #666; margin:0 0 10px 0; } | |
.attchmt p { width:100px; } | |
.prevImage { float:right; } | |
</style> | |
<?php | |
} | |
function update_attachment() { | |
global $post; | |
wp_update_attachment_metadata( $post->ID, $_POST['a_image'] ); | |
if( !empty( $_FILES['a_image']['name'] )) { | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
$override['action'] = 'editpost'; | |
$file = wp_handle_upload( $_FILES['a_image'], $override ); | |
if ( isset( $file['error'] )) { | |
return new WP_Error( 'upload_error', $file['error'] ); | |
} | |
$file_type = wp_check_filetype($_FILES['a_image']['name'], array( | |
'jpg|jpeg' => 'image/jpeg', | |
'gif' => 'image/gif', | |
'png' => 'image/png', | |
)); | |
if( $file_type['type'] ) { | |
$name_parts = pathinfo( $file['file'] ); | |
$name = $file['filename']; | |
$type = $file['type']; | |
$title = $_POST['a_title'] ? $_POST['a_title'] : $name; | |
$content = $_POST['a_desc']; | |
$post_id = $post->ID; | |
$attachment = array( | |
'post_title' => $title, | |
'post_type' => 'attachment', | |
'post_content' => $content, | |
'post_parent' => $post_id, | |
'post_mime_type' => $type, | |
'guid' => $file['url'], | |
); | |
foreach( get_intermediate_image_sizes() as $s ) { | |
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true ); | |
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options | |
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options | |
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options | |
} | |
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); | |
foreach( $sizes as $size => $size_data ) { | |
$resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] ); | |
if ( $resized ) | |
$metadata['sizes'][$size] = $resized; | |
} | |
$attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $post_id - for post_thumbnails*/); | |
if ( !is_wp_error( $id )) { | |
$attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] ); | |
wp_update_attachment_metadata( $attach_id, $attach_meta ); | |
} | |
update_post_meta( $post->ID, 'a_image', $images ); | |
} | |
} | |
} | |
function product_edit_columns( $columns ) { | |
$columns = array( | |
'cb' => '<input type="checkbox" />', | |
'a_image' => __( 'Image' ), | |
); | |
return $columns; | |
} | |
function product_custom_columns( $column ) { | |
global $attachment; | |
switch ( $column ) { | |
case 'a_image' : | |
the_attachment_link( $attachment->ID, false, array( 70, 70 )); | |
break; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment