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

Custom Fields For Taxonomy WordPress

  • SOLVED

I have a custom post type <strong>products</strong> and a taxonomy called <strong>products-category</strong>.

I have categories types such: <strong>Furniture</strong>, <strong>Lighting</strong>, <strong>Home Decor</strong> etc

What I'm looking to do, is to have in the back-end a custom field named e.g.<strong>category_color </strong> for each Category Type (Furniture, Lighting etc.).

After I update each category's custom field color, I need to echo it in<strong> products.php </strong>and <strong>single-products.php</strong>.
This will help me to mass change colors for each category type and its posts.

I found this post but I have hard time understanding what I need to do with the explanations:
[[LINK href="http://en.bainternet.info/2011/custom-taxonomies-extra-fields"]]http://en.bainternet.info/2011/custom-taxonomies-extra-fields[[/LINK]]

Not sure if this will be a small plugin or I can update the functions.php but I'll appreciate your help.

Answers (3)

2011-12-27

Luis Abarca answers:

You can use [[LINK href="http://codex.wordpress.org/Function_Reference/get_metadata"]]get_metadata[[/LINK]] funtions, insert, update.

You need to create a table first for the taxonomy.

Check this post [[LINK href="http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data"]]http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data[[/LINK]]


Luis Abarca comments:


/**
* create tables for taxonomies
* Inspired by http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
*/
function _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);
}


register_activation_hook(__FILE__, 'yourplugin_activate');

function yourplugin_activate()
{
// ... you actual code here
_create_metadata_table('products-category');
}


add_action( 'edited_category-color', 'category_color_save_extra_fields' );

function category_color_save_extra_fields( $term_id )
{
// La url del archivo
$color = $_POST['category_color'];
update_metadata('category-color', $term_id, 'category_color', $color);
}

// the rest is to add the custom field to the form when you are editing the taxonomy and seems you already done that.
// ...


Lucian Florian comments:

Thank you for taking your time to help me.

I tried your code but it doesn't shows the extra field in back-end:
http://cl.ly/2i390p0W331g3H310v0u

I checked your code and it seems you have both: category_color and category-color. Not sure if that's the problem.


Luis Abarca comments:

Yep, i was wrong, the taxonomy is product-category isn't it ?


/**
* create tables for taxonomies
* Inspired by http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
*/
function _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);
}

register_activation_hook(__FILE__, 'yourplugin_activate');

function yourplugin_activate()
{
// ... you actual code here
_create_metadata_table('products-category');
}

add_action( 'edited_products-category', 'products_category_save_extra_fields' );

function products_category_save_extra_fields( $term_id )
{
// Form field with the color value
$color = $_POST['category_color'];

update_metadata('products-category', $term_id, 'category_color', $color);
}

// Include your form
// }}}
// {{{

add_action( 'products-category_edit_form', 'products_category_extra_fields' );

/**
* Se agregan campos personalizados a los destinos
*
*/
function products_category_extra_fields()
{
$current_color = get_metadata('products-category', $_GET['tag_ID'], 'category_color', true);

include 'category-color-field.php';
}



Then, in the same folder.

<strong>category-color-field.php</strong>

<table class="form-table">
<tr class="form-field">
<th><label for="category_color">Category color</label></th>

<td>
<input id="category_color" type="text" name="category_color" value="<?php echo $current_color ?>" />
<span class="description">
Category Color.</span>
</td>
</tr>
</table>



Luis Abarca comments:

I copy this code to a gist entry, [[LINK href="https://gist.github.com/1525544"]]https://gist.github.com/1525544[[/LINK]]


Lucian Florian comments:

The taxonomy is <strong>products-category</strong> and stays under the Custom Post Type <strong>Products</strong>.

I added the last piece of code and I get some errors:
[[LINK href="http://cl.ly/1D30152k3I2j332k1J11"]]
http://cl.ly/1D30152k3I2j332k1J11
[[/LINK]]


Lucian Florian comments:

not sure if that causes the problem but I use a temporary URL for development, containing the IP address/username


Luis Abarca comments:

Make sure that you create the category-color-field.php file


Lucian Florian comments:

yes, I noticed that right now and created the file.

It shows the color field in back-end, but it doesn't seems to save the data.
[[LINK href="http://cl.ly/3A3Z0o0J1y2a191K1X2R"]]
http://cl.ly/3A3Z0o0J1y2a191K1X2R
[[/LINK]]
After I save and go back, the field is blank. Nothing happens in front end either.


Luis Abarca comments:

You need to create a new plugin, then activate to create the table for store the custom field


Luis Abarca comments:

I updated my code, https://gist.github.com/1525544

I also attach the custom plugin ready to install.

I try the plugin in my dev site,

1.- post type archive (<em>archive-products.php</em>) [[LINK href="http://dev.justoalblanco.com/products/"]]http://dev.justoalblanco.com/products/[[/LINK]]
2.- Single post (<em>single-products.php</em>) [[LINK href="http://dev.justoalblanco.com/products/apple-2/iphone-4s/"]]http://dev.justoalblanco.com/products/apple-2/iphone-4s/[[/LINK]]

I just add this code in the loop to show the color

<p>Color: <?php the_category_color() ?></p>