Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Simple WordPress Widget Template

Posted on May 17, 2011 by Pippin in Intermediate, Quick Tips, Tutorials, Widget Tutorials 12 Comments
Home» Tutorials » Intermediate » Simple WordPress Widget Template
Tweet
Love It - 4
This entry is part 2 of 5 in the Working with Widgets Series
← How to Create Advanced WordPress Widgets – @wprootsSimple Recent Posts Widget →
  • How to Create Advanced WordPress Widgets – @wproots
  • Simple WordPress Widget Template
  • Simple Recent Posts Widget
  • List Categories Widget Plugin and Tutorial
  • Adding the Farbtastic Color Picker to your WordPress Widgets

Widgets are utilized by tons of WordPress themes and plugins as a way to display customized content in various locations, and they’re relatively easy to create, as long as you have the appropriate resources. The code below will give you everything you need to make extensive and advanced widgets that can do anything you want.

I am not going to explain what everything does, but it is not difficult to figure out. You can copy the code below into your functions.php or plugin file; it will register a simple widget that accepts a “title” and “message” options.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
 * Example Widget Class
 */
class example_widget extends WP_Widget {
 
 
    /** constructor -- name this the same as the class above */
    function example_widget() {
        parent::WP_Widget(false, $name = 'Example Text Widget');	
    }
 
    /** @see WP_Widget::widget -- do not rename this */
    function widget($args, $instance) {	
        extract( $args );
        $title 		= apply_filters('widget_title', $instance['title']);
        $message 	= $instance['message'];
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>
							<ul>
								<li><?php echo $message; ?></li>
							</ul>
              <?php echo $after_widget; ?>
        <?php
    }
 
    /** @see WP_Widget::update -- do not rename this */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['message'] = strip_tags($new_instance['message']);
        return $instance;
    }
 
    /** @see WP_Widget::form -- do not rename this */
    function form($instance) {	
 
        $title 		= esc_attr($instance['title']);
        $message	= esc_attr($instance['message']);
        ?>
         <p>
          <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 $title; ?>" />
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Simple Message'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />
        </p>
        <?php 
    }
 
 
} // end class example_widget
add_action('widgets_init', create_function('', 'return register_widget("example_widget");'));
?>

To expand on this simple widget, just duplicate and rename the fields, then modify their field types as necessary.

I will explain this code in detail, and provide examples of more advanced widgets in upcoming posts.

Tweet Follow @pippinsplugins
widgets

12 comments on “Simple WordPress Widget Template”

  1. chip says:
    May 29, 2011 at 3:53 am

    Thanks SO MUCH for providing such excellent resources !!
    – you are my hero of the week :)

    Reply
    • pippin says:
      May 29, 2011 at 3:57 am

      :)

  2. Michael says:
    June 3, 2011 at 6:33 pm

    I’ll second the motion on the floor by Chip.

    Today is my lucky day, as I’ve discovered Pippin’s site, purchased an excellent Custom Content Types plugin, and now find even more to learn within his resources.

    The only thing that perplexes me is how I could have gone on so long without have come across your work/offerings/sharing Pippin.

    You’ve now a new evangelist mate. Thanks.

    Reply
    • pippin says:
      June 3, 2011 at 6:37 pm

      @Michael – thanks! I hope you continue to find useful resources here. Don’t forget to follow me @pippinsplugins and @pippinspages if you want to stay up to date

  3. Michael says:
    June 3, 2011 at 6:45 pm

    You’re welcome. Besides the RSS grab I just entered into NetNewsWire, I’ll add you to my follows as well [scurries off to twitdom..]Done[/end surrying]

    Reply
  4. Simple Recent Posts Widget | Pippin's Plugins says:
    August 6, 2011 at 5:04 pm

    [...] start, please refer back to the Simple WordPress Widget Template that I gave in the first part of this series. It will provide you with almost everything you need, [...]

    Reply
  5. List Categories Widget Plugin and Tutorial | Pippin's Plugins says:
    September 14, 2011 at 9:10 pm

    [...] In this tutorial I’m going to walk you through how to create a simple widget to list categories, and custom taxonomies, in your sidebar. The final result will be a complete plugin that you can drop into your wp-plugins folder and activate like any other plugin. The code for this widget will be based entirely upon the Basic Widget Template I gave you in part 1 of this series. [...]

    Reply
  6. Rian says:
    October 7, 2011 at 3:16 pm

    What is the global posttypes line (line 31)? It doesn’t appear to be used, nor is it a WP global variable (like wpdb) that I can see…?

    Reply
    • Pippin says:
      October 7, 2011 at 3:25 pm

      Ah! Good catch. That is left over from one of my widget plugins that I copied this code from. I’ll remove it now.

  7. littleguy says:
    August 28, 2012 at 3:34 pm

    Nowadays WordPress requires a comment block at the top of your plugin to work. Put this at the top of the code and it will work as of WordPress 3.4.1:

    /**
    * @package Example
    * @version 1.0
    */
    /*
    Plugin Name: Example widget
    Plugin URI:
    Description: Example widget
    Author: littleguy
    Version: 1.0
    Author URI: http://example.com
    */

    Reply
    • Pippin says:
      August 28, 2012 at 4:03 pm

      Yes, that is very true, but this code was not meant to be a plugin :) Though of course you are more than welcome to place it in a plugin.

  8. Debbie Campbell says:
    November 2, 2012 at 6:06 pm

    This is great, thank you!

    Reply

Leave a Reply Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Posts By Taxonomy Widget Plugin – Free
  • Better Recent Comments Widget Pro
  • Adding the Farbtastic Color Picker to your WordPress Widgets
  • Advanced Blog Authors Widget Plugin
  • Custom Post Type Calendar Widget

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @gmwdesign post a ticket in the forums and I&#039;ll help as soon as able
    May 21, 2013
  • RT @WordCampSF: Reminder that WordCamp San Francisco tickets go on sale this Thurs at 10am PT. For ticket details: http://t.co/psoTDMrFpn #…
    May 21, 2013
  • RT @wpthemetut: Logging wp_mail with WP_Logging: http://t.co/GFLSIkfh4y via @YouTube
    May 21, 2013

Topics

get_user_meta Tom McFarlin register_setting meta box attachments add_shortcode hook wp_enqueue_script campaign monitor featured contextual help shortcodes add_options_page forms login short codes do_action Related posts mail chimp plugin authors attachment image comments post types apply_filters bbpress recent posts short code taxonomies custom post type Ajax images gallery Stripe jquery taxonomy widgets users add_filter add_action easy content types widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins