1

This adds the field to the add new tag form

function tag_add_form_fields ( $taxonomy ){
    ?>
    <div class="form-field term-colorpicker-wrap">
        <label for="term-colorpicker">Category Color</label>
        <input type="color" name="_tag_color" value="#737373" class="colorpicker" id="term-colorpicker" />
        <p>This is the field description where you can tell the user how the color is used in the theme.</p>
    </div>
        <?php 
}
add_action('add_tag_form_fields','tag_add_form_fields');

This adds the field to the edit tag form

function tag_edit_form_fields ( $term ) {

    $color = get_term_meta( $term->term_id, '_tag_color', true );
    $color = ( ! empty( $color ) ) ? "#{$color}" : '#737373';

?>
    <tr class="form-field term-colorpicker-wrap">
        <th scope="row"><label for="term-colorpicker">Severity Color: <?php echo $color; ?></label></th>
        <td>
            <input type="color" name="_tag_color" value=" <?php echo $color; ?>" class="colorpicker" id="term-colorpicker" />
            <p class="description">This is the field description where you can tell the user how the color is used in the theme.</p>
        </td>
    </tr>

    <?php
 }
add_action('edit_tag_form_fields','tag_edit_form_fields');

This is the non-working part Saving and pulling data from the database

function save_termmeta_tag( $term_id ) {

     // Save term color if possible
    if( isset( $_POST['_tag_color'] ) && ! empty( $_POST['_tag_color'] ) ) {
        update_term_meta( $term_id, '_tag_color', sanitize_hex_color_no_hash( $_POST['_tag_color'] ) );
    } else {
        delete_term_meta( $term_id, '_tag_color' );
    }

}

add_action( 'created_tag', 'save_termmeta_tag' );
add_action( 'edited_tag',  'save_termmeta_tag' ); 

I guess action hooks are not correct.

Just to mention, the code is originally from another posted question. I just tweaked it to fit my needs.

Adding Colorpicker Field To Category

2

For updating and saving use add_action( 'edit_term', 'save_termmeta_tag' );

  • This answer should be selected. This solved my problem. – Himadri Dec 7 '18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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