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 lovers 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 »
  • WPBeginner Staff

    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.

  • Jeff

    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 ??



  • Jessi Lee

    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!


  • Sharul Hafiz

    I’ve got this error in debug mode

    Notice: Undefined index: before_title in …

    How to fix this?

  • Hunk

    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

      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.

  • Eric

    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.

  • Mike Oberdick

    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!

  • Luke Carrier

    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?

  • Jassi Bacha

    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!

  • Maddy

    Thankz for easy, simple code clearification…

  • HiddenLife

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

  • XCOP91

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

  • Mas

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

  • faizan ali

    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?


  • Eoghan

    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!

  • adolf witzeling

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

  • Ajay M

    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

      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?

  • Muhammad Khalil Janjua

    Thanks for this beautiful and simple I really like it.