Creating A Simple WordPress Plugin That Modifies Post Content

Posted in Wordpress Development on 29 June 2010 7 comments

In some occasion, you might need to add a same piece of codes to your blog posts over and over again, and when you need to make changes to them, you need to manually change all of them. This is very tedious. Not anymore, if you make a plugin that does the purpose.

In this guide, we are going to create a simple plugin that scan through the post content for “{donationcode}” and replace it with our paypal donation codes. Jump to complete code

1. Creating a new plugin php file

We created a file called simple_replacer.php and put it into wp-content/plugins/simple_replacer/ folder.

2. The plugin header

<?php
/*
Plugin Name: Simple Replacer
Plugin URI: link to your page to download the plugin or usage guide
Description: Replaces {donationcode} with paypal donation codes
Author: Zen
Version: 1.0
Author URI: http://zenverse.net/
*/
 
?>

3. Adding a function that does the replacing work

Type these codes into the file after the header part (after */)

function the_replacer($content) {
 
$post = str_replace( '{donationcode}' , 'YOUR_DONATION_HTML_CODES' , $content);
 
return $content;
}

NOTE:
Without returning the variable $content, you will ruin the whole post content (return empty content), so make sure you include the “return” line.

4. Adding filter, let wordpress know what we want to do

Add this, 1 line before the php closing tag (?>)

add_filter('the_content', 'the_replacer');
 
//if you want to run the function on excerpts too, uncomment the line below
//add_filter('the_excerpt', 'the_replacer');

NOTE:
The function name “the_replacer” must match the php function that you created in step 3.

The Complete Code

<?php
/*
Plugin Name: Simple Replacer
Plugin URI: link to your page to download the plugin or usage guide
Description: Replaces {donationcode} with paypal donation codes
Author: Zen
Version: 1.0
Author URI: http://zenverse.net/
*/
 
function the_replacer($content) {
 
$post = str_replace( '{donationcode}' , 'YOUR_DONATION_HTML_CODES' , $content);
 
return $content;
}
 
 
add_filter('the_content', 'the_replacer');
 
//if you want to run the function on excerpts too, uncomment the line below
//add_filter('the_excerpt', 'the_replacer');
 
?>

That’s it.

 

Posted by Zen   @   29 June 2010 7 comments
Tags :


or Subscribe to specific category only :




  - 7 Comments


shoppingcool says:

hi, i’m really lucky to read your post, this piece of info really courage me thanks ^o^

Very detailed information about Creating A Simple WordPress Plugin..Very nice sharing..Thanks

Buzzknow says:

Nice plugin,

btw how to create simple plugin that create post from rss?

thanks

shaukat ALi says:

Very nice tutorial. just a small correction. Change the line saying
“return $content” to “return $post”

thanks

Thank you. I want to make a plugin that displays an image at the beginning of each post. I think this code will help me to develop this idea.

Trackbacks

  1. 自制一个简单的Wordpress日志内容替换插件

Leave a Reply

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Previous Post
«
Next Post
»