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've been able to leverage this super tutorial by @Bainternet in order to add custom fields for taxonomies. I've tweaked the markup, and added a separate callback to add fields to the "Add New Category" admin page. My code is at the end of this question.

I've run into a gotcha though; the the custom field input is showing up on the add new category page, but the field is not saved.

I have noticed that the add new taxonomy page uses ajax, and that could be part of the problem. I tried disabling JS, and still run into the same issue. I've been digging around the core looking for a hook, but can't find one. There are various tutorials on the net, but they all seem to rely on the taxonomy edit screen to do their bidding.

Any ideas on how to make this work?

/**
 * Add extra fields to custom taxonomy edit and add form callback functions
 */
// Edit taxonomy page
function extra_edit_tax_fields($tag) {
    // Check for existing taxonomy meta for term ID.
    $t_id = $tag->term_id;
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="cat_Image_url"><?php _e( 'Category Navigation Image URL' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[img]" id="term_meta[img]" value="<?php echo esc_attr( $term_meta['img'] ) ? esc_attr( $term_meta['img'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter the full URL to the navigation image used for this category.' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'category_edit_form_fields', 'extra_edit_tax_fields', 10, 2 );

// Add taxonomy page
function extra_add_tax_fields( $tag ) {
    // Check for existing taxonomy meta for term ID.
    $t_id = $tag->term_id;
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <div class="form-field">
        <label for="cat_Image_url"><?php _e( 'Category Navigation Image URL' ); ?></label>
        <input type="text" name="term_meta[img]" id="term_meta[img]" value="<?php echo esc_attr( $term_meta['img'] ) ? esc_attr( $term_meta['img'] ) : ''; ?>">
        <p class="description"><?php _e( 'Enter the full URL to the navigation image used for this category.' ); ?></p>
    </div>
<?php
}
add_action( 'category_add_form_fields', 'extra_add_tax_fields', 10, 2 );

// Save extra taxonomy fields callback function.
function save_extra_taxonomy_fields( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}   
add_action( 'edited_category', 'save_extra_taxonomy_fields', 10, 2 );   
//add_action( '...Can't find hook to enable saving custom fields when adding a new term
share|improve this question
add comment

4 Answers 4

up vote 3 down vote accepted

Solved!

add_action( 'create_category', 'save_extra_taxonomy_fields', 10, 2 );   

Props to the Category Meta plugin. I downloaded various category meta plugins from the WP repo, and this one has the ability to add the meta data on the Add New screen. I dug through the code and found the elusive create_{term} hook.

share|improve this answer
add comment

You may go for taxonomy mangager plugin. You can add a new taxonomy,plus add custom fields to the chosen taxonomy ( eg image field)

share|improve this answer
add comment

I think the tutorial you referenced was great and I've based this code off it. It stores the metas for taxonomy menu_category on the add and edit forms for that taxonomy, and displays it in the edit form. The options table entry would be taxomomy_24_metas for the term id 24.

add_action('menu_category_edit_form_fields','menu_category_edit_form_fields');
add_action('menu_category_add_form_fields','menu_category_edit_form_fields');
add_action('edited_menu_category', 'menu_category_save_form_fields', 10, 2);
add_action('created_menu_category', 'menu_category_save_form_fields', 10, 2);

function menu_category_save_form_fields($term_id) {
    $meta_name = 'order';
    if ( isset( $_POST[$meta_name] ) ) {
        $meta_value = $_POST[$meta_name];
        // This is an associative array with keys and values:
        // $term_metas = Array($meta_name => $meta_value, ...)
        $term_metas = get_option("taxonomy_{$term_id}_metas");
        if (!is_array($term_metas)) {
            $term_metas = Array();
        }
        // Save the meta value
        $term_metas[$meta_name] = $meta_value;
        update_option( "taxonomy_{$term_id}_metas", $term_metas );
    }
}

function menu_category_edit_form_fields ($term_obj) {
    // Read in the order from the options db
    $term_id = $term_obj->term_id;
    $term_metas = get_option("taxonomy_{$term_id}_metas");
    if ( isset($term_metas['order']) ) {
        $order = $term_metas['order'];
    } else {
        $order = '0';
    }
?>
    <tr class="form-field">
            <th valign="top" scope="row">
                <label for="order"><?php _e('Category Order', ''); ?></label>
            </th>
            <td>
                <input type="text" id="order" name="order" value="<?php echo $order; ?>"/>
            </td>
        </tr>
<?php 
}
share|improve this answer
add comment
add_filter('created_term', 'update_{custom_taxonomy}_fields'); // you can name your callback function  as you like
function update_{custom_taxonomy}_fields($term_id) {
    if($_POST['taxonomy'] == '{custom_taxonomy}'): // a simple check to see if is you tax
        //do your update (I prefer in options table, but you can do it in any way you`d like)
    endif;
}

if you want a more general hook, you can use the "created_term" (at least from 3.4)

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.