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 badges2 Answers
Reset to default 5For 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:
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; ?>"
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条)