Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created November 17, 2014 10:04
Show Gist options
  • Select an option

  • Save vishalbasnet23/4874acd52606b1c14adf to your computer and use it in GitHub Desktop.

Select an option

Save vishalbasnet23/4874acd52606b1c14adf to your computer and use it in GitHub Desktop.
Add Custom Image Upload Meta Field WordPress.
<?php
/*************************************************************************/
/* Adding Images Meta Boxes */
/*************************************************************************/
add_action('add_meta_boxes', 'add_images_meta_boxes');
function add_images_meta_boxes() {
// Define the images attachment for gallery
add_meta_box(
'code-gallery-attachment',
'Custom Attachment',
'code_gallery_attachment',
'code_gallery',
'side'
);
}
/********************************************************************************/
/* Displaying image upload Fields */
/********************************************************************************/
//static $i = 1;
$k = 1;
$j = 1;
$m = 1;
function code_gallery_attachment() {
global $post;
$i =1;
wp_nonce_field(plugin_basename(__FILE__), 'code_gallery_attachment_nonce');
$html = '<p class="description">';
$html .= 'Upload your images here.';
$html .= '</p>';
// $html .= '<input type="hidden" id="upload_image" name="upload_gallery_image" value="'.get_post_meta($post->ID,'code_gallery_images',true).'" size="25">';
$html .= '<input type="hidden" id="upload_image" name="upload_gallery_image" value="" size="25">';
$html .=' <input id="upload_image_button" class="button" type="button" value="Upload Image" />' ;
echo $html;
$gallery_images = get_post_meta($post->ID,'code_gallery_images');
?>
<?php if( sizeof($gallery_images) > 0 ){?>
<div id="togglediv">
<?php if($gallery_images[0]!= NULL){foreach ($gallery_images[0] as $key=>$value ) {
if($value != NULL){?>
<input class="images" type="text" id="firstimage<?php echo $i?>" name="code_gallery_attachment[]"value="<?php echo $value;?>" />
<div class="editthumb" id="imagediv<?php echo $i;?>">
<img src="<?php echo $value;?>"><span class="removebtn"><a id="removebutton<?php echo $k++;?>" onClick="removeImage(<?php echo $i;?>)"class="glyphicon glyphicon-remove buttonremove" ></a></span>
</div>
<?php $i++; } ?>
<?php }}?>
</div>
<?php }?>
<script type="text/javascript">
function removeImage(id) {
console.log(id);
if(!id)
id = "";
jQuery('#firstimage'+(id)).remove();
jQuery('#imagediv'+(id)).remove();
}
</script>
<?php }
/**********************************************************************************/
/* Save the Metabox Data */
/**********************************************************************************/
add_action('save_post', 'code_save_images_meta', 1, 2); // save the custom fields
function code_save_images_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
//because save_post can be triggered at other times
// if ( !wp_verify_nonce( $_POST['jit_gallery_attachment'], plugin_basename(__FILE__) )) {
// return $post->ID;
// }
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
if( isset( $_POST['code_gallery_attachment']) ) :
$gallery_meta['images'] = $_POST['code_gallery_attachment'];
// Add values of $gallery_meta as custom fields
foreach ($gallery_meta as $key => $value) { // Cycle through the $gallery_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
update_post_meta($post->ID,'code_gallery_images',$_POST['code_gallery_attachment']);
endif; //isset End
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment