Introduction

One of the main reasons why we like WordPress so much is because there are so many plugins out there that can be used to make our websites better and easier to administrate. For example, our Maintenance Mode plugin that enables you to inform your users when your new website is going to be live while you are working on building it. However, it is always better to know how to make one yourself. Why? Well, because you can call it and design it however you want, and it will do whatever you want. Imagine, you can be a modern Dr. Frankenstein and create a little "monster plugin" of your own. How cool is that?

In this little article I will show you how to create a "Recent Posts" plugin, so you could get a general idea. It will be best understood if you are familiar with PHP programming language.



First thing's first

The first thing we need to do is to create a .php file (obviously) in which we'll write all the code that will make our little plugin work, let's call it latest-posts.php. WordPress stores it's plugins in the wp-content/plugins/ folder, so there's where we'll move our new .php file. After we made the main file and moved it to the right place, we must insert some information into the file's header. We do that so the WordPress installation could recognize our plugin:

 
<?php
/*
Plugin Name: Recent Posts Plugin [ThemeFuse]
Plugin URI: http://themefuse.com/blog
Description: Displays a list of your latest posted articles.
Version: 1.23
Author: Stefan Nistor
Author URI: https://twitter.com/themefuse
*/
?>

Now, we shall save our changes and check if WordPress found the plugin. Voila!




The Widget

Now that our plugin is recognized, we can go further on. The next thing we are going to do, is to create a widget for our plugin. Widgets are great because you can drag & drop them on the sidebar, or any other available widget area, from the administrator's site without a problem. The widget we are about to create will give us the possibility to name our list, and to choose the number of items displayed on the sidebar.


Widget's structure

 
class ThemeFuseWidget extends WP_Widget {
    function ThemeFuseWidget() {
        parent::WP_Widget(false, $name = 'ThemeFuse Recent Posts');	
    }

Title and number of posts - output the options given by us from the admin's area

 
function form($instance) {				
        $title = esc_attr($instance['title']);
        $dis_posts = esc_attr($instance['dis_posts']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <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; ?>" /></label></p>
     		<p><label for="<?php echo $this->get_field_id('dis_posts'); ?>"><?php _e('Number of Posts Displayed:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('dis_posts'); ?>" name="<?php echo $this->get_field_name('dis_posts'); ?>" type="text" value="<?php echo $dis_posts; ?>" /></label></p>
        <?php 
    }

The recent posts - output the content of the recent posts

 
function widget($args, $instance) {
    extract( $args );
    $title = apply_filters('widget_title', $instance['title']);
    $dis_posts = $instance['dis_posts'];
    ?>
      <?php echo $before_widget; ?>
         <?php if ( $title )
            echo $before_title . $title . $after_title; ?>

                 // we will insert the plugin functionality in here later

         <?php echo $after_widget; ?>
<?php
    }
}

Register the widget - after all the code above, paste the following line

 
<?php add_action('widgets_init', create_function('', 'return register_widget("ThemeFuseWidget");')); ?>

Now that we are done with the widget, let's check if the it works.




Plugin Functionality

So the widget seems to work fine, it is now time to create the plugin itself. In our case, it is actually the easier part. WordPress has a great API that will help us display the recent posts list with just a few lines of code.

 
<ul>
    <?php
        global $post;
        $args = array( 'numberposts' => $dis_posts);
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) : setup_postdata($post); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
</ul>

Now take the code above, and paste it instead of the "// we will insert the plugin functionality in here later" we wrote earlier.



The end

That's it. Not big deal. Of course this is just a brief tutorial and a simple plugin but I hope it was helpful and rather interesting. You can download the plugin I created for this tutorials to activate it and study it closer. Till next time.




Stefan Nistor,
Themefuse guest author

there are 12 comments added

  1. Ben 13th April 2011

    Great tutorial Stefan - is this something that will only work with a ThemeFuse theme, or can it work for any theme?

    • Stefan Nistor 13th April 2011

      Thank you, Ben! The plugin created for this tutorial and the methods used for its creation will work on any Wordpress installation and any theme with a widget area (sidebar).

  2. Alexandru Vornicescu 13th April 2011

    WordPress has already this plugin installed, why we need another one? would be better to have some additional features like thumbnail or filters

    • Stefan Nistor 13th April 2011

      Alexandru, the purpose wasn't to create a new plugin, the purpose was to write a brief tutorial about making it. I just took "Recent Posts" as an example so readers could get the general idea of plugin creation.

  3. wp 19th April 2011

    Wp Plugin...

  4. Wordpress Plugin 31st May 2011

    Good effort i like this plugin thanks for sharing.

  5. Josh Bedo 25th June 2011

    Awesome post please post more tutorials like this even if their simple they still help many people. I would like to see more plugin tutorials

  6. Bill 8th November 2011

    Awesome tutorial. I am still working on my WordPress site but would definitely like to incorporate your tutorial into it? Do have anything on creating a post author box plugin with maybe recent posts included in it? Or do you know where I could find a tutorial for that. thewpnovice

  7. Zarko 15th June 2012

    How would I offset the first posts so that the widget skips the posts that have excerpts on the home page?

  8. Eugene 10th December 2014

    Thank you, Stefan! Your tutorial helped me to learn exactly what I was looking for!

  9. Sandip 28th April 2016

    Thanks for sharing

  10. Siva 28th November 2018

    Awesome Post.....

Reset fields

back to top