Skip to content

Instantly share code, notes, and snippets.

@ellenmva
Created September 14, 2013 23:20
Show Gist options
  • Save ellenmva/6566585 to your computer and use it in GitHub Desktop.
Save ellenmva/6566585 to your computer and use it in GitHub Desktop.
Create a color meta field
function __construct() {
add_action( 'admin_enqueue_scripts', array(&$this,'em_enqueue_color_picker' ));
add_action('admin_head', array(&$this,'add_colorpicker_script'));
}
function em_enqueue_color_picker( $hook_suffix ) {
// first check that $hook_suffix is appropriate for your admin page
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' , false, true );
}
function add_colorpicker_script() {
global $post;
$custom_meta_fields = $this->your_meta_fields_function(); //where you meta_fields are defined
$output = '<script type="text/javascript">
jQuery(function() {';
foreach ($custom_meta_fields as $field) { // loop through the fields looking for certain color type
if($field['type'] == 'color')
$output .= 'jQuery(".colorpicker").wpColorPicker();';
}
$output .= '});
</script>';
echo $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment