8

Edit: Important note for WordPress v4.4 onward

This question used a great workaround for the lack of real term meta in older versions of WordPress, but since the release of WordPress v4.4, real term meta has been introduced, and that's definitely the way to go now.

Here's a post that looks like a good starting point for term meta in WordPress 4.4+

I'm leaving this question up because it could still be useful for someone dealing with older code, but wanted to add the note above so that developers starting on new implementations of term meta would be on the right track.

End of edit


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

5 Answers 5

6

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.

3
  • I stumbled across your post in an attempt to unravel why my code isn't working as intended - wordpress.stackexchange.com/questions/417562/…. I'm using the majority of what you have listed but for some odd reason, the term meta doesn't save the first time around but does for every other submission thereafter. I would appreciate it if you could point me in the right direction.
    – Motivated
    Commented Jul 21, 2023 at 10:44
  • Hi @Motivated, the code posted here is very outdated and shouldn't be used anymore. WP 4.4 introduced the Term Meta API which should be used instead. Here's an article covering how it can be used: smashingmagazine.com/2015/12/… Commented Jul 22, 2023 at 17:30
  • thanks. The code example I posted used the Term Meta API but for some odd reason, it doesn't fire the first time it's the custom field is posted.
    – Motivated
    Commented Jul 23, 2023 at 5:10
6

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 
}
1
  • No longer works. Please update. Commented May 11, 2017 at 1:01
3

You can add custom field to the add new term page screen through following hooks:

  1. {taxonomy_name}_add_form_fields
  2. {taxonomy_name}_edit_form_fields
  3. edited_{taxonomy_name}
  4. create_{taxonomy_name}

enter image description here

1
  • Link is broken! Commented May 8, 2019 at 21:23
1
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)

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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