Wordpress Plugins: How to modify post content
Hi there,
A friend of mine asked me how to add some text before and after post content in Wordpress using Plugins.. it’s easy!
Let me tell you how:
function add_test_text($content) {
$content = “Before Content<hr>”.$content;
$content .= “<hr>After Content”;
return $content;
}add_filter( “the_content”, “add_test_text”,1 );
What we are doing here? let me explain line by line:
function add_test_text($content) {
We are just starting a function here with $content as the first (and only) variable.. Function name is “add_test_text”
$content = “Before Content<hr>”.$content;
What we are doing here is we are assigning “Before Content” as the value of the variable $content.
".$content; this just adds (suffixes) the original $content variable after the string.
$content .= “<hr>After Content”;
If you notice there is a “.” (dot) before “=” (equals to sign), this DOT tells PHP that we want to add something after the variable.. rest is just like above.
return $content;
}
Then we return the post content to wordpress and } closes the function.
add_filter( “the_content”, “add_test_text”,1 );
This is the magic part, what we are doing here is we are telling wordpress to add and apply our function “add_test_text” to post content.
And that is all here is the full code for your “ease”:
<?php
/**
* @package Test Post Content
* @author Salman
* @version 0.1
*/
/*
Plugin Name: Post Content
Plugin URI: http://blog.skdev.net
Author: Salman Mehmood
Description: Shows how to add content before/after post
Version: 0.1
Author URI: http://blog.skdev.net
*/function add_test_text($content) {
$content=”Before Content<hr>”.$content;
$content.=”<hr>After Content”;
return $content;
}add_filter( “the_content”, “add_test_text”,1 );
?>
Comments, feedback, critics appreciated
-Salman
Related posts:

Thanx a lot bro! I have been searching and trying it out all the day. Just got it to work and your post have added to my learning.
Love you Salman! You are so cute!
and your explanation raaawwkkss!!! I think I’d keep on asking things from you so that you can get ideas for great posts
here I’ve a question. What if I want to call a function before or after the content?
Hi Owaeis,
To call a function before or after the post content? You can’t you can just issue the function ON post content as I showed above. I used two lines for Above & Below content insertion, you can chose one and use it to append or perpend text/script/graphics to your posts.
If you meant after all posts or before all posts (the loop i.e.) you would create the function as above but use this command for adding the filter:
OR
priority is 1-10, 1 meaning earliest as possible and 10 means it could be delayed.
Leave your response!
Get Updates in Your Inbox
Recent Comments
Categories
Tags
Friends
Recent Posts
Most Commented
Most Viewed