Post Pic

How to: Automatically insert content after each post

Most blogs automatically displays some text after each post, for exemple to ask readers to subscribe to their rss feed. This text is very often hardcoded. Why not using function.php instead, and be able to keep the text when you’ll switch theme?

To achieve this recipe, simply paste the following code in your functions.php file. By using functions.php, you'll not have to re-insert this code if you switch themes.

function insertFootNote($content) {
        if(!is_feed() && !is_home()) {
                $content.= "<div class='subscribe'>";
                $content.= "<h4>Enjoyed this article?</h4>";
                $content.= "<p>Subscribe to our  <a href='http://feeds2.feedburner.com/WpRecipes'>RSS feed</a> and never miss a recipe!</p>";
                $content.= "</div>";
        }
        return $content;
}
add_filter ('the_content', 'insertFootNote');

Credits goes to Cédric Bousmane for that trick!

Related Posts

Related Posts

No related posts.

49 Responses

Apr 06 2009 12:12

Nice recipe.
I use this kind of automated content for social bookmarking buttons.
Thanks

Apr 06 2009 14:03

Nice recipe. I will consider using this somewhere!

Apr 06 2009 14:30

If it’s in your functions.php file, aren’t you going to have to update it when you change your theme?

If you really want it to stick around regardless of what theme you use, you’ll have to create a simple plugin for it, like this:

/*
Plugin Name: Insert text after posts
Description: Automatically insert this text after posts.
*/

function insertFootNote($content) {
if(!is_feed() && !is_home()) {
$content.= “”;
$content.= “Enjoyed this article?”;
$content.= “Subscribe to our RSS feed and never miss a recipe!”;
$content.= “”;
}
return $content;
}
add_filter (‘the_content’, ‘insertFootNote’);

Apr 07 2009 15:05

hotness.

Apr 08 2009 06:18

Hey, is there any kind of this recipe for blogspot blog?

Apr 08 2009 15:32

It’s amazing what could be done with functions.php. Simple hacks like this saves me from installing plugins.

Apr 10 2009 14:28

I’m a beginner :-(

What I have to chance to insert something before the content?

Apr 12 2009 06:44

Nice recipe! Thanks!

Jun 04 2009 11:31

Hello everybody

It’s possible to add this before the content ?

Sorry for my english

Jun 04 2009 13:44

@Kakushin: Create a variable and put your desired content. Name it $moreConte, for example.
Then:
if(!is_feed() && !is_home()) {
$content.= $moreContent.$content;
}
return $content;

Not tested, but it should work.

Jun 26 2009 07:55

my theme functions.php seemed to show an error after putting the code …as well the whole text came before the header started…did i place the code wrong

Aug 13 2009 11:07

I think there is a plugin that already does that.

It is called Add Post Footer and you find it here:
http://www.freetimefoto.com/add_post_footer_plugin_wordpress

Aug 14 2009 08:44

Hi, can I replace those words (Subscribe to our blablabla..) with adsense code?

Aug 15 2009 22:36

Very cool! Never thought of doing it this way but it makes sense. I probably would have cracked open the php files and hard coded this in but this is much easier.

Thanks!

Aug 20 2009 07:55

please let me copy your simple code, and thanks again.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required