Magento Web Development

How to add a color picker in the Magento admin or anywhere

Greetings, developers of all colors!

First of all this tutorial assumes that you already know how to make a Magento extension. Now there are many ways to add a color picker in the Magento admin (or anywhere). Here’s one “correct” and “extendable” way to do it:

First, we would need a color picker. This color picker should not use any JavaScript framework so it fits in easily with Prototype, which is used in Magento. If you don’t already know this: Magento has switched to jQuery from version 2 onwards. So it would be nice to have a color picker that would work basically anywhere.

If you’ve spent about 10 seconds trying to look for a color picker, you’ve probably stumbled upon JsColor. I’ve read a few posts that show you how to integrate it into your Magento admin, but as always, I’ve not found anything that is thorough enough. So here’s my effort to simplify it for you. Here is a bare-bones extension to help you understand how this works:

Step 1: Download this file and extract it to /app/code/local
Step 2: Download this file and extract it to /app/etc/modules
Step 3: Download this file and extract is to /app/design/adminhtml/default/default/layout
Step 4: Download this file and extract it to /js

And you’re done! (Don’t forget to clear all caches, log out and log in etc.) . Navigate to System -> Configuration -> Your Module and choose your color!


First, we add the JSColor file in the admin head using this file: /app/design/adminhtml/default/default/layout/yourmodule.xml

Next, let’s explore the file: /app/code/local/Yourcompany/Yourmodule/Block/Adminhtml/System/Config/Color.php

The magic happens in this line:

$color->addClass( 'color ' . $element->getClass() );

JSColor requires that the input field have a class “color” for it to work. So we add it here in this line, along with any other classes that might be required on the field. The other classes might also include classes entered in the “validate” section of your field.

In the file /app/code/local/Yourcompany/Yourmodule/etc/system.xml

<my_color translate="label">
	<label>My Color</label>

The “required-entry” class is added to the field after the “color” class since it is added inside “validate”.

But wait …

I can’t see the JSColor’s image when I select the color! If you can, ignore the following.

Magento has a “Merge JavaScript” option under System -> Advanced -> Developer . When this option is enabled, JSColor would break, and not show the color picker image because it can’t find the JSColor directory where the image is located. Which is why we’ve modified the JSColor file at /js/yourcompany/jscolor/jscolor.js

dir : BLANK_URL.replace( 'blank.html', 'yourcompany/jscolor/' )

Magento has a JavaScript variable called “BLANK_URL” both in the admin and front end. It points to /js/blank.html . We can use this to find out the path to the JS folder. So what we’ve done is removed “blank.html” and replaced it with “yourcompany/jscolor/” so the string would now be /js/yourmodule/jscolor/ which can now be used by JSColor to load the image.

Happy coloring!

8 replies on “How to add a color picker in the Magento admin or anywhere”

Hi, I need to include in color picker in new product creation page under custom options. How can i includes your code to get the corresponding results ?

Hi Nanda,
In the layout file you can change “adminhtml_system_config_edit” to “adminhtml_catalog_product_edit” . This will make the JS file load in the product edit page.

After that, just use the fields from system.xml . I’m sorry I can’t go into detail with this.

Hi there,

I get the following error on my modules section:
Notice: Undefined variable: format in /var/www/ on line 12.

Where does this variable refer to? I can’t see a definition for $format in the code anywhere.

Leave a Reply

Your email address will not be published. Required fields are marked *