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 think I'm pretty close to cracking this nut :)

I'm trying to add a set of custom fields to the Category editor. Since I'm not dealing with post meta, I believe I'll be writing my custom category field values to the wp_term_taxonomy table rather than the wp_options table. Is this correct?

If there are any examples of how to do this, please share a link or bit of code. I'm not sure how to capture and save my custom category fields.

Here's my code...

//add the hook to place the form on the category editor screen
add_action('edit_category_form','ce4_category_admin');

//Adds the custom title box to the category editor
function ce4_category_admin($category){
    echo "category: ".$category->term_id; //great, I got a reference to the term_id, now I need to get/set my custom fields with this key
    ?>
<table class="form-table">
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th>
            <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo get_taxonomy($category->term_id, '_ce4-categoryTitle'); ?>" />
            <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th>
            <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php echo get_taxonomy($category->term_id, '_ce4_fullDescription'); ?></textarea>
            <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td>
        </tr>
</table>
<?php
}

//How to save the custom field data? Normally I'd use..
//add_action('save_post', 'custom_save_function');
//but this is not a post, so perhaps there's another method?
share|improve this question

1 Answer 1

up vote 6 down vote accepted

No. You have to use wp_options, because you can't create new fields in the wp_term_taxonomy table (If you do, in the next WP update you'll loose them).

So:

// the option name
define('MY_CATEGORY_FIELDS', 'my_category_fields_option');

// your fields (the form)
add_filter('edit_category_form', 'my_category_fields');
function my_category_fields($tag) {
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);

    ?>

<table class="form-table">
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th>
            <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id]['my_title']; ?>" />
            <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th>
            <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php  echo $tag_extra_fields[$tag->term_id]['my_description']; ?></textarea>
            <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td>
        </tr>
</table>

    <?php
}


// when the form gets submitted, and the category gets updated (in your case the option will get updated with the values of your custom fields above
add_filter('edited_terms', 'update_my_category_fields');
function update_my_category_fields($term_id) {
  if($_POST['taxonomy'] == 'category'):
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    $tag_extra_fields[$term_id]['my_title'] = strip_tags($_POST['_ce4-categoryTitle']);
    $tag_extra_fields[$term_id]['my_description'] = strip_tags($_POST['_ce4_fullDescription']);
    update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
  endif;
}


// when a category is removed
add_filter('deleted_term_taxonomy', 'remove_my_category_fields');
function remove_my_category_fields($term_id) {
  if($_POST['taxonomy'] == 'category'):
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    unset($tag_extra_fields[$term_id]);
    update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
  endif;
}

There might be some category-related hooks for the last two, I didn't search for them. I just adapted the code above from a old theme of mine in which the admin can attach a image to a custom taxonomy term...

share|improve this answer
    
@AA: Thanks, this works great! How would you retrieve the value for the title when you are viewing the category archive page? –  Scott B Jan 11 '11 at 18:26
    
using get_option(MY_CATEGORY_FIELDS) –  onetrickpony Jan 11 '11 at 19:21
    
gr8 one @OneTrickPony - thanks a lot ;) –  Sagive SEO Apr 13 '12 at 17:45
    
I used this code and it works great. I found that the field I want to add gets put below the add category button. Do you know a way to get the new fields to show above the button? I tried removing the table from the code and just left the tr and td but that didn't work –  Jamie Sep 25 '12 at 1:40
    
yes, apparently WP has new actions now which you can use. Mainly edit_category_form_fields instead of edit_category_form. See edit_tags.php –  onetrickpony Sep 25 '12 at 10:01

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.