Creating A Simple WordPress Plugin That Modifies Post Content

Posted in Wordpress Development on 29 June 2010 11 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 on 29 June 2010 • 21,579 visits 11 comments
Tags :


or Subscribe to specific category only :




  - 11 Comments


shoppingcool says:

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

Website design chennai says:

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

Ralph says:

Hey Zen, I’d like to use something like your plugin here to replace

[code]

...

[/code]

with

[code]

...

[/code]

in my posts.

I tried this in [code]the_replacer()[/code]:

[code]
$post = str_replace( '

' , '
' , $content);
$post = str_replace( '

' , '

' , $content);
[/code]

Nothing changed. The plugin is active, so that’s not the problem. :)

Care to help a noob?

Ralph says:

Oops, that didn’t print right. I’ll use HTML entities.

I’m trying to replace <blockquote>…</blockquote> with <blockquote><div>…</div></blockquote>.

shaukat ALi says:

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

thanks

Domain Chennai says:

very nice plugin

Invertir en bolsa says:

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.

fashion jewellery says:

It�s laborious to search out knowledgeable people on this topic, but you sound like you understand what you�re talking about! Thanks

Kanupriya says:

heyy…very nice!! thanks.

Trackbacks

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

Leave a Reply

You must be logged in to post a comment.

Previous Post
«
Next Post
»