Use code MGT25 to get $25 Free Credit on Cloudways Magento Hosting

How to Create a Widget in Magento 2

A Widget is an essential element in Magento 2 and helps the visitors view and surf your website with ease. It’s an important part of a Magento 2 store, and this is why the Mageplaza team is going to address this topic today: How to Create a  Widget in Magento 2.

First, we’ll have a general look at the tutorial. To make the instructions are easy and understandable, let’s begin with a short explanation of what exactly a Widget is. If you already know what a Widget is and the functions it serves, you can skip the following heading and head to the next one.

Let’s begin!

Widget and Its Function in Magento 2

According to, a widget is an element of a graphical user interface (GUI) that displays information or provides a specific way for a user to interact with the operating system or an application. Similarly, Widgets in Magento 2 can be explained as small blocks which perform a definite function on your website.

There are many types of widgets and Magento 2 supports the following types by default: categories, tag cloud, navigation menu, calendar, search, and recent posts, etc. Correspondingly, store admins can use a widget to improve the user-experience as well as the functionality of the store.

Here are some achievable implementations of Magento widgets:

  • Dynamic product data,
  • Dynamic recently-viewed-item lists,
  • Promotional images placed on different Magento frontend locations,
  • Interactive navigation elements and action blocks,
  • Dynamic flash elements that can be easily configured and embedded in content pages,
  • And so on.

In fact, the method to create a widget in Magento 2 is almost the as same as building an optimized front-end extension with a simple module. Hence, there is no complexity since customization for widgets is much simpler than the helloworld module.

Let’s get creating.

Creating a Magento 2 Widget

To create a Magento 2 widget, please follow the 4 steps below:

Step 1: Initialize Widget

Create a file etc/widget.xml with the following content:

Step 2: Create a Widget Template File

Create a file with the path view/frontend/templates/widget/posts.phtml, and add this code to it:

Step 3: Create a Widget Block Class

Create block file: Block/Widget/Posts.php

Step 4: Post Widget

  • Before posting the widget, you should flush cache by opening SSH terminal and run this command in the root directory of Magento 2:

php bin/magento cache:flush

  • Then go to Admin panel > Content > Pages > Homepage > Edit
  • In the Content tab, click on Insert Widget icon.

insert widget icon

And here is the result, and you will see the Blog posts in the widget list.

blog post

And that’s about it. Now that you know how to create a widget in Magento 2, you can go and start empowering your website with added functionalities. If you have any questions, just use the comments section below and let’s get talking.

Author Bio:

Mageplaza is one of the top Magento Extension Providers and custom development services. With over four years of comprehensive experience in Magento Module Development, we are committed to providing the highest quality extensions and services for our e-business customers.