Comments on: Adding an image upload field to categories and taxonomies https://pluginrepublic.com/adding-an-image-upload-field-to-categories/ WooCommerce Plugins Sun, 28 May 2023 10:51:46 +0000 hourly 1 By: Anthony Nguyễn https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-194556 Sun, 28 May 2023 10:51:46 +0000 https://pluginrepublic.com/?p=267#comment-194556 Thanks you, this save me a lot.
For frontend, I prepare two functions:
function tx_get_category_icon_url($category) {
if (is_numeric($category)) {
$category = get_term($category, ‘app_cat’); // term_id, ‘category-image-id’, true);
if (empty($image_id)) {
return false;
}

$image_url = wp_get_attachment_image_url($image_id);

if (empty($image_url)) {
return false;
}

return $image_url;
}

function tx_the_category_icon($category, $classes = ”, $width = ”, $height = ”) {
if (is_numeric($category)) {
$category = get_term($category, ‘app_cat’);
}

$image_url = tx_get_category_icon_url($category);

if (empty($classes)) {
$classes = ‘img-fluid’;
}
$width_attr = ”;
$height_attr = ”;
if ($width) {
$width_attr = ‘ width=”‘ . esc_attr($width) . ‘”‘;
}
if ($height) {
$height_attr = ‘ height=”‘ . esc_attr($height) . ‘”‘;
}

if ($image_url) {
?>
<img src="” alt=”name); ?>” class=””>
<?php
}
}

Then you can use anywhere by:

]]>
By: Hussain Vohra https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-73706 Mon, 16 Mar 2020 06:24:23 +0000 https://pluginrepublic.com/?p=267#comment-73706 Hello, the code works great, the only issue I am getting is to display the image on the front-end. I am using this code for custom taxonomies (eg. locations), and want to display the image on the home page using a shortcode, (in a loop), can you please help how can I achieve this?

]]>
By: Jason Houston https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-68861 Wed, 26 Feb 2020 16:01:17 +0000 https://pluginrepublic.com/?p=267#comment-68861 Thank you! Helped me big time

]]>
By: Patrick https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-61675 Wed, 29 Jan 2020 14:27:54 +0000 https://pluginrepublic.com/?p=267#comment-61675 Hi,

Thanks for this awesome tut! I have one small issue and that is the following

Uncaught TypeError: Cannot read property ‘value’ of null
at Object.insert (media-editor.min.js?ver=5.3.2:1)

Anyone managed to get rid of the following console error when you click on “Insert into post”?

]]>
By: Dario Zadro https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-61377 Tue, 28 Jan 2020 01:58:30 +0000 https://pluginrepublic.com/?p=267#comment-61377 This is fantastic! Thank you very much.

I changed it a little bit to extend for more categories as needed:

function init($taxonomy = ‘category’) {

// default categories
add_action( $taxonomy . ‘_add_form_fields’, array ( $this, ‘add_category_image’ ), 10, 2 );
add_action( ‘created_’ . $taxonomy, array ( $this, ‘save_category_image’ ), 10, 2 );
add_action( $taxonomy . ‘_edit_form_fields’, array ( $this, ‘update_category_image’ ), 10, 2 );
add_action( ‘edited_’ . $taxonomy, array ( $this, ‘updated_category_image’ ), 10, 2 );

// enqueue
//add_action( ‘admin_enqueue_scripts’, array( $this, ‘load_media’ ) );
//add_action( ‘admin_footer’, array( $this, ‘add_script’ ) );
}

function load_media() {
wp_enqueue_media();
add_action( ‘admin_footer’, array( $this, ‘add_script’ ) );
}

Then, you can just add the below to functions.php

add_action( ‘admin_enqueue_scripts’, function () {

//load the whole class, change name and directory as needed
require_once get_template_directory() . ‘/assets/inc/the-whole-class.php’;

// setup general category
$category = new CT_TAX_META();
$category->init();
$category->load_media(); //setup JS scripts

// repeat as needed for other taxonomies
(new CT_TAX_META)->init(‘category-other’);
} );

Hope this helps! Also, I removed the public declaration on all class functions.

]]>
By: Narendra https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-42051 Thu, 24 Oct 2019 06:57:10 +0000 https://pluginrepublic.com/?p=267#comment-42051 How can get image url only

]]>
By: Leon https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-29313 Fri, 16 Aug 2019 06:50:12 +0000 https://pluginrepublic.com/?p=267#comment-29313 Works Perfect for me !!
thanks for this great solution

]]>
By: Sebastien https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-24102 Mon, 10 Jun 2019 18:46:40 +0000 https://pluginrepublic.com/?p=267#comment-24102 Works like a charm (test with taxonomy) !!!
Thanks a lot I was looking for something like this for a long time…

]]>
By: Stefano https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-22823 Thu, 09 May 2019 14:24:42 +0000 https://pluginrepublic.com/?p=267#comment-22823 Really useful!
I’m thinking about a mod to make it possible to upload multiple images? Do you have just completed something similar?

]]>
By: Maksym https://pluginrepublic.com/adding-an-image-upload-field-to-categories/#comment-22676 Mon, 06 May 2019 00:51:55 +0000 https://pluginrepublic.com/?p=267#comment-22676 In reply to Yunus Tan.

//Getting the term by name
$terms = get_the_terms($post->ID, ‘teams’);

//Retrieving images from taxonomy by term id
foreach($terms as $term) {
$image_id = get_term_meta($term->term_id, ‘showcase-taxonomy-image-id’, true);
echo wp_get_attachment_image($image_id, ‘large’);
}

]]>