Home » Freebies, Tips & Tricks

Wordpress Plugins: How to modify post content

3 October 2009 348 views 4 Comments

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:

  1. kPicasa Gallery compatibility with other plugins
  2. How to reset wordpress admin password
  3. Free Wordpress Theme: ilookgood 1.1 (UPDATED)
  4. Helping Hand
  5. Wanted

4 Comments »

  • Owaeis said:

    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! ;)

  • Owaeis said:

    and your explanation raaawwkkss!!! I think I’d keep on asking things from you so that you can get ideas for great posts ;)

  • Owaeis said:

    here I’ve a question. What if I want to call a function before or after the content?

  • Salman (author) said:

    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:

    add_action( “loop_start”, “function_name”,priority );

    OR

    add_action( “loop_end”, “function_name”,priority );

    priority is 1-10, 1 meaning earliest as possible and 10 means it could be delayed.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.