Skip to main content

How to Create a Custom WordPress Widget

Introduction to Widgets

To Create a Custom WordPress Widgets you need to understand What WordPress Widgets are. Let’s find out. WordPress widgets were originally created to design the WordPress theme so users can easily maintain it from back end. The most important part of widgets is its drag & drop functionality. It makes super easy to users to simply drag & drop elements into their site. There many WordPress themes and plugins which uses WordPress widgets to allow users to create their desired layout in such a easy way. WordPress also provides some plugins to add and improve widgets functionality.

Adding custom Widget sidebar to WordPress Theme

First we will register the sidebar into WordPress Theme. To do so open function.php file of your active theme and find the following code:
register_sidebar
On that location you will find your all registered sidebars of your theme. It will look like following screenshot which is taken from twentytwelve theme of WordPress:

Now add following code just after the all register_sidebar function finish.

Then save the funciton.php file and go to wodpress admin >  Appearance > Widgets. You will see our newly created sidebar will display with the Title ‘My New sidebar’.

Now you can simple drag & drop any widgets to display in this sidebar.

Display Widget area into Theme

 To display Widget content you need to call function dynamic_sidebar with Widget Id and that will shows Widget at certain point. Use the following code to display our above Widget area content.
<?php
if(is_active_sidebar(‘sidebar-new’)){
                 dynamic_sidebar(‘sidebar-new’);
          }
?>
You can also style the widget area as you required.I hope this tutorial helps you learn how to create custom WordPress Widget, you can let me know what Widget you are creating, by commenting below.

Hardik Joshi

Hardik Joshi is Founder, Developer and Writer at eCodezone.com, He loves to write usable code for the Web and other digital sources.