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
0

Actually I also wanted to use it now. Basically this is the only place in the internet to actually work for Tags.

I have found some troubles with it, managed to edit it and it works for me now:

  1. Here you are saving also SPACE character to the value

        <input type="color" name="_tag_color" value=" <?php echo $color; ?>" 
    

-> deleted space

<input type="color" name="_tag_color" value=" <?php echo $color; ?>"
  1. I have found out that even with the helping from Ben. The saving still do not works and somehow your default color is there.. I found out by luck - but I do not now why, the sanitizing method is messing up this causing not to save it.

    sanitize_hex_color_no_hash()

So basically If I removed it, it started to work normally.

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',  $_POST['_tag_color'] );
    } else {
        delete_term_meta( $term_id, '_tag_color' );
    }

}

SO no need for add_action( 'edit_term', 'save_termmeta_tag' );

UPDATE Later I found out that if I sanitize it before putting it to another method, also helps

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

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.