Default WordPress taxonomy (Tag) - How to add a custom field to form and save it to the database

This adds the field to the add new tag formfunction tag_add_form_fields ( $taxonomy ){?><div class="form-fiel

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

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

Share Improve this question edited May 29, 2017 at 21:01 valentina lepcevic asked May 29, 2017 at 19:22 valentina lepcevicvalentina lepcevic 211 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 5

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

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' );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745424615a4627117.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信