Skip to content

Instantly share code, notes, and snippets.

@lagenceoueb
Last active May 13, 2022 06:54
Show Gist options
  • Save lagenceoueb/5d0b44e00114dc6d45edef9f4bf1a021 to your computer and use it in GitHub Desktop.
Save lagenceoueb/5d0b44e00114dc6d45edef9f4bf1a021 to your computer and use it in GitHub Desktop.
<?php
/**
* ADD IMAGE META ON CATEGORIES
* @from : https://yourblogcoach.com/how-to-add-taxonomy-images-in-wordpress/#Add_Taxonomy_Images_to_Default_Taxonomy_Category
*/
add_action( 'category_add_form_fields', 'add_category_image', 10, 2 );
/**
* Add image field on category
* @param $taxonomy
* @return void
*/
function add_category_image ( $taxonomy ) {
?>
<div class="form-field term-group">
<label for="image_id"><?php _e('Image', 'taxt-domain'); ?></label>
<input type="hidden" id="image_id" name="image_id" class="custom_media_url" value="">
<div id="image_wrapper"></div>
<p>
<input type="button" class="button button-secondary taxonomy_media_button" id="taxonomy_media_button" name="taxonomy_media_button" value="<?php _e( 'Add Image', 'taxt-domain' ); ?>">
<input type="button" class="button button-secondary taxonomy_media_remove" id="taxonomy_media_remove" name="taxonomy_media_remove" value="<?php _e( 'Remove Image', 'taxt-domain' ); ?>">
</p>
</div>
<?php
}
/**
* Save Image Field
*/
add_action( 'created_category', 'save_category_image', 10, 2 );
function save_category_image ( $term_id, $tt_id ) {
if( isset( $_POST['image_id'] ) && '' !== $_POST['image_id'] ){
$image = $_POST['image_id'];
add_term_meta( $term_id, 'category_image_id', $image, true );
}
}
/**
* Add Image Field in Edit Form
*/
add_action( 'category_edit_form_fields', 'update_category_image', 10, 2 );
function update_category_image ( $term, $taxonomy ) { ?>
<tr class="form-field term-group-wrap">
<th scope="row">
<label for="image_id"><?php _e( 'Image', 'taxt-domain' ); ?></label>
</th>
<td>
<?php $image_id = get_term_meta ( $term -> term_id, 'image_id', true ); ?>
<input type="hidden" id="image_id" name="image_id" value="<?php echo $image_id; ?>">
<div id="image_wrapper">
<?php if ( $image_id ) { ?>
<?php echo wp_get_attachment_image ( $image_id, 'thumbnail' ); ?>
<?php } ?>
</div>
<p>
<input type="button" class="button button-secondary taxonomy_media_button" id="taxonomy_media_button" name="taxonomy_media_button" value="<?php _e( 'Add Image', 'taxt-domain' ); ?>">
<input type="button" class="button button-secondary taxonomy_media_remove" id="taxonomy_media_remove" name="taxonomy_media_remove" value="<?php _e( 'Remove Image', 'taxt-domain' ); ?>">
</p>
</div></td>
</tr>
<?php
}
/**
* Update Image Field
*/
add_action( 'edited_category', 'updated_category_image', 10, 2 );
function updated_category_image ( $term_id, $tt_id ) {
if( isset( $_POST['image_id'] ) && '' !== $_POST['image_id'] ){
$image = $_POST['image_id'];
update_term_meta ( $term_id, 'image_id', $image );
} else {
update_term_meta ( $term_id, 'image_id', '' );
}
}
/**
* Enqueue Media Library
*/
add_action( 'admin_enqueue_scripts', 'load_media' );
function load_media() {
wp_enqueue_media();
}
add_action( 'admin_footer', 'add_custom_script' );
function add_custom_script() {
?> <script>jQuery(document).ready( function($) {
function taxonomy_media_upload(button_class) {
var custom_media = true,
original_attachment = wp.media.editor.send.attachment;
$('body').on('click', button_class, function(e) {
var button_id = '#'+$(this).attr('id');
var send_attachment = wp.media.editor.send.attachment;
var button = $(button_id);
custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( custom_media ) {
$('#image_id').val(attachment.id);
$('#image_wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
$('#image_wrapper .custom_media_image').attr('src',attachment.url).css('display','block');
} else {
return original_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
taxonomy_media_upload('.taxonomy_media_button.button');
$('body').on('click','.taxonomy_media_remove',function(){
$('#image_id').val('');
$('#image_wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
});
$(document).ajaxComplete(function(event, xhr, settings) {
var queryStringArr = settings.data.split('&');
if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
var xml = xhr.responseXML;
$response = $(xml).find('term_id').text();
if($response!=""){
$('#image_wrapper').html('');
}
}
});
});</script> <?php
}
/**
* Display Image in Column
*/
add_filter( 'manage_edit-category_columns', 'display_image_column_heading' );
function display_image_column_heading( $columns ) {
$columns['category_image'] = __( 'Image', 'taxt-domain' );
return $columns;
}
/**
* Let’s display the value for this column against each term id.
*/
add_action( 'manage_category_custom_column', 'display_image_column_value' , 10, 3);
function display_image_column_value( $columns, $column, $id ) {
if ( 'category_image' == $column ) {
$image_id = esc_html( get_term_meta($id, 'image_id', true) );
$columns = wp_get_attachment_image ( $image_id, array('50', '50') );
}
return $columns;
}
@lagenceoueb
Copy link
Author

Now, you can display image on frontend :
$image_id = get_term_meta ( $term_id, 'image_id', true ); echo wp_get_attachment_image ( $image_id, 'full' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment