Skip to content

Instantly share code, notes, and snippets.

@alispx
Last active October 21, 2015 13:36
Show Gist options
  • Save alispx/320c23bec54bbfeb4a83 to your computer and use it in GitHub Desktop.
Save alispx/320c23bec54bbfeb4a83 to your computer and use it in GitHub Desktop.
<?php
/**
* Constructor
*/
add_action( 'init', 'tax_image_init' );
function tax_image_init() {
$tk_taxonomies = array( 'authors', 'publisher' );
foreach ( $tk_taxonomies as $tk_taxonomy ) {
// Add form
add_action( $tk_taxonomy.'_add_form_fields', 'add_category_fields' );
add_action( $tk_taxonomy.'_edit_form_fields', 'edit_category_fields', 10, 2 );
// Add columns
add_filter( 'manage_edit-'.$tk_taxonomy.'_columns', 'taxonomies_columns' );
add_filter( 'manage_'.$tk_taxonomy.'_custom_column', 'taxonomy_column', 10, 3 );
}
add_action( 'created_term', 'save_category_fields', 10, 3 );
add_action( 'edit_term', 'save_category_fields', 10, 3 );
// Taxonomy page descriptions
}
/**
* Category thumbnail fields.
*
* @access public
* @return void
*/
function add_category_fields() {
?>
<div class="form-field">
<label><?php _e( 'Thumbnail', 'textdomain' ); ?></label>
<div id="taxonomy_thumbnail" style="float:left;margin-right:10px;"><img src="<?php echo wc_placeholder_img_src(); ?>" width="60px" height="60px" /></div>
<div style="line-height:60px;">
<input type="hidden" id="taxonomy_thumbnail_id" name="taxonomy_thumbnail_id" />
<button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'textdomain' ); ?></button>
<button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'textdomain' ); ?></button>
</div>
<?php wp_enqueue_media(); ?>
<script type="text/javascript">
// Only show the "remove image" button when needed
if ( ! jQuery('#taxonomy_thumbnail_id').val() )
jQuery('.remove_image_button').hide();
// Uploading files
var file_frame;
jQuery(document).on( 'click', '.upload_image_button', function( event ){
event.preventDefault();
// If the media frame already exists, reopen it.
if ( file_frame ) {
file_frame.open();
return;
}
// Create the media frame.
file_frame = wp.media.frames.downloadable_file = wp.media({
title: '<?php _e( 'Choose an image', 'textdomain' ); ?>',
button: {
text: '<?php _e( 'Use image', 'textdomain' ); ?>',
},
multiple: false
});
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
jQuery('#taxonomy_thumbnail_id').val( attachment.id );
jQuery('#taxonomy_thumbnail img').attr('src', attachment.url );
jQuery('.remove_image_button').show();
});
// Finally, open the modal.
file_frame.open();
});
jQuery(document).on( 'click', '.remove_image_button', function( event ){
jQuery('#taxonomy_thumbnail img').attr('src', '<?php echo wc_placeholder_img_src(); ?>');
jQuery('#taxonomy_thumbnail_id').val('');
jQuery('.remove_image_button').hide();
return false;
});
</script>
<div class="clear"></div>
</div>
<?php
}
/**
* Edit category thumbnail field.
*
* @access public
* @param mixed $term Term (category) being edited
* @param mixed $taxonomy Taxonomy of the term being edited
*/
function edit_category_fields( $term, $taxonomy ) {
$image = '';
$thumbnail_id = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) );
if ( $thumbnail_id )
$image = wp_get_attachment_thumb_url( $thumbnail_id );
else
$image = wc_placeholder_img_src();
?>
<tr class="form-field">
<th scope="row" valign="top"><label><?php _e( 'Thumbnail', 'textdomain' ); ?></label></th>
<td>
<div id="taxonomy_thumbnail" style="float:left;margin-right:10px;"><img src="<?php echo $image; ?>" width="60px" height="60px" /></div>
<div style="line-height:60px;">
<input type="hidden" id="taxonomy_thumbnail_id" name="taxonomy_thumbnail_id" value="<?php echo $thumbnail_id; ?>" />
<button type="submit" class="upload_image_button button"><?php _e( 'Upload/Add image', 'textdomain' ); ?></button>
<button type="submit" class="remove_image_button button"><?php _e( 'Remove image', 'textdomain' ); ?></button>
</div>
<?php wp_enqueue_media(); ?>
<script type="text/javascript">
// Uploading files
var file_frame;
jQuery(document).on( 'click', '.upload_image_button', function( event ){
event.preventDefault();
// If the media frame already exists, reopen it.
if ( file_frame ) {
file_frame.open();
return;
}
// Create the media frame.
file_frame = wp.media.frames.downloadable_file = wp.media({
title: '<?php _e( 'Choose an image', 'textdomain' ); ?>',
button: {
text: '<?php _e( 'Use image', 'textdomain' ); ?>',
},
multiple: false
});
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
jQuery('#taxonomy_thumbnail_id').val( attachment.id );
jQuery('#taxonomy_thumbnail img').attr('src', attachment.url );
jQuery('.remove_image_button').show();
});
// Finally, open the modal.
file_frame.open();
});
jQuery(document).on( 'click', '.remove_image_button', function( event ){
jQuery('#taxonomy_thumbnail img').attr('src', '<?php echo wc_placeholder_img_src(); ?>');
jQuery('#taxonomy_thumbnail_id').val('');
jQuery('.remove_image_button').hide();
return false;
});
</script>
<div class="clear"></div>
</td>
</tr>
<?php
}
/**
* save_category_fields function.
*
* @access public
* @param mixed $term_id Term ID being saved
* @param mixed $tt_id
* @param mixed $taxonomy Taxonomy of the term being saved
* @return void
*/
function save_category_fields( $term_id, $tt_id, $taxonomy ) {
if ( isset( $_POST['taxonomy_thumbnail_id'] ) )
update_woocommerce_term_meta( $term_id, 'thumbnail_id', absint( $_POST['taxonomy_thumbnail_id'] ) );
delete_transient( 'wc_term_counts' );
}
/**
* Thumbnail column added to category admin.
*
* @access public
* @param mixed $columns
* @return array
*/
function taxonomies_columns( $columns ) {
$new_columns = array();
$new_columns['cb'] = $columns['cb'];
$new_columns['thumb'] = __( 'Image', 'textdomain' );
unset( $columns['cb'] );
return array_merge( $new_columns, $columns );
}
/**
* Thumbnail column value added to category admin.
*
* @access public
* @param mixed $columns
* @param mixed $column
* @param mixed $id
* @return array
*/
function taxonomy_column( $columns, $column, $id ) {
if ( $column == 'thumb' ) {
$image = '';
$thumbnail_id = get_woocommerce_term_meta( $id, 'thumbnail_id', true );
if ($thumbnail_id)
$image = wp_get_attachment_thumb_url( $thumbnail_id );
else
$image = wc_placeholder_img_src();
$columns .= '<img src="' . esc_url( $image ) . '" alt="Thumbnail" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
/**
* Display author details
* @author alispx
* @since 2.0
*/
function alispx_get_author_details() {
$authors = wp_get_object_terms( get_the_ID(), 'authors' );
if ( ! empty( $authors ) ) {
foreach( $authors as $author ){
$thumbnail_id = get_woocommerce_term_meta( $author->term_id, 'thumbnail_id', true );
$termlink = get_term_link( $author->slug, 'authors' );?>
<div class="author_details">
<?php if ( $thumbnail_id ) : ?>
<a href="<?php echo $termlink; ?>" class="avatar">
<img class="author-ava" src="<?php echo wp_get_attachment_url( $thumbnail_id ); ?>" alt="Author" width="50" height="50">
</a>
<?php else : ?>
<a href="<?php echo $termlink; ?>">
<img class="author-ava" src="<?php echo get_template_directory_uri().'/img/avatar.png'; ?>" alt="Author">
</a>
<?php endif; ?>
<?php if ( $author->description ): ?>
<p><?php echo $author->description; ?></p>
<?php else : ?>
<p><?php _e( 'This author did not have detail information', 'textdomain' ); ?></p>
<?php endif; ?>
</div>
<?php }
} else {
_e( 'No Author assigned to this book', 'textdomain' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment