Post Pic

WordPress tip: Insert custom content after each post

For some reason, you may want to be able to automatically display a custom text below each of your posts. You can hard-code it, but it will be way better to do so using a WordPress hook. Here’s how to do.

You just have to paste the following code into your functions.php and save the file. Once done, custom content will be inserted below each of your posts.

function add_post_content($content) {
	if(!is_feed() && !is_home()) {
		$content .= '<p>This article is copyright &copy; '.date('Y').'&nbsp;'.bloginfo('name').'</p>';
	}
	return $content;
}
add_filter('the_content', 'add_post_content');

Thanks to Jeff Starr for the great snippet!

By the way, if you need any kind of WordPress help I'm happy to inform you that I'm starting to work freelance. Don't hesitate to contact me to get a quote for your new project!

16 Responses

Apr 16 2010 14:43

usefull :) ) tanks !

Apr 16 2010 18:57

Thats a great tip :)

Apr 16 2010 19:04

Maybe I’m missing something, can’t you just code this in your theme’s single or post template? What ever you type after the post will appear on every single or page template?

Apr 17 2010 03:43

great tip. Thank you :D

Apr 17 2010 04:52

Does this also update the RSS feed with the additional content?

Consider this recipe used.
Thanks!

Apr 17 2010 15:13

I love your site. I would find it the example much clearer if you showed an example of this in action. Many thanks!

Apr 17 2010 20:46

This is useful for applying reoccurring content on all your posts like : copy write information, contact info or a signature.

Cool! Thanks

Apr 18 2010 03:49

I’m with Neal unless I’m missing the point.

Apr 18 2010 19:54

Cool. This would suit a significant amount of users, but what about the ones that want more flexibility? The idea I’m suggesting involves the use of custom fields on ‘single’ post articles.

Consider something like this one:

function add_post_content($content) {
if(is_single()) {

$custom_content = get_post_meta($post->ID, ‘Custom Content’);

if(is_empty($custom_content)) {

$content .= ‘This article is copyright © ‘.date(‘Y’).’ ’.bloginfo(‘name’).”;

}else{

$content .= $custom_content;
}

}
return $content;
}
add_filter(‘the_content’, ‘add_post_content’);

You will have to set a custom field key of ‘Custom Content’[without the single quotes].

I aint tested this yet but you can hopefully see where I’m going with this. There are many possibilities with this one, including changing the code so that you can add a custom message plus the copyright message. To do this simply add:

$content .= ‘This article is copyright © ‘.date(‘Y’).’ ’.bloginfo(‘name’).”;

Just below the:

$content .= $custom_content;

So the copyright will be there either way and you can tweak the code and add an activating/deactivation option for the copyright message for certain posts you may not want it to appear in, as you create another custom field giving this option.

You can check the link below on understanding custom fields for those not familiar:
codex.wordpress.org/Function_Reference/get_post_meta

All the best :)

Apr 19 2010 13:56

Amazing little trick that saves a lot!
Already adopted on my blog, thank you =)

Apr 21 2010 06:37

Nice snippets! Tested and works.

Apr 29 2010 15:00

This is awesome! Thanks for the code. I’ve been looking for smth extraordinary to make on my blog and that’s it! :)

May 02 2010 11:23

Really very useful tips.Thanks for sharing.

Jul 07 2010 10:03

Hey.. It was really great tip.. I’m going to try this.. Many thanx !

Jul 11 2010 08:27

Thanks for the great tip. I am using this together with the Supple Forms plugin, the author have not yet enable this feature and this is just what I need.
Thanks again.

Aug 02 2010 20:35

@Neal and 401k

Yes you could code it into a theme if you were just adding copyright data or some random text and had a custom theme.

However I am creating a plugin which will be adding data to the end of the post. This post hit the spot for my problem.

Thanks

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required