Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am looking to achieve something which I figured would be fairly simple to accomplish but it seems there are no real examples available to achieve my needs.

Essentially I have a custom post type of "Articles" for this custom post type I have a new taxonomy which I have registered which was created so that source publications can be added.

My goal was to be able to add an article through this custom post type and then select the applicable "source publication" from the term list so I could display where the article came from.

Now, the problem I am having seems to be simple... All I am trying to do is add some additional fields to the page where you can enter each taxonomy term. In this case I would like to add a field for "URL" and include an image for each source so I can add a logo.

So the question here is... how would I go about adding an additional field to each term?

I am assuming if wordpress does not allow for this functionality naively that somehow the "description" field could be utilized as a type of custom fields area and thus that the data can be stored there.

Then I am trying to of course extract the data out and display it.

I was able to customize the taxonomy column titles in case anyone is interest in the same way that columns can be modified for custom post types like this:

// CUSTOM TAXONOMY COLUMNS FOR CONTENT SOURCES
   add_filter("manage_edit-content_sources_columns", 'content_sources_columns');    
   function content_sources_columns($content_sources_columns) {
    $new_columns = array(
        'cb' => '<input type="checkbox" />',
        'name' => __('Name'),
//      'source_image' => '',
        'description' => __('URL'),
        'slug' => __('Slug'),
        'posts' => __('Posts')
        );
    return $new_columns;
   }
share|improve this question
add comment

5 Answers

up vote 5 down vote accepted

Hi @NetConstructor.com:

I wrote this last month for somebody and it may address what you are looking for. It is an example you would modify, not a complete ready-to-use solution:

<?php
/*
 * Example code showing how to hook WordPress to add fields to the taxonomny term edit screen.
 * 
 * This example is meant to show how, not to be a drop in example.
 *
 * This example was written in response to this question:
 *
 *    http://lists.automattic.com/pipermail/wp-hackers/2010-August/033671.html
 *
 * By:
 *
 *    Mike Schinkel (http://mikeschinkel.com/custom-wordpress-plugins/)
 *
 * NOTE:
 *
 *    This could easily become a plugin if it were fleshed out.
 *    A class with static methods was used to minimize the variables & functions added to the global namespace.
 *    wp_options was uses with one option be tax/term instead of via a serialize array because it aids in retrival
 *    if there get to be a large number of tax/terms types. A taxonomy/term meta would be the prefered but WordPress
 *    does not have one.
 *
 * This example is licensed GPLv2.
 *
 */

// These are helper functions you can use elsewhere to access this info
function get_taxonomy_term_type($taxonomy,$term_id) {
  return get_option("_term_type_{$taxonomy}_{$term->term_id}");
}
function update_taxonomy_term_type($taxonomy,$term_id,$value) {
  update_option("_term_type_{$taxonomy}_{$term_id}",$value);
}

//This initializes the class.
TaxonomyTermTypes::on_load();

//This should be called in your own code. This example uses two taxonomies: 'region' & 'opportunity'
TaxonomyTermTypes::register_taxonomy(array('region','opportunity'));

class TaxonomyTermTypes {
  //This initializes the hooks to allow saving of the
  static function on_load() {
    add_action('created_term',array(__CLASS__,'term_type_update'),10,3);
    add_action('edit_term',array(__CLASS__,'term_type_update'),10,3);
  }
  //This initializes the hooks to allow adding the dropdown to the form fields
  static function register_taxonomy($taxonomy) {
    if (!is_array($taxonomy))
      $taxonomy = array($taxonomy);
    foreach($taxonomy as $tax_name) {
      add_action("{$tax_name}_add_form_fields",array(__CLASS__,"add_form_fields"));
      add_action("{$tax_name}_edit_form_fields",array(__CLASS__,"edit_form_fields"),10,2);
    }
  }
  // This displays the selections. Edit it to retrieve
  static function add_form_fields($taxonomy) {
    echo "Type " . self::get_select_html('text');
  }
  // This displays the selections. Edit it to retrieve your own terms however you retrieve them.
  static function get_select_html($selected) {
    $selected_attr = array('text'=>'','user'=>'','date'=>'','etc'=>'');
    $selected_attr[$selected] = ' selected="selected"';
    $html =<<<HTML
<select id="tag-type" name="tag-type">
  <option value="text"{$selected_attr['text']}>Text</option>
  <option value="user"{$selected_attr['user']}>User</option>
  <option value="date"{$selected_attr['date']}>Date</option>
  <option value="etc" {$selected_attr['etc']}>Etc.</option>
</select>
HTML;
    return $html;
  }
    // This a table row with the drop down for an edit screen
    static function edit_form_fields($term, $taxonomy) {
    $selected = get_option("_term_type_{$taxonomy}_{$term->term_id}");
    $select = self::get_select_html($selected);
    $html =<<<HTML
<tr class="form-field form-required">
  <th scope="row" valign="top"><label for="tag-type">Type</label></th>
  <td>$select</td>
</tr>
HTML;
    echo $html;
  }
  // These hooks are called after adding and editing to save $_POST['tag-term']
  static function term_type_update($term_id, $tt_id, $taxonomy) {
    if (isset($_POST['tag-type'])) {
      update_taxonomy_term_type($taxonomy,$term_id,$_POST['tag-type']);
    }
  }
}

Hope it helps.

share|improve this answer
    
Hi again Mike! You sure seem to be a true expert on these matters and its great to have your here. I have been experimenting with this code and have a few followup questions. First of all... In your example, where is the the custom field data for each taxonomy term being stored? Second, would you possibly be able to provide an example or code which would allow one to add a "select media" button which in turn opens the media library to add/select an image from and then shows the applicable image. I find this is totally not documented and sooo useful! –  NetConstructor.com Sep 6 '10 at 15:38
    
@NetConstuctor.com: Thanks for the complement. Data is stored in wp_options table; it's the only place you really can w/o adding a new taxonomy meta table. As for the second question it's orthogonal to this question; i.e. it is about media dialogs and not about custom fields in a taxonomy input. Please ask as another question here on WordPress Answers. –  MikeSchinkel Sep 6 '10 at 20:38
    
@NetConstuctor.com: You might find that this addresses your 2nd question albeit in a way that is probably not what you wanted to hear: core.trac.wordpress.org/ticket/13817 –  MikeSchinkel Sep 6 '10 at 21:11
    
@MikeSchinkel: Great answer, thank you. Extra info: if you need to do stuff when a term gets deleted, here's the hook you need delete_term. –  Geert Mar 13 '12 at 14:25
add comment

Came across this post in my quest for likely the same thing, and not long after came across this plug in: Ultimate Taxonomy Manager . I'm not endorsing it just yet, but I've tried it in a test environment and I think it does what you're looking for. It feels a bit clumsy when retrieving the data, but that could just be me and my understanding of the documentation.

share|improve this answer
add comment

I think saving custom taxonomy meta/extra fields/custom fields in option table is something killing your site performance. Like if your site is heavy weighted(lots of visitor, lots of hits) then for large number of custom taxonomy the option table will be huge. and it will increase the load time for get_option() query for other plugins or I think this will hamper performance a lot. we should save unnecessary things in option table or increase rows in option table.

share|improve this answer
add comment

Your trying to modify the taxonomy by adding something that isn't a taxonomy, which will only confuse.

Instead you'll need to add a custom meta box to the article post type, and save the urls and image urls in custom fields. Then you would use the code to add columns in functions.php to look up those custom fields on the relevant post using get_meta

share|improve this answer
add comment

A simpler and easier way would be:

WordPress Taxonomies Extra Fields the easy way - Bainternet

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.