Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
Building Impressive Presentations with impress.js
Are you tired of creating boring Presentations?
Take a look at the the future of online presentations with impress.js
in my latest book

Writing WordPress Plugins using Post Filter Functions

on 2011/06/15 1:38 AM stored in: Wordpress and tagged: ,

Introduction to WordPress Filter API Functions

Wordpress provides a filter api which can be used to filter and modify the content before saving to database or displaying on browser. Basically these functions are used with wordpress plugins to dynamically prepare the output of your blog posts. In this tutorial I’m going to use the add_filter function to show you how to create simple plugins using wordpress. add_filter function will execute a custom php function on the specified component. This component can be your post title, post content,post tags ,etc….

	add_filter( $component, $function);

add_filter function has 2 required attributes and 2 optional attributes. Since this post is meant to create a very basic plugin I’m only using the 2 required attributes. First parameter is the wordpress component you want to filter. This can be your post content,title..etc. For each of these types wordpress has a predefined tag. For example if you want to filter the post content, the_content method should be used for the component part. Next parameter is the php function which is used to filter the content.

	add_filter(the_content, sample_function);

Writing a Simple WordPress Plugin

Now lets start writing our first wordpress plugin using post filters. WordPress uses a common format to identify details about a plugin. Every time you create a plugin you have to make sure to include this details according to the standard syntax in your main plugin page. In this case i’am using only a single php page for my plugin. Following lines contain the syntax to define plugin details.

/*
	Plugin Name: Simple Ad
	Plugin URI:  -
	Description: Adds a advertisement image on every blog post.
	Version: 1.0
	Author: nimeshrmr
	Author URI: www.innovativephp.com
*/
  • Plugin name should be a unique one for your wordpress installation. 2 plugins with same name will make the system conflict.
  • You can include a plugin uri if your plugin is hosted in a public place.
  • Description about the functionality of your plugin.
  • Version number of your plugin.
  • Name of the person who created the plugin.
  • Url of the website of the author.
    •  

      Now we can start writing the plugin code. First create a php function as shown below and call the function using the add_filter function.

      	function displayAd($content){
      		return $content.'<img src="ad.jpg" />';
      	}
      	add_filter('the_content', 'displayAd');
      

      Above code contains a simple plugin function which adds a advertisement image after every post. $content variable passed to the displayAd method contains the actual blog post text. displayAd function is added as a filter to the blog post content using the wordpress the_content method. You can make the image appear first by concatenating content to image instead of image to content.

       

      This example shows you how to make a simple filters to your content. You can create plugins like post rating,post commenting,post views using the above method. Also you can use filters like the_title,the_tags instead of the_content method, according to your requirement.

Installing and Activating your WordPress Plugin

  • Create a folder with the plugin name and include all the code scripts inside the folder.
  • Copy the folder to plugins directory inside the wp-content folder in your wordpress installation.
  • Log in as admin and click menu item called plugins in the left sidebar.
  • Plugin will be listed with your provided details and click activate to activate the plugin.
  • Now every time a blog post is loaded your plugin function will be executed and displayed in the browser.
You may have browsed many websites and spent hours to find the informaion provided in this tutorial. So take 1 minute to share this post to help others find it quickly. Thank You for visiting InnovativePHP.

 

Popular Posts
Recent Posts

 

Comments are closed.

Follow Us On Facebook

Page optimized by WP Minify WordPress Plugin