{"id":100,"date":"2018-05-14T04:31:15","date_gmt":"2018-05-14T04:31:15","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/05\/14\/add-color-picker-in-magento-2-admin-configuration\/"},"modified":"2025-05-22T17:31:18","modified_gmt":"2025-05-22T12:01:18","slug":"add-color-picker-in-magento-2-admin-configuration","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-color-picker-in-magento-2-admin-configuration\/","title":{"rendered":"How to Add Color Picker in Magento 2 Admin Configuration"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes while creating an extension, you may need to provide admin the complete control of the frontend UI. Changing UI includes the change of background color, font colors which arises the need of adding color picker in Magento 2 Admin Configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, the settings in a Magento 2 extension resides under Stores \u2013&gt; Configuration and here you need to add the color picker to allow change the color in frontend UI. Here, I\u2019ll show to&nbsp;<em><strong>add color picker in Magento 2 Admin Configuration&nbsp;<\/strong><\/em>in just three steps!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Add Color Picker in Magento 2 Admin Configuration:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Add color picker to textbox through&nbsp;<strong>system.xml<\/strong>&nbsp;file located at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\etc\\adminhtml<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n       xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n    &lt;system>\n        &lt;section>\n            &lt;group id=\"my_color_group\" ...>\n                &lt;field id=\"my_color_option\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\"\n                       showInWebsite=\"1\" showInStore=\"1\">\n                    &lt;label>Background Color&lt;\/label>\n                    &lt;comment>&lt;![CDATA[Background color]]>&lt;\/comment>\n                    &lt;frontend_model>Vendor\\Module\\Block\\Color&lt;\/frontend_model>\n                &lt;\/field>\n            &lt;\/group>\n        &lt;\/section>\n    &lt;\/system>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create one&nbsp;<strong>Color.php<\/strong>&nbsp;file at below location under the hood of extension&nbsp;<strong><strong>app\\code\\Vendor\\Module\\Block<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Block;\nuse Magento\\Config\\Block\\System\\Config\\Form\\Field;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Framework\\Registry;\nuse Magento\\Framework\\Data\\Form\\Element\\AbstractElement;\nclass Color extends Field\n{\n    protected $_coreRegistry;\n    public function __construct($context, Registry $coreRegistry, array $data = [])\n    {\n        $this->_coreRegistry = $coreRegistry;\n        parent::__construct($context, $data);\n    }\n    protected function _getElementHtml(AbstractElement $element)\n    {\n        $html = $element->getElementHtml();\n        $cpPath = $this->getViewFileUrl('Vendor_Module::js');\n        if (!$this->_coreRegistry->registry('colorpicker_loaded')) {\n            $html .= '&lt;script type=\"text\/javascript\" src=\"' . $cpPath . '\/' . 'jscolor.js\">&lt;\/script>';\n            $this->_coreRegistry->registry('colorpicker_loaded', 1);\n        }\n        $html .= '&lt;script type=\"text\/javascript\">\n        var el = document.getElementById(\"' . $element->getHtmlId() . '\");\n        el.className = el.className + \" jscolor{hash:true}\";\n        &lt;\/script>';\n        return $html;\n    }\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Add the JS file at&nbsp;<strong>Vendor_Module\/view\/adminhtml\/web\/js\/jscolor.js,&nbsp;<\/strong>click here to copy&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2018\/05\/jscolor.js\">jscolor.js<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Clear the cache and navigate to stores configuration. The color picker option will be displayed. Use this color picker anywhere in Magento 2 with your own customized code when needed!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes while creating an extension, you may need to provide admin the complete control of the frontend UI. Changing UI includes the change of background&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":15656,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/100\/revisions\/15656"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}