How to Create a Custom WordPress Widget

Widget is one of the most useful features provided by WordPress. Most WordPress developers even don’t use this feature.  Even only a few developers know that there is an option to Create  a Custom WordPress Widget

If you are one of those few who know that we can create a Custom WordPress Widget, this post is for you. In this, I will walk you through the complete process of how to create a custom WordPress Widget.

Here I will use an example with the following scenario, which was the client’s actual requirement.

The requirement was to create a custom category (Taxonomy) for the woocommerce products with the name “Items”.

All the terms added to Items taxonomy needs to be added to different product page sidebar like shop page, single page etc. First thing came to my mind was to create a shrtcode and then add to the sidebar of different pages but then I realize that instead I should create a custom WordPress widget which I can use at different locations by just drag and drop and even if client wants to remove that any time then he/she can remove.

Following are the steps I performed to create a custom WordPress widget that can add a custom taxonomy with the name “items”.

 

Step 1 – Create a custom taxonomy with the name “items”

Add the following code to your active theme’s functions.php or inside the plugin main file.

Step 2 – Create a custom WordPress widget

Now add the following code to your active theme’s functions.php or inside the plugin main file. This will create a custom widget with the name “DCS Widget”. You can change this and use any name for your widget.

 

Optional (Bonus code) – Advanced option to exclude some terms by id

Add the following code instead of step 2 in the functins.php