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
We created a file called simple_replacer.php and put it into wp-content/plugins/simple_replacer/ folder.
<?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/ */ ?> |
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.
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.
<?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.


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
Nice plugin,
btw how to create simple plugin that create post from rss?
thanks
Very nice tutorial. just a small correction. Change the line saying
“return $content” to “return $post”
thanks
very nice plugin
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.