public
Last active

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
<?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;
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.