How to create custom wordpress widget

In this post, I am going to explain “How to create custom WordPress Widget”. WordPress has some default widgets that you can use easily by just dragging it to the widget area. But if you want something special OR in your way you need to write your own custom code. Most of people create widgets using theme functions.php file but there are some drawbacks like whenever your theme update then your code get lost and also your widget code stop workign automatically. So I will always suggest don’t make changes directly in theme file. If you require any functionality then create your seprate plugin for that functionality this is the best way of creating any functionality in wordpress without any risk.

Before we get started, it would be best if you create a specific plugin where you will be pasting the widget code from this post.

You can also paste the code in your theme’s functions.php file. However, it will only be available when that particular theme is active.



In this post, we will create a simple widget that just greets visitors. Take a look at this code and then paste it in your specific plugin to see it in action.

After adding the code you need to go to the widget page. You will notice the new Webprepration Widget in the list of available widgets. You need to drag and drop this widget to a sidebar.

There are most important few things that you might want to know like web_prep_widget_domain ? If your are creating custom widget for your theme then you can change web_pre_widget_domain to your theme’s text domain.

We hope this post helped you learn how to easily create a custom WordPress widget.

Thanks!!!

 

157 Comments

Add a Comment

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