Skip to content

Instantly share code, notes, and snippets.

@isotrope
Created December 3, 2014 15:05
Show Gist options
  • Save isotrope/b9eb6953022f738b95b5 to your computer and use it in GitHub Desktop.
Save isotrope/b9eb6953022f738b95b5 to your computer and use it in GitHub Desktop.
Add a colour picker to taxonomies
<?php
/**
* Colour meta box for taxonomies
*
* You know... for kids...
* ...and also for things like labels
*
*/
class Iso_Taxonomy_Colours {
/**
* @var array Holds our array of taxonomies
*/
private $taxonomies;
/**
* @param mixed $tax_array
*/
function __construct( $tax_array ) {
$this->taxonomies = $tax_array;
// Add the iris script among other things
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Bind to add/edit screens
add_action( 'init', array( $this, 'bind_metaboxes' ) );
// Custom columns to show the colour
add_action( 'init', array( $this, 'add_tax_colour_columns_filters' ) );
}
/**
* Enqueues our scripts during the 'admin_enqueue_scripts' hook
*/
function enqueue_scripts() {
/**
* TODO: Figure out this damn undocumented new script. Damn youuuuu, Automattic!
*/
wp_enqueue_script( 'iris' );
}
/**
* Binds the metaboxes to taxonomy edit screens
*/
function bind_metaboxes() {
if ( is_array( $this->taxonomies ) ) {
foreach ( $this->taxonomies as $taxonomy ) {
add_action( $taxonomy . '_add_form_fields', array( $this, 'add_colour_metabox' ) );
add_action( $taxonomy . '_edit_form_fields', array( $this, 'edit_colour_metabox' ) );
// saving
add_action( 'edited_' . $taxonomy, array( $this, 'save_tax_colour' ), 10, 2 );
add_action( 'create_' . $taxonomy, array( $this, 'save_tax_colour' ), 10, 2 );
}
} else {
add_action( $this->taxonomies . '_add_form_fields', array( $this, 'add_colour_metabox' ) );
add_action( $this->taxonomies . '_edit_form_fields', array( $this, 'edit_colour_metabox' ) );
// saving
add_action( 'edited_' . $this->taxonomies, array( $this, 'save_tax_colour' ), 10, 2 );
add_action( 'create_' . $this->taxonomies, array( $this, 'save_tax_colour' ), 10, 2 );
}
}
/**
* Adds a metabox to any Taxonomy contained in our $taxonomies variable
*/
function add_colour_metabox() {
?>
<tr class="form-field">
<th scope="row"><label for="iso-tax-colour"><?php _e( 'Taxonomy Colour', 'iso2015' ); ?></label></th>
<td><input type="text" size="40" value="" id="iso-tax-colour" name="iso-tax-colour">
<p class="description"><?php _e( 'Select a colour to associate with this term', 'iso2015' ); ?></p></td>
</tr>
<?php
}
/**
* Adds a metabox to any Taxonomy contained in our $taxonomies variable
*
* @param object $term term object coming from $this->taxonomies . '_edit_form_fields'
*/
function edit_colour_metabox( $term ) {
$curr_value = get_option( $term->taxonomy . '_' . $term->term_id . '_tax-colour' );
?>
<tr class="form-field">
<th scope="row"><label for="iso-tax-colour"><?php _e( 'Taxonomy Colour', 'iso2015' ); ?></label></th>
<td><input type="text" size="40" value="<?php echo $curr_value; ?>" id="iso-tax-colour" name="iso-tax-colour">
<p class="description"><?php _e( 'Select a colour to associate with this term', 'iso2015' ); ?></p></td>
</tr><?php
}
/**
* Saving our taxonomy's custom colour
*
* @param int $term_id comes from the 'edited_' . $this->taxonomies and 'create_' . $this->taxonomies hooks
*/
function save_tax_colour( $term_id ) {
if ( isset( $_POST['iso-tax-colour'] ) && trim( $_POST['iso-tax-colour'] ) != '' && isset( $_POST['taxonomy'] ) && trim( $_POST['taxonomy'] ) != '' ) {
$value = trim( $_POST['iso-tax-colour'] );
$taxonomy = trim( $_POST['taxonomy'] );
/**
* TODO: turn on sanitation once I figure out the iris JS
*/
//Check if it's a hexadecimal value
//if ( $this->is_hex( $value ) ) {
update_option( $taxonomy . '_' . $term_id . '_tax-colour', $value );
//}
}
}
/**
* Hook into the Taxonomy list screen to add a column and display the custom colour inside it
*/
function add_tax_colour_columns_filters() {
if ( is_array( $this->taxonomies ) ) {
foreach ( $this->taxonomies as $taxonomy ) {
add_filter( 'manage_edit-' . $taxonomy . '_columns', array( $this, 'add_tax_colour_columns' ) );
add_filter( 'manage_' . $taxonomy . '_custom_column', array(
$this,
'display_tax_colour_column'
), 10, 3 );
}
} else {
add_filter( 'manage_edit-' . $this->taxonomies . '_columns', array( $this, 'add_tax_colour_columns' ) );
add_filter( 'manage_' . $this->taxonomies . '_custom_column', array(
$this,
'display_tax_colour_column'
), 10, 3 );
}
}
/**
* @param array $columns comes from the 'manage_edit-' . $taxonomy . '_columns' hook
*
* @return mixed
*/
function add_tax_colour_columns( $columns ) {
$columns['iso-tax-colour'] = __( 'Tax Colour', 'iso2015' );
return $columns;
}
/**
*
* Display the custom colour in our custom column
*
* @param string $string_out need to figure this one out better. Needs to be returned by the function.
* @param string $column_name column name currently being processed
* @param int $term_id term_id currently being processed
*
* @return mixed
*/
function display_tax_colour_column( $string_out, $column_name, $term_id ) {
if ( $column_name == 'iso-tax-colour' ) {
if ( isset( $_GET['taxonomy'] ) ) {
$taxonomy = trim( $_GET['taxonomy'] );
$colour = get_option( $taxonomy . '_' . $term_id . '_tax-colour' );
if ( $colour && $colour != '' ) {
$colour = str_replace( '#', '', $colour );
echo '<span style="background-color: #' . $colour . '; width: 60px; height: 20px; display:inline-block; text-align: center;">#' . $colour . '</span>';
}
}
}
return $string_out;
}
/**
*
* Hexadecimal sanitation
*
* @param string $hex_code Expects a hexadecimal code
*
* @return bool returns true if it is a valid hexadecimal code
*/
function is_hex( $hex_code ) {
return @preg_match( "/^[a-f0-9]{2,}$/i", $hex_code ) && ! ( strlen( $hex_code ) & 1 );
}
}
// pre-declaring to illustrate.
$iso_taxonomies_to_apply_to = array(
'categories', // built-in Categories taxonomy
'post_tags', // built-in Tags taxonomy
'iso_wp_functions', // Custom taxonomy
);
$iso_taxonomy_colours = new Iso_Taxonomy_Colours( $iso_taxonomies_to_apply_to );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment