Back to Top

How to add extra field in Custom Taxonomy WordPress?

To create extra field into category or Custom Taxonomy in Wordpress

Today I am going to explain about to create extra field into category section or custom Taxonomy in WordPress.

Sometimes in WordPress, we need some extra fields into category or custom taxonomy like an image for a category or detailed description etc. So we thanks to WordPress that provides action hooks using that we can create action hooks in that without any plugin.

Yes, without a plugin, It’s better to code itself because plugin might have some extra code or code which can’t support in next version of WordPress. So try to minimize the use of plugins in WordPress and code by yourself. It’s quite easy to write such a code in WordPress.It may take few mins.

Let’s check out code and understand:

First of all, let’s create action hook for adding html for new field into category or Custom Taxonomy.

Read about :To Configure Auto Updates in WordPress

add_action('{$taxonomy}_edit_form_fields','edit_form_fields'); 
add_action('{$taxonomy}_edit_form', 'edit_form'); 
add_action('{$taxonomy}_add_form_fields','edit_form_fields');
add_action('{$taxonomy}_add_form','edit_form');

function edit_form() {
 // your desired code
}

function edit_form_fields ($tag) {
    $termid = $tag->term_id; 
    $cat_meta = get_option( "tax_$termid"); 
?>
  
            
                
            
            
    
Entering detail description php }

Next,action hooks are for saving that new field into a database after created or updated.

Also Read: Customize Search in WordPress

// when the form gets submitted, and the new field gets updated (in your case the option will get updated with the values of your custom fields above

add_action ( 'edited_{$taxonomy}', 'save_extra_fileds');
add_action('created_{$taxonomy}','save_extra_fileds');
// save extra category extra fields callback function

function save_extra_fileds( $term_id ) {
    if ( isset( $_POST['description_detail'] ) ) {
        $termid = $term_id;
        $cat_meta = get_option( "tax_$termid");
        if ($cat_meta !== false ) {
            update_option(  "tax_$termid",$_POST['description_detail']  );
        }else{
             add_option(  "tax_$termid",$_POST['description_detail'] ,  '', 'yes'  );
        }
    }
}    

well, If you create some fields in the term, then you need to remove that field when delete term. so next filter hook is about for same.

// when a category is removed
add_filter('deleted_term_taxonomy', 'remove_tax_Extras');
function remove_tax_Extras($term_id) {
    $termid = $term_id; 
      if($_POST['taxonomy'] == '{$taxonomy}'):
        if(get_option( "tax_$termid"))
            delete_option( "tax_$termid");
      endif;
}

Below action hooks are to display newly created field into display section of taxonomy which is dependent on a version of WordPress too.

add_filter( 'manage_edit-{$taxonomy}_columns', 'taxonomy_columns_type');
add_filter( 'manage_{$taxonomy}_custom_column', 'taxonomy_columns_type_manage', 10, 3);

function taxonomy_columns_type($columns) {
        $columns['keywords'] = __( 'Detailed Description', 'dd_tax' );
        return $columns;
    }
function taxonomy_columns_type_manage( $out ,$column_name, $term) {
    global $wp_version;
    $out =  get_option( "tax_$termid"); 
    if(((float)$wp_version)<3.1)
        return $out;
    else
        echo $out;        
}

That’s it. Like I said, quick but exceptionally handy.All you need to do is just copy and paste above code into your theme’s functions.php file and replace {$taxonomy} with your desired taxonomy.

As always, thanks for reading. Don't Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (5)

  1. Uday Ghulaxe says:

    I want to attach a upload image field to taxonomies

  2. Hi there, thanks for the article! I was just wondering, where do I add this code, how would I upload images and then how would I get the image in a template!?

    Thanks for your help!

    1. I have already mention in post where to put code. pl read again.

  3. Thanks for the guide. Very useful.

    There is a slight typo in the last function declaration to show the value in the display section, it should be:

    function taxonomy_columns_type_manage( $out ,$column_name, $termid) {

  4. i want to know how to show output extra fields value?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

MySQL FULLTEXT Index and Search

Posted on 3 years ago

Bhumi

How to use media upload in WordPress form?

Posted on 5 years ago

Bhumi

How To Send Email in MODx

Posted on 6 years ago

Bhumi

Problem with German special character

Posted on 7 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi