Skip to main content

How to create a simple Wordpress plugin

Last year, I was working on some projects related to bloging platforms such as Wordpress, and I became really passionate about Wordpress during that time. Wordpress has become a very popular CMS, although originally it came as a blog publishing platform. One of the main reasons for this popularity may be it's powerful plugin architecture. So, I thought it's better to write a simple tutorial about how to create a simple plugin for Wordpress.

In this tutorial, I am giving the main priority for the simplicity since I need to show how easy to create a plugin for Wordpress. I highly recommend to improve the structure of code if you are going to use this for a complex project.

This simple plugin will show, the head lines of the latest posts in the bottom of each single post page, so that users will be able to navigate to the latest articles without moving back to the home page.

To check out the source code for this example :

Step 1. Create the main plugin file

Create a directory (ex: wp_latest_posts) and create a php file as the main plugin file (ex: wp_latest_posts.php) and add the following header information in the top.

Plugin Name: LatestPosts
Plugin URI:
Description: Show the latest posts bottom of each single post page
Version: beta 1.0
Author: Author L.
Author URI:
License: GPL2
This is the standard plugin information which lets Wordpress identify your plugin.

Step 2. The function to get latest posts

Add the function given below to the file created in the step 1 after the header information. On execution, it will return a brief list of last five posts ordered descending by post date so that the latest one to be in the top. I guess the code is self explanatory.

function get_latest_posts_list() {

    $args = array(
        'posts_per_page' => 5,
        'numberposts' => 5,
        'offset' => 0,
        'category' => '',
        'orderby' => 'post_date',
        'order' => 'DESC',
        'include' => '',
        'exclude' => '',
        'meta_key' => '',
        'meta_value' => '',
        'post_type' => 'post',
        'post_mime_type' => '',
        'post_parent' => '',
        'post_status' => 'publish',
        'suppress_filters' => true );

        $posts = get_posts( $args );

    if (empty($posts) || count($posts)<1) return;

    $html = "<ul>";
    foreach ($posts as $post) {

    $html .= "<li><a href='".$post->guid."'>".
        "</a> - ".strip_tags(substr($post->post_content, 0, 150)).
    $html .= "</ul>";

    $container = "<div><h2>Latest Posts... </h2>{$html}</div>";

    return $container;

Step 3. Create the filter function

Wordpress uses filter functions to pass data through. So, here we can use a filter function to add the list retuned by the function in step 2 to the bottom of each post. Add this function too to the main file after the function created in the step 2.

function show_latest_posts($content){

        return $content . get_latest_posts_list();
    } else{
        //if `the_content` belongs to a page no change to `the_content`
        return $content;

Next, this filter function should be added to the hook. Add the line given below to the main file after the filter function.

add_filter('the_content', 'show_latest_posts');

Now, our simple plugin is ready to install and test.  

Step 4: Installation

Please follow the instructions given below to install the plugin.

  • Upload the 'wp_latest_posts' folder (created in the step one which contains the plugin main file) to the `/wp-content/plugins/` directory
  • Activate the plugin through the 'Plugins' menu in WordPress
  • Compress 'wp_latest_posts' folder as a zip file. (
  • Login as wp admin.
  • Go to "Plugins --> Add New"
  • Click Upload
  • Brows and select the file(, click "Install Now" button.
  • Activate the plugin

Now open a post in your blog (you should have at least 2 posts in your blog) and check the bottom of the page between the post and comments areas.

Above code was tested in WordPress 3.3.1.



  1. Need more posts like this :-)

  2. Thanks for sharing deep knowledge

  3. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
    WordPress development company in Chennai

  4. If you are looking for a quick and easy way to add a donation button to your website, then Donorbox WordPress donation plugin is a great solution for you. This free WordPress plugin enables you to add a donation button to any page, post or sidebar on your website.


Post a Comment

Popular posts from this blog

How to create a new module for vtiger...

Recently, I had to create a new module for vtigerCRM for my client in current working place. I did search in many places including the official vtiger sites, but couldn’t find a better documentation for my purpose. The latest vtiger version at that time was 5.0.3. Because I had some experience doing lots of core modifications for this system, I did decide to read the source code and find how to add a new module. Finally, I could create a new module and started the project. So, I thought it will be a good thing to write some thing on my blog about this topic, so that others who want to do this thing can read. Given below is a brief description about how to create a new module for vtiger CRM 5.0.3. Source code of this example module is also available to Download . Step 01: Creating the module directory and minimum required files. Create a directory called “newModule” inside your vtiger modules directory, or any other name that you prefer. Now, module index file should be created. Creat

How to create a waveform animation with HTML and JavaScript

Recently, for a pet project, I wanted to create an animation of a waveform based on the varying decibel level of the microphone input. I was thinking about a really simple way to accomplish this with SVG and JavaScript. Given below is the first sample code I did on the Codepen. You can change this code to use with any framework of your choice. For this sample code, I am using a random number as the microphone input. You can replace it with any other time based input. Code Pen : For this example, you need an HTML code snippet like the one given below. It’s just an SVG with a Polyline element inside.  <div style="text-align:center">   <svg height="150" width="400" id='svg'>     <polyline id="polyline-id" fill="none" stroke="#005c66" stroke-width="1" />   </svg> </div> And then the plain Javascript code to animate the polyline. You can

De Morgan's Laws in Programming

Recently, while I was reviewing some codes, I saw there were some conditional statements that check for the same condition but written in different ways. Most of these statements were written with common sense without using any mathematical analysis, since those are too simple to go for a more formal approach. The two identical conditional statements that has been written in different ways are given below. 01) if ($comment['deleted'] == '1' || $comment['approved'] == '0') {                 unset($conversationsArray[$key]); } else {                ++$count; } 02) if ($comment['deleted'] == '0' && $comment['approved'] == '1') {                ++$count;              } else {              unset($conversationsArray[$key]); } Obviously, the above lines say that the inverse of the first condition is equals to the second condition and vice versa. That is...  ($comment['deleted'] == '1' |