Custom fields ‘for’ custom taxonomy?? Can someone explain why?

I’m implementing various custom taxonomies for my project and I came across the concept of custom fields for custom taxonomies, which baffles me. I though custom fields where just custom fields associated to posts and not a taxonomy. Can someone explain this relationship and how it is used? Thanks!

Related posts

Leave a Reply

1 comment

  1. Custom fields for custom taxonomies are nothing more than custom templates. For example, you can remove the template of the post_tag taxonomy via remove_meta_box() function and add custom template via add_meta_box() function.

    Examples

    Remove tags template:

    /* Remove default post tags template. */
    add_action( 'admin_menu' , 'example_remove_taxonomy_template' );    
    
    /**
     * Remove taxonomy template
     * 
     * Removes the defualt meta tags template, in this example, we don't want
     * people to add their own tags, so the whole purpose of this function is 
     * to remove the functionality that allows people to type in their own tags.
     */
    function example_remove_taxonomy_template() {
        remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'side' ); 
    }
    

    The above removes the Tags box. We’ll add it again, but we’ll change the template of the post_tag taxonomy. In this case we’ll remove the input bar where people used to type in their new tag terms, in our new template, people can only choose predefined tags from a dropdown list.

    The template:

    /** 
     * Custom tags template.
     * 
     * Creates a custom taxonomy template. Poeple will no longer be able to type new rubbish tags,
     * they should use the existing ones ;)
     */
    function example_tags_template() {
    
        global $post;
    
        $example_tags = array();
    
        /* Set the post ID if not provided. */
        $post_id = $post->ID;
    
        /* Get the 'given' terms attached to the given post ID. */
        $terms = get_the_terms( $post_id, 'post_tag' );
    
        /* Return the format of each taxonomy. */
        if ( $terms && ! is_wp_error( $terms ) ) {
    
            foreach( $terms as $term )  
                $example_tags = $term->$format;
        }
        $example_tag = join( ", ", $example_tags );
    
        /* Open the container div of the taxonomy. */
        echo'<div class="input-text-wrap" style="margin:5px 0 0">';
    
        /* Get the terms in a select input field. */
        wp_dropdown_categories( 
            array(
                'taxonomy'      => 'post_tag',
                'hide_empty'    => 0,
                'name'          => "example[post_tag]",
                'selected'      => ( $example_tag ? $example_tag : 0 )
            ) 
        );
    
        echo'</div>';
    }
    

    As you can see in the code above, the custom post_tag template is ready and we only need to hook it into WordPress, the code below will do that job.

    /* Add modified custom template post tags taxonomy. */
    add_action( 'add_meta_boxes', 'example_add_taxonomy_template' );
    
    /**
     * Add taxonomy template
     * 
     * Adds post tags taxonomy with custom taxonomy template. People should select 
     * post tags instead of typing them.
     */
    function example_add_taxonomy_template() {
        add_meta_box( 'post_tag', 'Choose a Tag', 'example_tags_template', 'post', 'side', 'low' );
    }
    

    As you can see in the above examples, you can modify the default templates of built-in meta boxes, you can even remove the post excerpt meta box and reattach again to enable WYSIWYG support.

    The above code is not the best example and I’ve written the functions on the fly, but I’ve used this functionality few times when I wanted clients to choose where they want to put their content on the front page; e.g. slideshow area, features area, etc…

    Cheers,