New Color Picker in WP 3.5
You may have noticed that WordPress 3.5 is sporting a new color picker. We originally developed it for the Custom Colors feature on WordPress.com. We offered it to WordPress Core, and 4 months, eleventy million tests and iterations later, there’s a shiny new, HiDPI-ready, CSS gradient-based color picker called Iris in your WordPress. But how do you use it?
The easiest way possible is in the Customizer. Keep registering your color controls the same way you always have and they’ll now sport a slick new Iris-based color picker interface. Otto’s great Theme Customizer tutorial shows you how to register controls, including a color control, so I won’t repeat that information here.
Want to use the new color picker somewhere else? Here’s a quick guide:
1. Enqueue the ‘wp-color-picker’ script and style
add_action( 'admin_enqueue_scripts', 'mw_enqueue_color_picker' );
function mw_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( 'my-script-handle', plugins_url('my-script.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}
My plugin now has a my-script.js file that has declared wp-color-picker as a dependency. We can now use the wpColorPicker jQuery method inside it.
2. Add an input to your admin page
I’m not going to tell you how to code an admin page. Somewhere on it, you’ll want:
<input type="text" value="#bada55" class="my-color-field" />
You can also prime a default color for the field if you want:
<input type="text" value="#bada55" class="my-color-field" data-default-color="#effeff" />
3. Call ‘wpColorPicker’ from your script
Remember that we enqueued my-script.js above? Open it up, and be prepared for some long coding:
jQuery(document).ready(function($){
$('.my-color-field').wpColorPicker();
});
What, you wanted more? There’s a lot more you can do, but that will get the job done for most cases. (Note that only IE8 and up are supported. IE7 and under will fail gracefully, leaving you with a perfectly usable text input.)
4. Advanced usage
wpColorPicker supports a few options. You can supply an options object when calling it:
var myOptions = {
// you can declare a default color here,
// or in the data-default-color attribute on the input
defaultColor: false,
// a callback to fire whenever the color changes to a valid color
change: function(event, ui){},
// a callback to fire when the input is emptied or an invalid color
clear: function() {},
// hide the color picker controls on load
hide: true,
// show a group of common colors beneath the square
// or, supply an array of colors to customize further
palettes: true
};
$('.my-color-field').wpColorPicker(myOptions);
5. More advanced usage
wpColorPicker is set of UI controls wrapped around the underlying Iris color picker. We did things this way so that, if a better color picker comes along, we can more easily swap it out. But, if you want to just play with Iris itself, just declare iris as your script dependency instead. I’m not going to document the options here, but Iris’s GitHub repo should see some actual docs in the future.
Alex Mills (Viper007Bond) 9:50 pm on November 30, 2012 Permalink | Log in to Reply
So awesome.
Quick link to an Iris demo: http://automattic.github.com/Iris/
Emil Uzelac 9:51 pm on November 30, 2012 Permalink | Log in to Reply
Very nice!
Jose Castaneda 10:16 pm on November 30, 2012 Permalink | Log in to Reply
Awesome!
designsimply 6:42 am on December 1, 2012 Permalink | Log in to Reply
This rocks! I love the story behind Iris too.
cubricks 7:25 am on December 1, 2012 Permalink | Log in to Reply
Amazing! Exactly what I needed for my theme, thanks guys.
Gijs Jorissen 9:11 pm on December 1, 2012 Permalink | Log in to Reply
Nice work on the new colorpicker guys!! And ofcourse the rest of WP 3.5
WebYip 11:25 pm on December 9, 2012 Permalink | Log in to Reply
New color picker= #bada55! Great work.
a.hoereth 2:25 am on December 11, 2012 Permalink | Log in to Reply
Really like the new color picker. Is there a recommandation on how to add an fallback to the farbtastic color picker when using them manually for an options page?
My attempt on this at the moment: http://d.pr/n/j843
Matt Wiebe 3:21 am on December 11, 2012 Permalink | Log in to Reply
@a.hoereth That would be a decent approach if you already have a script that integrates farbtastic. Otherwise, you could copy over the script and style from core and conditionally register them with the same handles.
a.hoereth 10:44 am on December 11, 2012 Permalink | Log in to Reply
Yea my approach works fine, it just sucks that I now have 3 differen js files, one for colorpicker =3.5 and a general one for all version with all my other stuff.. Already took a look at the idea of shipping iris with my plugin for older WordPress versions, but I just needed for one input field and do not think it is worth the hassle.