{"id":90,"date":"2018-04-23T12:57:00","date_gmt":"2018-04-23T12:57:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/04\/23\/create-a-custom-widget-in-magento-2\/"},"modified":"2025-05-22T17:31:48","modified_gmt":"2025-05-22T12:01:48","slug":"create-a-custom-widget-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-a-custom-widget-in-magento-2\/","title":{"rendered":"How to Create a Custom Widget in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Basically, Magento widget is a Magento extension with definite functionality in your store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default Magento supports the following types of widget:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>categories<\/li>\n\n\n\n<li>tag cloud<\/li>\n\n\n\n<li>navigation menu<\/li>\n\n\n\n<li>calendar<\/li>\n\n\n\n<li>search<\/li>\n\n\n\n<li>recent posts, etc.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The Magento widgets assist in better user experience and functionality of the store. Apart from these default widgets and its functionality, you may need advanced features. Creating a&nbsp;<strong><em>c<\/em><em>ustom&nbsp;<\/em><\/strong><em><strong>widget in Magento 2<\/strong><\/em>&nbsp;is like developing an independent extension. Customize a widget to extend the functionality of the core widgets or create your own<em><strong>&nbsp;custom widget in Magento 2.<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Widgets are useful in the administration to add interactive interfaces and features in the front-end of Magento 2 store. Learn&nbsp;<em><strong>how to create a custom widget in Magento 2<\/strong><\/em>&nbsp;by following the steps assembled here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code to Create Custom Widget in Magento 2:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">File setup\/Our module<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Initially, we need to create the module setup file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create file&nbsp;<em><strong>app\/code\/Meetanshi\/CustomWidget\/etc\/module.xml.&nbsp;<\/strong><\/em>Paste the following code snippet in that file.<\/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;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/Module\/etc\/module.xsd\">\n    &lt;module name=\"Meetanshi_CustomWidget\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Widget Declaration File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create the widget file<strong>&nbsp;<em>app\/code\/Meetanshi\/CustomWidget\/etc\/widget.xml<\/em>&nbsp;<\/strong>with content.<\/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;?xml version=\"1.0\" encoding=\"UTF-8\"?> \n&lt;widgets xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/Magento\/Widget\/etc\/widget.xsd\">\n   &lt;widget id=\"meetanshi_customwidget\" class=\"Meetanshi\\CustomWidget\\Block\\Widget\\ContactInformations\">\n      &lt;label translate=\"true\">Contact Informations Widget&lt;\/label>\n      &lt;description>Widget in Magento2&lt;\/description>\n      &lt;parameters>\n         &lt;parameter name=\"fullname\" xsi:type=\"text\"  visible=\"true\" sort_order=\"0\" >\n            &lt;label translate=\"true\">Full Name&lt;\/label>\n         &lt;\/parameter>\n         &lt;parameter name=\"age\" xsi:type=\"text\"  visible=\"true\" sort_order=\"10\" >\n            &lt;label translate=\"true\">Age&lt;\/label>\n         &lt;\/parameter>\n         &lt;parameter name=\"gender\" xsi:type=\"select\" source_model=\"Meetanshi\\CustomWidget\\Model\\Config\\Source\\Gender\" visible=\"true\" sort_order=\"10\" >\n            &lt;label translate=\"true\">Gender&lt;\/label>\n         &lt;\/parameter>\n      &lt;\/parameters>\n   &lt;\/widget>\n&lt;\/widgets><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The first code:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Declare our widget with the unique identification is&nbsp;<strong><em>meetanshi_customwidget&nbsp;<\/em><\/strong>and the&nbsp;<strong>class<\/strong>&nbsp;attribute is used to map to widget file&nbsp;<strong><em>app\/code\/Meetanshi\/CustomCode\/Block\/Widget\/ContactInformations.php<\/em><\/strong><\/li>\n\n\n\n<li>The field&nbsp;<strong>description<\/strong>&nbsp;will show description, introduce about module when created.<\/li>\n\n\n\n<li>We need to declare all the option of module inside the field tag<strong>\u201cparameters\u201d<\/strong><\/li>\n\n\n\n<li>And the&nbsp;<strong><em>source_model<\/em><\/strong>&nbsp;attribute maps to the model file&nbsp;<strong><em>app\/code\/Meetanshi\/CustomWidget\/Model\/Config\/Source\/Gender.php<\/em><\/strong>, where we\u2019ll get our options for the drop-down.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">To create the model file:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>app\/code\/Meetanshi\/CustomWidget\/Model\/Config\/Source\/Gender.php<\/strong><\/em><\/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 Meetanshi\\CustomWidget\\Model\\Config\\Source;\n\nclass Gender implements \\Magento\\Framework\\Option\\ArrayInterface\n{\n    public function toOptionArray()\n    {\n        return [\n            ['value' => 'mal', 'label' => __('Male')],\n            ['value' => 'female', 'label' => __('Female')]];\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">To create the block file:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em><strong><em>Meetanshi\\CustomWidget\\Block\\Widget\\ContactInformations<\/em><\/strong><\/em><\/strong>&nbsp; is declared in the above code snippet. In this file, we assign custom template file inside&nbsp;<strong>_toHtml()&nbsp;<\/strong>method<\/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 Meetanshi\\CustomWidget\\Block\\Widget;\n\nclass ContactInformations extends \\Magento\\Framework\\View\\Element\\Template implements \\Magento\\Widget\\Block\\BlockInterface\n{\n    public function _toHtml()\n    {\n        $this->setTemplate('widget\/contact_informations.phtml');\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">To create the template file<\/h3>\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\n$fullname = $this->getData('fullname');\n$age = $this->getData('age');\n$gender = $this->getData('gender');\n?>\n&lt;ul>\n   &lt;?php if($fullname){ ?>\n   &lt;li>&lt;?php echo $fullname ?>&lt;\/li>\n   &lt;?php } ?>\n   &lt;?php if($age){ ?>\n   &lt;li>&lt;?php echo $age ?>&lt;\/li>\n   &lt;?php } ?>\n   &lt;?php if($gender){ ?>\n   &lt;li>\n      &lt;?php if($gender){\n         echo __('Male')\n      }else{\n         echo __('Female');\n      } ?>\n   &lt;\/li>\n   &lt;?php } ?>\n&lt;\/ul><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>Meetanshi\\CustomWidget\\view\\frontend\\widget\\contact_informations.phtml<\/em><\/strong>&nbsp;\u2013 which will show all widget data on site.<\/li>\n\n\n\n<li>You need to clear all the caches from the backend of Magento or delete folder<strong><em>&nbsp;var\/cache<\/em><\/strong>.<\/li>\n\n\n\n<li>Now, go to&nbsp;<strong>Administrator Page&nbsp;=&gt;&nbsp;Content&nbsp;=&gt;&nbsp;Pages<\/strong>&nbsp;and add a new Page using Add New page button, then click Widget icon in Content Tab and you need fill information for all fields.<\/li>\n\n\n\n<li>Save CMS page and go to the front end of page to<strong>&nbsp;check<\/strong>&nbsp;<strong>your widget<\/strong>.<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A Magento 2 store needs a lot of features to function smoothly and to attract more visitors. Having an appealing user experience is essential in E-commerce&nbsp;and that\u2019s where widgets come into the picture!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also <a href=\"https:\/\/meetanshi.com\/blog\/add-snowfall-effects-to-magento-2\/\">add snowfall effect<\/a> to get some seasonal touches to your website to enhance the user experience, also can create an awesome user interface with custom widgets and benefit your store  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basically, Magento widget is a Magento extension with definite functionality in your store. The default Magento supports the following types of widget: The Magento widgets&#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-90","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/90","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=90"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":15658,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions\/15658"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}