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 Recent Posts Widget

Posted on July 1, 2011 by Pippin in Intermediate, Tutorials, Widget Tutorials 1 Comment
Home» Tutorials » Intermediate » Simple Recent Posts Widget
Tweet
Love It - 3
This entry is part 3 of 5 in the Working with Widgets Series
← Simple WordPress Widget TemplateList Categories Widget Plugin and Tutorial →
  • 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

This next part in our Working with Widgets Series is going to show you how to create a simple recent posts widget, much like my Better Recent Posts Widget plugin, though it will be even simpler. The goal of this tutorial will be to demonstrate a very useful technique that you can use in countless different kinds of widgets, and that is how to query and displays post lists.

To 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, and we will simply be expanding on that code.

So the first thing we will do is define our widget class:

/**
 * Recent Posts Widget Class
 */
class pippin_simple_recent_posts extends WP_Widget {
    // the rest of our code will go in here
}

Nothing too special here, so we’ll go ahead and move on to the next step: the constructor function.

/** constructor */
function pippin_simple_recent_posts() {
	parent::WP_Widget(false, $name = 'Simple Recent Posts');
}

This function essentially “builds” our widget and gives it a name. The name is what will show up on the widget panel in the WordPress admin.

Next is the function that defines the output of the widget, which, in this case, is a simple post query.

/** @see WP_Widget::widget */
function widget($args, $instance) {
	extract( $args );
	$title 		= apply_filters('widget_title', $instance['title']);
	$number 	= apply_filters('widget_title', $instance['number']);
	$offset 	= apply_filters('widget_title', $instance['offset']);
	?>
		  <!--?php echo $before_widget; ?-->
			  <!--?php if ( $title )
					echo $before_title . $title . $after_title; ?-->

At the top of this function we have setup variables that retrieve the option values set by the user on the widget panel (we’ll set these up in a moment). $title is the title of the widget that will be displayed above the post list; $number is the number of posts we wish to display; and $offset is the number of posts to skip.

After the option variables, we place the standard widget variables for $before_widget, $before_title, etc, so that our widget will fit into 99% of WordPress themes seamlessly. Now we get to the core part of this function: displaying the post list. The query, for which we use get_posts(), is no different than any other get_posts query. Pretty simple actually. The only part that may be a little different is the $args variable declaration. Because we want our users to be able to customize the posts that are displayed through the options we present them in the widget panel, we use the title, number, and offset variables we declared at the top to build our final $args variable. Then we pass the constructed variable to get_posts().

Our next function is the one that handles the updating / saving of the widget options. This one is very simple.

/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
	$instance = $old_instance;
	$instance['title'] = strip_tags($new_instance['title']);
	$instance['number'] = strip_tags($new_instance['number']);
	$instance['offset'] = strip_tags($new_instance['offset']);
	return $instance;
}

We just have to remember that our $instance variable names must match in every function we write for this widget.

Now there is just one more function, and it is the one that will display our widget options in the widgets panel.

/** @see WP_Widget::form */
function form($instance) {	
 
	$title = esc_attr($instance['title']);
	$number = esc_attr($instance['number']);
	$offset = esc_attr($instance['offset']);
	?&gt;<label for="<?php echo $this->get_field_id('title'); ?>"><!--?php _e('Title:'); ?--></label> 
<input id="<?php echo $this->get_field_id('title'); ?>" class="widefat" type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" /><label for="<?php echo $this->get_field_id('number'); ?>"><!--?php _e('Number to Show:'); ?--></label> 
<input id="<?php echo $this->get_field_id('number'); ?>" class="widefat" type="text" name="<?php echo $this->get_field_name('number'); ?>" value="<?php echo $number; ?>" /><label for="<?php echo $this->get_field_id('number'); ?>"><!--?php _e('Offset (the number of posts to skip):'); ?--></label> 
<input id="<?php echo $this->get_field_id('offset'); ?>" class="widefat" type="text" name="<?php echo $this->get_field_name('offset'); ?>" value="<?php echo $offset; ?>" />

There’s nothing too special going on here, just remember, again, that our instance variable names must match. Each option is nothing more than a simple HTML input field with names that match the instance variables.

And, finally, there is just one more step: hooking our widget class into WordPress. All the functions above this went inside our class, but this line must go outside of the class. I usually place it just after the class’ closing bracket.

// register Recent Posts widget
add_action('widgets_init', create_function('', 'return register_widget("pippin_simple_recent_posts");'));

That’s it! You will now have a fully functional recent posts widget that includes options to control the Title, the Number of posts displayed, and the post Offset.

We will be continuing this Working With Widgets Series with ever more complex tutorials, so stay tuned!

Tweet Follow @pippinsplugins
get_posts, recent posts, widget, widgets

One comment on “Simple Recent Posts Widget”

  1. paul says:
    December 15, 2012 at 4:17 am

    Hi,

    really enjoying this series, but reading the code examples above they seem to be brokem / in complete…..

    P.

    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

  • Featured Comments Plugin
  • User Submitted Image Gallery – Part 7
  • Creating a Simple Events System with Easy Content Types
  • Displaying Custom Post Types in Widgetized Sidebars
  • Advanced WordPress Widget Bundle

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 (1)

    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

  • @jasonbobich hmm, interestinf
    May 19, 2013
  • @jasonbobich I haven&#039;t recently
    May 19, 2013
  • @strickland lol I passed out for two hours, so who knows
    May 19, 2013

Topics

add_options_page attachments featured register_setting contextual help hook meta box Sugar Event Calendar add_shortcode Tom McFarlin get_user_meta the_content wp_enqueue_script Related posts plugin do_action mail chimp image login forms authors short codes attachment apply_filters post types bbpress recent posts comments taxonomies short code custom post type images gallery Ajax Stripe taxonomy jquery widgets users add_filter easy content types add_action 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