WP: Add content to the bottom of every post

I wrote a new WordPress plugin last week, July of 2011. This plugin is a simple filter that adds some content to the bottom of each post. I thought for sure a plugin like this would already exist, and I was surprised to find a few very complex solutions with way more features than necessary.

This plugin has no other features and adds no administration options pages in your admin dashboard (because you can edit the settings via the Plugin editor). The message that is appended to each post is saved in a text file in the plugin directory. I chose a file on disk instead of a WordPress database option because I dislike plugin options pages for simple plugins.

How can I edit the message?
To edit the message that will be added to the bottom or footer of your posts, Go to Plugins > Editor and choose “Bottom of every post” with the top right selector. The plugin’s files will be loaded on the right. Choose the file `bottom_of_every_post.txt` and edit at will.
How can I remove the message from my home page posts?
You’ll have to edit the plugin. Find this line:

if( !is_page( ) && file_exists( $fileName )){

…and change it to this:

if( !is_page( ) && !is_home( ) && file_exists( $fileName )){

If I update the plugin, and you accept the update, you’ll have to make this change again.

Download bottom-of-every-post.zip

Installation instructions

  1. Modify `bottom-of-every-post.txt` to contain the content you would like at the bottom of every post
  2. Upload the `bottom-of-every-post` folder to the `/wp-content/plugins/` directory
  3. Activate the plugin through the ‘Plugins’ menu in WordPress

I am also going to use this code to teach a few people how to create their own WP plugins. Here is the full source code of my new plugin:


<?php
/*
Plugin Name: Bottom of every post
Plugin URI: http://www.tacticaltechnique.com/wordpress/bottom-of-every-post/
Description: Add some content to the bottom of each post.
Version: 1.0
Author: Corey Salzano
Author URI: http://profiles.wordpress.org/users/salzano/
License: GPL2
*/


/*
	avoid a name collision, make sure this function is not
	already defined */

if( !function_exists("bottom_of_every_post")){
	function bottom_of_every_post($content){

	/*	there is a text file in the same directory as this script */

		$fileName = dirname(__FILE__) ."/bottom_of_every_post.txt";

	/*	we want to change `the_content` of posts, not pages
		and the text file must exist for this to work */

		if( !is_page( ) && file_exists( $fileName )){

		/*	open the text file and read its contents */

			$theFile = fopen( $fileName, "r");
			$msg = fread( $theFile, filesize( $fileName ));
			fclose( $theFile );

		/*	append the text file contents to the end of `the_content` */
			return $content . stripslashes( $msg );
		} else{

		/*	if `the_content` belongs to a page or our file is missing
			the result of this filter is no change to `the_content` */

			return $content;
		}
	}

	/*	add our filter function to the hook */

	add_filter('the_content', 'bottom_of_every_post');
}

?>

50 comments

  1. Bryan:

    You CAN change the text from the WP admin panel. Under plugins, click Editor. Choose “Bottom of every post” in the upper right hand corner. Choose the file bottom_of_every_post.txt, and make any changes you like.

  2. I am using your ‘bottom-of-every-post’ plugin to add a google+ button to my blog articles at fraggit.info/fitblog. Works great, I thanks you much :)

  3. The plugin is great but I have an issue with it: it will append the text on the frontpage even when there is a MORE link in the post, so that the visitor must click it to read the whole post. In this case I don’t wanna the visitor to see the bottom of the post on each and every post, but only below those who showing their full content. Any trick to circumvent this problem?

  4. I was so excited about this plugin because I wanted to put a Call To Action on the bottom of every post. However, I was so disappointed to learn that I am expected to know how to put the code in myself. I appreciate the thought but for the most part, code is like a foreign language to me.

    I would much rather have a options via the admin dashboard where I just type and hyperlink to my hearts desire. After all, those why us code illiterates love wordpress!

  5. Corey, thanks so much for this plugin. Is there a simple way to augment the code so that the bottom-of-post content only loads within the posts themselves and not on the site homepage?

  6. A few people have asked about showing this text only on a single post view and not on the main page. Here’s how I accomplished that.

    First, put your bottom-of-post content inside a div with a named class, for instance:

    Your bottom content goes here

    Then in your stylesheet, set up two rules:

    div.EveryPostFooter
    { display: none; }

    body.single-post div.EveryPostFooter
    { display: block; }

    Now although the bottom text is still added to every post, it won’t be displayed at all except when in single-post view. (How it works: the first rule says not to display any div with class EveryPostFooter. The second rule reverses the first rule, but only in cases where the div is somewhere inside of a body element that has class single-post applied to it, which it does when you’re in single-post view.)

    You could modify this technique to do the opposite, if you want (show the text on the main page, but not on the single-post view).

  7. Whoops, my last post failed to show what I wanted, because the comment system stripped out the HTML tags. Rewording the first step above, what I meant was:

    First, put your bottom-of-post content inside a div with a named class, for instance:

    !div class=”EveryPostFooter”!
    Your bottom content goes here
    !/div!

    Just substitute less-than/greater-than characters (angle brackets) for the exclamation points, to turn them into div tags.

  8. When I modify the text to what I want, I can’t save the changes. It tells me “You need to make this file writable before you can save your changes.” How do I do that?

  9. Hi,I`m a bit confused here.So,I installed the plugin,activated it,went to plugins editor,then to bottom of every post.text,added the G+ code and updated the file ,but nothing shows.What am I doing wrong?
    Another thing please,can I use it to added hyper links ,like Facebook page like with the content?

  10. Love this plugin for my posts. What about pages? Do you have a code for bottom of every page? thank you!

  11. Great plugin! I’m using a Studio Press Child theme, and am not adept at hooks, so I was losing my mind trying to do what I wanted.

    You saved it!

  12. another thumbs up from me and one question … if there are a few posts I would like it to “display none” on, how do I do that?

  13. Nice plugin … just wanted to know if I can add Jetpack’s contact form code below each post to display contact form. I need to fetch the Author’s email and insert it in the code. Will this allow to execute PHP code ?

  14. Corey – thanks for this plugin!

    Trajan or Corey – I’m trying to have this display only on the single post view, not the home page.

    I turned the content on the .txt page into a div as suggested above. But am not sure where to but the two rules.

    I tried pasting them into the styles.css file (under =layout and .postfooter) but that didn’t do anything.

    I’m not a code monkey so it is probably my error. But I would love for this to work on single posts only if you have any assistance! Thank you.

  15. Is it possible to insert a picture in place of the text with this plugin. I really like the plugin but thought it would be a great spot for a gif of my signature after each post

  16. I have a much easier method of getting the plugin to ONLY display on the single post page… rather than the overly-complicated way posted above using CSS.

    Simply change

    if( !is_page( ) && file_exists( $fileName )){

    to

    if( is_single( ) && file_exists( $fileName )){

    That’s it.

  17. I have your Bottom of Every Post plugin installed. Now instead of adding related posts at the bottom of each post, it has this text with links: “Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design.” Is there any way to remove this AND how can I get my related posts back to the bottom?

  18. Hey Corey,

    Just wanted to say that I just installed your plugin with great success. It’s working beautifully and solved what I thought was going to be a couple of days of work of coding myself.

    Thanks!

  19. Nick: I would make it a separate plugin to keep this one simple. Right now, paid work is capturing my focus, and I already have a backlog of 2 finished free plugins that I have yet to publish.

  20. Corey, Thanks so much for the plugin, its a fantastic, simple function to do exactly what I needed! I’m clueless about code. What I need is for the custom text to NOT appear on Category Archive pages. I saw your response to Rebecca above, and tried replacing is_home with has_category but that did not make the code not appear on the Category Archive pages. Looked at the codex page also, but don’t see anything different. Any help?

  21. Corey, I’ve discovered by trial ‘n error, mostly error!, the answer to my question above (removing the text added to the bottom of each post on Category Archive pages), at least it works on the theme I’m using, which is to add:
    && !is_archive( )
    to the function code so that the result looks like this:
    if( !is_page( ) && !is_archive( ) && file_exists( $fileName )){

    Again, thanks for the great plugin!

  22. Thanks for your plugin, did the trick for me, without any extra useless feature.
    May be useful one day if I decide to try and create some plugin for my own website.

  23. I used this plugin for a customer today who wanted a dotted line in between posts. It works fine and its compatible with WordPress 3.5.1
    Thnx!

  24. Hey Corey,
    I love your plugin and have added my own Opt-in box to the bottom of each post, However, i keep getting this big gap in-between the end of my post and the plug-in content (like i have several blank lines in there). Do you know how I can get rid of this? It only happens when I activate this plugin.
    You can see it on one of my pages here: http://provennutritionforkids.com/nutritional-healing/

  25. Bonnie: There are two empty paragraph elements before the image. I used FireFox’s Inspector tool to hover over the area and find the empty elements.

  26. After searching through several plug-ins that claimed to allow me to create a post footer, only to find them way too confusing to set up, I finally found one that is super easy to use and actually works: this one! I don’t know how to rate plug-ins on WP (tell me how and I’ll do it) so I at least wanted to post a comment here to say THANK YOU for this easy to use plug-in

Leave a Reply

Your email address will not be published. Required fields are marked *

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