How to Create a Custom Widget in Magento 2

Basically, Magento widget is a Magento extension with definite functionality in your store.

The default Magento supports the following types of widget:

  • categories
  • tag cloud
  • navigation menu
  • calendar
  • search
  • recent posts, etc.

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 custom widget in Magento 2 is like developing an independent extension. Customize a widget to extend the functionality of the core widgets or create your own custom widget in Magento 2.

Widgets are useful in the administration to add interactive interfaces and features in the front-end of Magento 2 store. Learn how to create a custom widget in Magento 2 by following the steps assembled here.

Code to Create Custom Widget in Magento 2:

File setup/Our module

Initially, we need to create the module setup file.

Create file app/code/Meetanshi/CustomWidget/etc/module.xml. Paste the following code snippet in that file.

Widget Declaration File

Create the widget file app/code/Meetanshi/CustomWidget/etc/widget.xml with content.

The first code:

  • Declare our widget with the unique identification is meetanshi_customwidget and the class attribute is used to map to widget file app/code/Meetanshi/CustomCode/Block/Widget/ContactInformations.php
  • The field description will show description, introduce about module when created.
  • We need to declare all the option of module inside the field tag“parameters”
  • And the source_model attribute maps to the model file app/code/Meetanshi/CustomWidget/Model/Config/Source/Gender.php, where we’ll get our options for the drop-down.

To create the model file:


To create the block file:

Meetanshi\CustomWidget\Block\Widget\ContactInformations  is declared in the above code snippet. In this file, we assign custom template file inside _toHtml() method

To create the template file:

  • Meetanshi\CustomWidget\view\frontend\widget\contact_informations.phtml – which will show all widget data on site.
  • You need to clear all the caches from the backend of Magento or delete folder var/cache.
  • Now, go to Administrator Page => Content => Pages 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.
  • Save CMS page and go to the front end of page to check your widget.

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 and that’s where widgets come into the picture!

Now you can create an awesome user interface with custom widgets and benefit your store 🙂

Feel free to put forward any doubts in the Comments section regarding the widgets and how to create them in Magento 2. I’d be happy to help!

Rate my article with 5 stars if found useful and don’t forget to review it.


(based on 4 Reviews)
How to Create a Custom Widget in Magento 2Author Magento Badge

Sanjay Jethva

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a Reply

Your email address will not be published.