Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Created June 29, 2013 08:45
Show Gist options
  • Save UmeshSingla/5890409 to your computer and use it in GitHub Desktop.
Save UmeshSingla/5890409 to your computer and use it in GitHub Desktop.
Image Uploader for Custom Taxonomies
<?php
//Add a Image Uploader for each taxonomy
// Add term page
function rtp_news_image_add($tag) {
// this will add the custom meta field to the add new term page
$get_img = get_option( "custom_marketimg_id_$tag->term_id" ); ?>
<table class="form-table">
<tr class="form-field">
<th>
<label for="news-category-image"><?php _e( 'Upload Image', 'rtPanel' ); ?></label>
</th>
<td>
<input type="button" value="<?php _e( "Upload Image for $tag->name", 'rtPanel' ); ?>" class="button button-primary" id="news-category-image" style="width: 200px;"/>
<input type="hidden" name="image_id_<?php echo $tag->term_id; ?>" id="image_id" value="<?php if( isset( $get_img ) ) echo $get_img; ?>" />
</td>
</tr>
<?php if(isset($get_img)){ ?>
<tr>
<th>
<input value="1" type="checkbox" name="remove_img" id="remove_img" />
<label for="remove_img"><?php _e( ' Remove This Image', 'rtPanel' ); ?></label>
</th>
<td>
<?php $custom_image_uploader = wp_get_attachment_image_src( $get_img, 'full' ); ?>
<img src="<?php echo @$custom_image_uploader[0]; ?>" alt="<?php _e( 'Custom Image', 'rtPanel' ); ?>" <?php echo ( isset( $custom_image_uploader[0] ) ? 'style="max-width: 150px; width: 150px"' : 'style="max-width: 150px; width: 150px; display: none;"' ); ?> />
</td>
</tr><?php } ?>
</table>
<?php
}
add_action( 'news-category_add_form_fields', 'rtp_news_image_add' );
// Add term page
function rtp_news_image_edit($tag) {
$get_img = get_option( "custom_marketimg_id_$tag->term_id" ); ?>
<table class="form-table">
<tr class="form-field">
<th>
<label for="news-category-image"><?php _e( 'Upload Image', 'rtPanel' ); ?></label>
</th>
<td>
<input type="button" value="<?php _e( "Upload Image for $tag->name", 'rtPanel' ); ?>" class="button button-primary custom_image_uploader" id="news-category-image" style="width: 200px;"/>
<input type="hidden" name="image_id_<?php echo $tag->term_id; ?>" id="image_id" value="<?php if( isset( $get_img ) ) echo $get_img; ?>" />
</td>
</tr>
<?php if(isset($get_img)){ ?>
<tr>
<th>
<input value="1" type="checkbox" name="remove_img" id="remove_img" />
<label for="remove_img"><?php _e( ' Remove This Image', 'rtPanel' ); ?></label>
</th>
<td>
<?php $custom_image_uploader = wp_get_attachment_image_src( $get_img, 'full' ); ?>
<img class="image-preview" src="<?php echo @$custom_image_uploader[0]; ?>" alt="<?php _e( 'Custom Image', 'rtPanel' ); ?>" <?php echo ( isset( $custom_image_uploader[0] ) ? 'style="max-width: 150px; width: 150px"' : 'style="max-width: 150px; width: 150px; display: none;"' ); ?> />
</td>
</tr><?php } ?>
</table>
<?php
}
add_action( 'news-category_edit_form_fields', 'rtp_news_image_edit' );
// Add term page
function rtp_news_image_save($term_id) {
if(isset($_POST["image_id_$term_id"]) && ($_POST["image_id_$term_id"]!='') && ((!isset($_POST['remove_img']))) ){
update_option("image_id_$term_id", $_POST["image_id_$term_id"]);
}else{
update_option("image_id_$term_id", '');
}
}
add_action( 'edited_category', 'rtp_news_image_save', 10, 2 );
add_action( 'create_category', 'rtp_news_image_save', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment