Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Create a Custom WordPress Widget

Last updated on by
Follow WPBeginner on YouTube
How to Create a Custom WordPress Widget

WordPress widgets make it super easy for users to simply drag and drop elements into their site. There are many WordPress themes and plugins that use widgets to allow users to create their own layouts. There are even plugins to improve widget management. In this article, we will show you how to create your own custom WordPress widget.

What is a WordPress Widget?

WordPress widgets were originally created to provide a simple and easy-to-use way of giving design and structure control of the WordPress theme to the user. The great thing about widgets is that you can drag and drop them into your sidebars or any widget ready areas of your website. This allows great flexibility to plugin and theme developers. They can add functionality into their products and let users decide when and where to use that functionality without messing with code. Similarly, as a user you can also create your own widgets in a site-specific plugin, so that you can drag and drop them into any theme you are using.

Creating a Custom WordPress Widget

Creating a Widget in WordPress

Before we get started, it would be best if you create a site-specific plugin where you will be pasting widget code. You can also paste it in your theme’s functions.php file but a site-specific plugin is better.

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

// Creating the widget 
class wpb_widget extends WP_Widget {

function __construct() {
// Base ID of your widget

// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'), 

// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), ) 

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
// Widget Backend 
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
else {
$title = __( 'New title', 'wpb_widget_domain' );
// Widget admin form
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
	register_widget( 'wpb_widget' );
add_action( 'widgets_init', 'wpb_load_widget' );

Now go to Appearance » Widgets, drag and drop WPBeginner Widget in your sidebar to see this custom widget in action.

Simple wasn’t it? First we created a custom widget. Then we defined what that widget does and how to display the widget back-end. Then we defined how to handle changes made to widget. Lastly, we registered and loaded the widget.

Now there are a few things that you might want to ask. For example, what wpb_text_domain does? WordPress uses gettext to handle translation and localization. This wpb_text_domain and __e tells gettext to make a string available for translation. See how you can find translation ready WordPress themes.

We hope this tutorial helped you learn how to create a custom WordPress widget. Let us know what widgets you are creating, by leaving a comment below.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. tkjeldsen says:

    hi is it posible to add HTML5 API in to a widget

  2. Akhil says:

    Hi I want to create a widget for home page only. It is not in side bar, its will be static home page and 3 boxes under the banner, I need to display widget in the boxes with learn more link. How I can do it?

  3. Dave Levy says:

    Works for me too, if one wants to use the include statement in the plugin file, then the code above needs to be surrounded by

  4. shree says:

    hey i want to add widgets to the center page??im confuse

  5. JKeefer says:

    Worked great, thank you!

  6. Chris says:

    I’m sure there’s a way to do this but I’m not entirely clear on how to go about it. I’ve done this tutorial and made a widget. Everything works fine. I’ve also extended this widget in that now it has image upload support through WordPress’ media upload functionality. And a way to add hyperlinks around uploaded images. What I thought would be nice is to implement jQuery’s drag and drop functionality so the display of the available fields in this widget could be controlled by the user. I’ve seen other plugins with this kind of functionality, but like I said I have no idea how to implement this. I know the jQuery UI library is at work here if that makes it easier to understand what I’m trying to do. Thanks much for the tutorial.

  7. WPBeginner Staff says:

    Yes Yatko it makes sense, however we are trying to just get our users started. We will try to cover more advanced topics in some future article.

  8. yatko says:

    Thank you for the great explanation it really helps in creating a widget in a very easy and proper way. I have added a few more details (will share the code) such as register/enqueue styles and scripts or jQuery, that may help beginners. One thing would be very helpful if you would include in the article and sample code, one or two extra options and their admin-form-values. That would help most people with adding their desired widget options and settings. Does this make sense? Thanks again!

  9. M J says:

    I have created my widget and it’s saving all the info I put in, the issue I have is that I have a title for the menu I select but I can’t figure out how to display that title on the site side. It’s a secondary title that is displayed above the menu not the widget title

  10. Folthan says:

    Ahh thanks for this tutorial, together with the site specific plugin tutorial it helped me make a simple widget that echo’s form information to the homepage. Now I am off editing it all in css :).

  11. Jeff says:

    HI, thank u so much. Really easy explained. Im a WP and PHP beginner. Can u tell me what i need to do to add a Textbox after the Title ??



  12. Jessi Lee says:

    This worked great for me! Thank you SO much for sharing :)
    I was trying to add Doubleclick For Publisher inside a WordPress widget and this method worked perfectly. I just echoed the javascript and div tags where your ‘Hello World’ was and viola!


  13. Sharul Hafiz says:

    I’ve got this error in debug mode

    Notice: Undefined index: before_title in …

    How to fix this?

  14. Hunk says:

    I did as you said and succeed, but it is just a text widget, useless, how can I make a complicated widget with your method?

    • WPBeginner Support says:

      Great, the purpose of the tutorial is to teach the basics of how to create a custom widget. After that, you can try your hands on creating a jQuery Tabber Widget. Also, another way to learn is to study the source of other plugins. WordPress plugin directory has thousands of plugins tagged widgets. You can download them and learn by trying to modify those plugins.

  15. Eric says:

    Thank you! Do you have a minute to help me troubleshoot something? We can continue via email if you’d like. The code works wonderfully, except that it’s displaying “000” before my output.

  16. Mike Oberdick says:

    Excellent tutorial…I like that you left it up to the user to tweak and learn from what you presented. I’ll be creating a widget that only displays posts from a certain category. Thanks for the great info!

  17. Luke Carrier says:

    The formatting of this code is extremely poor for a beginner’s tutorial. Could you at least correctly indent it so as to encourage best practice?

  18. Jassi Bacha says:

    Pasted this code and tweaked it around, had my own widget working in under 20 minutes. The commented documentation was perfect, you’re a lifesaver!

  19. Maddy says:

    Thankz for easy, simple code clearification…

  20. HiddenLife says:

    i paste your code into my functions.php file and it worked correctly . thanks a lot !

  21. XCOP91 says:

    Yup, this worked for me thank you very much. Great Stuff.

  22. Mas says:

    I just pasted the code into my theme , functions.php , but no widgets added !
    how should I correct it ?

  23. faizan ali says:

    Hey Guys

    I’m very new to wordpress, i want to make a custom Widget like above so please kindly let me know in which page should i paste the above widget code?


  24. Eoghan says:

    Ah yes finally! I can copy and paste and see it working!
    That’s the easiest way for me to break it down and understand how to use the component.

    Thank you very much!

  25. adolf witzeling says:

    Enjoyed reading this great tutorial.Very easy to follow, even for beginners like me.

  26. Ajay M says:

    Don’t you think the title is bit misleading as it does not cover the how to part of the title pretty well.

    • WPBeginner Support says:

      We tried to cover the how to part in the code by commenting. We wanted users to study the code. Let us know which part you would like us to explain further?

  27. Muhammad Khalil Janjua says:

    Thanks for this beautiful and simple I really like it.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.