I want to add a custom field to my categories where the user can color code them. I’ve added the field but I want to use built in colorpicker (either tinyMCE or the farbtastic) to give the user an easy way to pick colors. I can’t figure out how to add the functionality yet though, here’s what I have so far:
Category Field Setup
/** Add New Field To Category **/
function extra_category_fields( $tag ) {
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Image Url'); ?></label></th>
<td>
<input type="text" name="Cat_meta[bgc]" id="colorinput" size="3" style="width:20%;" value="<?php echo $cat_meta['bgc'] ? $cat_meta['bgc'] : '#fff'; ?>" class="my-color-field" />
<div id="colorpicker"></div><br />
<span class="description"><?php _e('Can't Think of A Desc Yet, Suggestions?'); ?></span>
<br />
</td>
</tr>
<?php
}
add_action ( 'category_add_form_fields', 'extra_category_fields');
/** Save Category Meta **/
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
add_action ( 'edited_category', 'save_extra_category_fileds');
Colorpicker Script (Farbtastic) – Doesn’t Work
/** Enqueue Color Picker **/
function farbtastic_scripts() {
wp_enqueue_script( 'jQuery' );
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#colorpicker').hide();
jQuery('#colorpicker').farbtastic("#colorinput");
jQuery("#colorinput").click(function(){jQuery('#colorpicker').slideToggle()});
});
</script>
<?php
}
add_action( 'admin_enqueue_scripts', 'farbtastic_scripts' );
:: Edit :: If it makes it any easier, I do have the “Advanced Custom Fields” plugin which has a colorpicker option. I’m looking to see if it would be easier to use that.
This has been updated for WordPress 4+ with the introduction of Term Meta. The code is heavily commented.
:: My Functions – functions.php ::
The below function is to display the colorpicker on the “Add New Category” screen:
The below function will add the colorpicker field to the “Edit Category” screen:
The below function will sanitize and save the term meta without the
#
but there is a separate function which will sanitize it with the hash calledsanitize_hex_color()
:Now that the fields are added and data is being saved we need to enqueue the script(s) that put the colorpicker in place. Do note that in the first conditional I test against the taxonomy I’m showing the field on.
Finally, we need to initialize the colorpicker. Here I’ve chosen to just print it into the footer but it may be more beneficial, especially from a plugin perspective, to put this in an external file and enqueue it like a normal script.