How to create custom widget in WordPress

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.

 

By default WordPress comes with standard widgets such as Search, Calendar, Recent Posts, Recent Comments etc… All the WordPress widget appear in the user’s Administration Screen at Appearance > Widgets. In WordPress version 4.9.6, there are altogether 17 widgets. They are: Archives, Audio, Calendar, Categories, Custom HTML, Gallery, Image, Meta, Navigation Menus, Pages, Recent Comments, Recent Posts, RSS, Search, Tag Cloud, Text and Video.

 

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:

 

Note: Before saving or updating unsafe data, unsafe data must be properly sanitized i.e. data must be validated. And while displaying data, proper escaping must be done.

 

After creating the custom widget, it should be registered. Otherwise it won’t appear in your Administration Screen at Appearance > Widgets. To register the custom widget, following code is needed.

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.

 

Themebeez

We are leading WordPress guru from the top of the world Nepal. We specialize on WordPress themes, plugins & custom WordPress websites.

5 thoughts on “How to create custom widget in WordPress

  1. 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!

  2. I love your blog.. very nice colors & theme.
    Did you design this website yourself or did you hire someone to do it for you?

    Plz reply as I’m looking to create my own blog and would like to find out where u got this from.
    thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *