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 : https://github.com/nadeeth/wp_latest_posts/tree/simple

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: http://plugin.example.com
Description: Show the latest posts bottom of each single post page
Version: beta 1.0
Author: Author L.
Author URI: http://example.com
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."'>".
        strip_tags($post->post_title).
        "</a> - ".strip_tags(substr($post->post_content, 0, 150)).
        "...</li>";
    }
    $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){

    if(is_single()){
        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
OR
  • Compress 'wp_latest_posts' folder as a zip file. (wp_latest_posts.zip)
  • Login as wp admin.
  • Go to "Plugins --> Add New"
  • Click Upload
  • Brows and select the file(wp_latest_posts.zip), 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.



[Refrences]

Comments

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. Create a…

How to create a simple Web Crawler

Web crawlers are used to extract information from web sites for many purposes.

The simple example given here accepts an URL and exposes some functions to query the content of the page.

To check out the source code of this example : https://github.com/nadeeth/crawler

If you are going to make any improvements to this code, I recommend you to follow TDD and use the unit test class in the code.

Step 1 : Create the class, init function and required attributes In this example, xpath is used for querying the given web page. There is an attribute to hold the page url, and another to hold the xpath object of the loaded page.

The init() function initializes the xpath object for the page URL assigned to url attribute.

class Crawler { public $url = false; protected $xpath = false; public function init() { $xmlDoc = new DOMDocument(); @$xmlDoc->loadHTML(file_get_contents($this->url)); $this->xpath = new DOMXPath(@$xmlDoc); } } In the next two ste…

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' || $comment['ap…