WordPress Plugins: How to modify post content

Posted: July 21, 2010 in Freebies, Tips & Tricks
Tags: , , , , ,

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;
}

to be continue..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s