1

Can I add some custom fields to specific taxonomy so when ever I call that taxonomy, custom fields attached to that taxonomy should also appear.

2 Answers 2

2

You can hook into the edit form field action of that specific taxonomy.

If you look in /wp-admin/edit-tag-form.php there are hooks for every taxonomy. Starting at Line 69:

        if ( 'category' == $taxonomy )
        do_action('edit_category_form_fields', $tag);
    elseif ( 'link_category' == $taxonomy )
        do_action('edit_link_category_form_fields', $tag);
    else
        do_action('edit_tag_form_fields', $tag);

    do_action($taxonomy . '_edit_form_fields', $tag, $taxonomy);

So for a custom taxonomy, it would be $taxonomy_edit_form_fields with $taxonomy being the taxonomies name. So you would add the hook, within the function, add your form fields. Then you also need to hook into the 'edit_term' action and if your form fields aren't empty then save it as an option using update_option().

This won't allow you to just "call the taxonomy" and have them already attached to the object, but you can loop through your terms once you get them and get the option associated with that term very easily.

0

I developed a small script that simplifies the process of adding custom fields to taxonomies (both core & custom), and optionally lets you add columns to the terms table for each field. The script is called amarkal-taxonomy, and is part of the Amarkal WordPress framework.

Using amarkal-taxonomy, adding a custom field simplifies to:

// Add a text field to the 'category' taxonomy 'add' & 'edit' forms:
amarkal_taxonomy_add_field('category', 'cat_icon', array(
    'type'        => 'text',
    'label'       => 'Icon',
    'description' => 'The category\'s icon',
    'table'       => array(
        'show'      => true,  // Add a column to the terms table
        'sortable'  => true   // Make that column sortable
    )
));

// Then you can retrieve the data using:
$icon = get_term_meta( $term_id, 'cat_icon', true );

You can change 'category' to whatever your taxonomy name is.

6
  • hmmm to answer an old question just to link to your own product borders spam Commented Mar 22, 2017 at 16:09
  • @MarkKaplun It's an open source script that solves the problem that is addressed in the question. Nothing spammy about it. Commented Mar 22, 2017 at 16:14
  • your answer adds nothing to the accepted answer, and therefor it seems like its whole purpose of existence is SEO. Sane answer could have been given by the authors of ACF, CMB2 or whatever other frameworks are there, but none of them has done that.... Commented Mar 22, 2017 at 17:18
  • @MarkKaplun many answers on stackexchange offer solutions to questions using a certain framework. If the author of another framework (like ACF) would have done the same, there would be nothing inappropriate about it. Commented Mar 22, 2017 at 18:01
  • If the answer can help anyone who stumbles upon this question, then the answer has served its purpose. I'm sure that many other developers who are looking for a way to add custom fields to taxonomies would appreciate an approach that doesn't involve writing 100+ lines of code. Commented Mar 22, 2017 at 18:16

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.