Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

add an uploader for each category WordPress

  • SOLVED

I need to add an uploader in the edit page of each category
(and need to display this image in the category page, in the front-end of course).

Without plugin it's better.

Answers (4)

2011-11-29

Luis Abarca answers:

I'm using a similar way to show the cities here [[LINK href="http://ihata.com.mx/"]]http://ihata.com.mx[[/LINK]]

Add a custom meta and functions

function get_the_category_image_src($term_ID)
{
$taxonomy = 'category'; // or yourtaxonomy

return get_metadata($taxonomy, $term_ID, 'thumbnail', true);
}

function the_category_image($term)
{
$src = get_the_category_image_src($term->ID);

if (!empty($src)) {
echo '<img class="my-category-thumb" src="' . $src . '" />';
}
}

add_action('category_edit_form', 'mycat_extra_fields' );
// or add_action( 'yourtaxonomy_edit_form', 'mycat_extra_fields' );

function mycat_extra_fields()
{
$thumbnail = get_metadata('category', $_GET['tag_ID'], 'thumbnail', true);
// or
// $thumbnail = get_metadata('yourtaxonomy', $_GET['tag_ID'], 'thumbnail', true);

include 'form.php';
}

// }}}
// {{{

function my_admin_scripts()
{
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');

wp_register_script('my-custom-upload', get_bloginfo('template_url') . '/custom-upload.js', array('jquery', 'media-upload', 'thickbox'));

wp_enqueue_script('my-custom-upload');
}

/*
* Add thickbox
*/
function my_admin_styles()
{
wp_enqueue_style('thickbox');
}

// Add js to uoload file
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');




form.php

<table class="form-table">
<tr class="form-field">
<th><label for="upload_url">Image</label></th>

<td>
<input style="width: 70%" id="upload_url" type="text" name="upload_url" value="<?php echo $thumbnail ?>" />
<input style="width: 20%" class="button" id="btn-upload" type="button" value="Select a image" />
<br />
<span class="description">
Upload a JPG, PNG or GIF image.</span>
</td>
</tr>


custom-upload.js


(function($)
{
$(document).ready(function() {
$('#btn-upload').click(function()
{
formfield = $('#upload_url').attr('name');
tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true&width=640&height=480');
return false;
});

window.send_to_editor = function(html)
{
url = $(html).attr('href');

if ("" == url) {
url = $('img', html).attr('href');
}

$('#upload_url').val(url);
tb_remove();
}
});
})(jQuery);



In your theme loop or forehac ($terms as $term)....

...
the_category_image($term);
...


Luis Abarca comments:

If you want to use custom taxonomy, you need to change the category with


add_action( 'yourtaxonomy_edit_form', 'jab_hotel_city_extra_fields' );


and


$thumbnail = get_metadata('yourtaxonomy', $_GET['tag_ID'], 'thumbnail', true);


Luis Abarca comments:

opps, i miss the save action



add_action( 'edited_category', 'mycategory_save_extra_fields' );
// or
//add_action( 'edited_yourtaxonomy', 'mycategory_save_extra_fields' );

function mycategory_save_extra_fields( $cat_id )
{
// image URL
$url = trim($_POST['upload_url']);

update_metadata('category', $cat_id, 'thumbnail', $url);
// or
// update_metadata('yourtaxonomy', $cat_id, 'thumbnail', $url);
}


Sébastien | French WordpressDesigner comments:

i have replace
wp_register_script('my-custom-upload', YOUR_PLUGIN_URL . '/custom-upload.js', array('jquery', 'media-upload', 'thickbox'));
by
wp_register_script('my-custom-upload', get_bloginfo('template_url').'/custom-upload.js', array('jquery', 'media-upload', 'thickbox'));

i have a button in my edit page of category
but when i click on the button nothing happened


Luis Abarca comments:

i updated my first answer, the highlighted line was wrong,


wp_register_script('my-custom-upload', get_bloginfo('template_url') . '/custom-upload.js', array('jquery', 'media-upload', 'thickbox'));

<strong> wp_enqueue_script('my-custom-upload'); </strong>


Sébastien | French WordpressDesigner comments:

ok, now the window of media-uploader appears.
the url of the image is in the field
i submit
but when i come back in the edit page of the category, the field "url of the image" is empty


Luis Abarca comments:

opps :(, please update the form.php with this code


<table class="form-table">
<tr class="form-field">
<th><label for="upload_url">Image</label></th>
<td>
<input style="width: 70%" id="upload_url" type="text" name="upload_url" value="<?php echo $thumbnail ?>" />
<input style="width: 20%" class="button" id="btn-upload" type="button" value="Select a image" />
<br />
<span class="description">
Upload a JPG, PNG or GIF image.</span>
</td>
</tr>
</table>


Sébastien | French WordpressDesigner comments:

nothing has changed :(


Luis Abarca comments:

OK, Are you using the category or a custom taxonomy ?

Can you give me login or ftp to check was wrong ??, im using this code, but i edited because it contains a map and some other custom fields


Sébastien | French WordpressDesigner comments:

i send you the login


Luis Abarca comments:

OK, i'll check it in about 15 mins, meal time :D


Luis Abarca comments:

OK, i'm working on it amigo


Luis Abarca comments:

Its OK Sébastien, i almost forget to add the table for the custom meta data :(

But i added to your site in a new plugin, category-thumbs


Luis Abarca comments:

For the record, you need to create a table for store the custom terms metadata


global $wpdb;
// table for custom metadata
$wpdb->categorymeta = $wpdb->prefix . 'categorymeta';

my_create_metadata_table('category');

/**
* create tables for taxonomies
* Inspired by http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
*/
function my_create_metadata_table($type)
{
global $wpdb;

$table_name = $wpdb->prefix . $type . 'meta';

if (!empty ($wpdb->charset)) {
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}

if (!empty ($wpdb->collate)) {
$charset_collate .= " COLLATE {$wpdb->collate}";
}

$sql = "CREATE TABLE IF NOT EXISTS {$table_name} (
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
{$type}_id bigint(20) NOT NULL default 0,
meta_key varchar(255) DEFAULT NULL,
meta_value longtext DEFAULT NULL,
UNIQUE KEY meta_id (meta_id)
) {$charset_collate};";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);

}


Sébastien | French WordpressDesigner comments:

Luis, it's really perfect !!
Thanx

I need just another advice :
do you know why the value change in the upload file, the event "change" seems not work

i use this code to display a preview



<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function($) {


$('#upload_url').change(function() {

var url = $('#upload_url').val();
var image = $('#image').attr('src', url);

image=url;

});

});
</script>


Sébastien | French WordpressDesigner comments:

in form.php
i have add this code
<?php if($thumbnail) { ?>
<td><img id="image" src="<?php echo $thumbnail ?>" style="max-width:125px;">
</td>
<?php } ?>


Sébastien | French WordpressDesigner comments:

euh... how can i delete an icone ?


Luis Abarca comments:

Ummm, i have no delete, let me add it, to show a previw i use this



function my_manage_thumb(element)
{
var url = element.val();

thumb = $('#image');

if (url != '') {
thumb.show();
thumb.attr('src', url);
} else {
thumb.hide();
}
}

$(document).ready(function()
{
$('#upload_url').change(function()
{
my_manage_thumb( $(this) );
});
});


Sébastien | French WordpressDesigner comments:

could you integrate this code please ?
and, please : how can i change the folder of the uploaded images (only for this plugin)

PS : i increase my fee :-)


Sébastien | French WordpressDesigner comments:

Luis, have you read my last message please ? :-)


Luis Abarca comments:

Hi Sébastien, sorry i just read your message.

If you left blank the input, it will delete it from the metadata. You can also add un "Delete" button to clear the text input.

I made this changes to your code.


add_action('edited_category', 'mycategory_save_extra_fields' );

function mycategory_save_extra_fields( $cat_id )
{
// image URL
$url = trim($_POST['upload_url']);

if ( !empty($url) ) {
update_metadata('category', $cat_id, 'thumbnail', $url);
} else {
delete_metadata('category', $cat_id, 'thumbnail');
}
}


Sébastien | French WordpressDesigner comments:

nothing happens when i click on the button

& please : how can i change the folder of the uploaded images (only for this plugin)


Luis Abarca comments:

OK, i changed the JS file, now it works with custom-upload.js inside the plugin.


Luis Abarca comments:

I'm testing the custom upload folder, i'll be right back after the meal time, in about 30 mins.


Luis Abarca comments:

Hey amigo, i finally cracked, now you have custom upload folder to that screen.

And for the record:

add_filter( 'swfupload_post_params', 'add_flash_upload_url_vars');

// add params to flash uploader
function add_flash_upload_url_vars( $params )
{
if ( isset($_GET['customdir']) ) {
$params['customdir'] = trim($_GET['customdir']);
}

return $params;
}

// add a custom upload folder to categories
add_filter('upload_dir', 'my_upload_dir', 10, 2);

// add a custom upload folder to categories
add_filter('upload_dir', 'my_upload_dir', 10, 2);

// Normal uploads uses the usual folders
if ( !isset($_REQUEST['customdir']) ) {
remove_filter('upload_dir', 'my_upload_dir');
}

function my_upload_dir( $upload )
{
$customdir = trim($_REQUEST['customdir']);

if ( !empty($customdir) ) {
$upload['subdir'] = '/' . $customdir . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
}

return $upload;
}


Luis Abarca comments:

Thanks for your comment Julio, regards !


Sébastien | French WordpressDesigner comments:

This is a very good work luis. I increase my fee. That's normal :-)

2011-11-29

John Cotton answers:

Hi Sebastien

I have done this, but it's not 3 lines of code - might you up the prize a little? :)

JC

2011-11-29

Peter Michael answers:

[[LINK href="http://wordpress.org/extend/plugins/category-images-ii/"]]Category Images II[[/LINK]]

You can integrate the plugin code into the theme as well.


Sébastien | French WordpressDesigner comments:

many problems with this plugin
-image preview in admin : not refresh (must use ctrl+F5 to view the curent image)
-not support png correctly (black background)

2011-12-02

Julio Potier answers:

I think Luis deserves the price, all is ok Seb ? Or can i help more ?


Sébastien | French WordpressDesigner comments:

Tu as complètement raison Julio, Luis a largement mérité son prix. C'est du très beau boulot.
Merci pour ta proposition d'aide :-)

Tu es français je crois. ça fait plaisir de voir un compatriote ici :-)

----

You're right Julio. Luis deserves the price. It's a very great work, and a very good plugin.


Julio Potier comments:

Oui et "Gregory Viguier" aussi est français, je l'ai amené, on est au moins 3 comme ça, haha.
A bientôt !
ps : julio.boiteaweb si tu veux skyper WordPress tout ça.


Sébastien | French WordpressDesigner comments:

Cool ! Quand on sera assez nombreux, on pensera à créer un wpquestions en français ;-)
A+

Je garde ton contact au cas où. On aura peut-être l'occasion de bosser ensemble 1 de ces 4