How to create custom widget in WordPress

Looking to create a create custom widget in WordPress ? Let’s start! As you all may know,widgets in WordPress are the components by the use of which one can add contents and features in the site. Widgets are placed inside defined widget areas such as sidebar, footer, header etc.

For example, you may find advertisement on the header of websites of news portal, online magazine and some blogging websites. The advertisement is actually a widget and the part of header where the advertisement is displayed is actually a widget area.

If you are using a WordPress theme for your site, then you might find other widgets except the default ones. You’ll also find different widget areas where you can place widgets as your wish.

Today however, we are discussing about creation of custom widget. So let’s hop into creating a widget.

The first thing to know is that you can write you widget creation code in functions.php file or some other file which you can later include in functions.php file.

To create a widget, we need to create a class which extends the core class of WordPress called WP_Widget. The class contains four functions,

__construct(), widget(), form() and update().

In function __construct(), we define the base ID, name, description etc… of the widget. The base ID uniquely identifies the widget. The name sets, the name of the widget and description sets the short description of widget describing the purpose of the widget.

In function widget(), we write the markup for displaying widget contents.

Then In function form(), we create form using which contents are created.

In function update(), we save or update the contents created using form.

Now let’s create a text widget similar to the default Text widget in WordPress. The PHP code need for the Text widget is as below:

In this way, you can create custom widget in WordPress. If you are looking for best free wordpress themes of 2018 that already have great widgets & features then make sure you check this article for best free WordPress themes of 2018.

If you liked this article, then please share it.

And if you have any questions, then comment below. You can follow us on Facebook for more updates.

6 thoughts on “How to create custom widget in WordPress”

Thanks on your marvelous posting! I really enjoyed reading it, you’re a great author.I will be sure to bookmark your blog and will come back at some point. I want to encourage you continue your great writing, have a nice evening!