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.
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.
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.
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.
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.
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
- 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