On many sites these days, you will find a standard text at the bottom of all WordPress posts, for example the one you can find at the bottom of this post.

There are several ways to add content to your post, without having to do so manually. I will discuss three possible solutions, I would love to hear your preferred method, and why.

We will have a look at:

Using a filter to add content to your post content

By far the easiest and hasslefree way to add code to all your content, is to append it to your post content using a WordPress filter.

WordPress filters allow you to “hack” into the core WordPress functionality, and change, add or remove it. The filter to use to add your own content to your WordPress post content is the_content.

The following code will execute a function (here: insertSubscribeNewsLetter that in turn will append text, similar to what you can see at the bottom of this post, right after the content.

[php]add_filter (‘the_content’, ‘insertSubscribeNewsLetter’);
function insertSubscribeNewsLetter($content) {
if(is_single()) {
$content.= ‘<div style="border:1px dotted #000; text-align:center; padding:10px;">’;
$content.= ‘<h4>Enjoyed this article?</h4>’;
$content.= ‘<p><a href="http://feeds.feedburner.com/VanWeerd">Stay up to date, and subscribe to my RSS feeds!</a></p>’;
$content.= ‘</div>’;
return $content;

Note the conditional tag used if(is_single()), which will make sure that this filter is only applied to single post pages. Without this condition, the code would be appended to posts and pages.

The downside is that you can’t re-use the same code in other locations, as described in the next chapter.

Using a shortcode to add content to your post content

A shortcode is, as the name implies, a short code, that represents whatever code you assign to it. Using a shortcode is in particular useful if you want to add a specific text on various locations. The downside is that you have to add the shortcode “manually” to each post or page where you want it to appear, you do however have the ability to add it at the top of your post, the bottom, or perhaps just before or after the “read-more” break.

This method requires you to add code to your functions.php file. This means, that when you upgrade your theme, and replace your functions.php file, your modification will be lost. Using the Easy Theme and Plugin Update Plugin you can minimise the damage, since your existing theme will be backed up before being overwritten. Or, if you are using Child Themes, you should be ok if you apply this modification to your child themes’ functions.php file.

In order to use a shortcode, it needs to have a name (here: subscribe), and a function (here: subscribeNewsLetter) attached to it, that will execute when the shortcode is used in a WordPress post, page.

[php]add_shortcode(‘subscribe’, ‘subscribeNewsLetter’);

function subscribeNewsLetter() {
return ‘<div style="border:1px dotted #000; text-align:center; padding:10px;">
<h4>Enjoyed this article?</h4>
<p><a href="http://feeds.feedburner.com/VanWeerd">Stay up to date, and subscribe to my RSS feeds!</a></p>

Now, when you enter the shortcode [subscribe] anywhere in your post, or page content, the function subscribeNewsLetter will be executed, and the text will be inserted.

Note: if you wish to use shortcodes in widgets as well, add the following line to your functions.php to enable this:

[php]add_filter(‘widget_text’, ‘do_shortcode’);[/php]

Using a widget to add content to your post content

This method will describe how you can create your own widget area (still called “sidebars” which is not covering the actual functionality).

The advantages of using a widget is that you can change the content without having to go into the code. If for instance your site will be used by less technical users, that would also like to be able to change the content on a regular basis, e.g. for announcements, a widget provides an easy way to modify content.

The downside here is that you have to edit two of your theme files, functions.php and the file(s) where you would like the widget text show up, in this case single.php.

You first need to register the sidebar, the widget area. Add the following code to your functions.php:
[php]register_sidebar(array(‘name’=>’Bottom of Post’,’before_widget’ => ‘<div id="bottom_post">’,’after_widget’ => ‘</div>’,’before_title’ => ‘<h4>’,’after_title’ => ‘</h4>’,));

This will add a widget area named “Bottom of Post”, it will be wrapped in a div container “bottom_post” (which may come in handy if you want to add additional styling), and the title of the widget will be wrapped in <h4>tags</h4>

Next, you will have to make sure the widget contents will be displayed on your page. To add the widget to your single post page, you will have to modify the single.php file. In most themes, somewhere in the single.php you will find where the content is being written. You can add the following code after that, but you can also choose, as you can see on my post pages, to add the widget below the meta data, categories, and add it just before the comments start.

[php]<?php the_content(‘Read More’); ?>

<?php if ( function_exists(dynamic_sidebar(‘Bottom of Post’)) ) :
dynamic_sidebar(‘Bottom of Post’); endif; ?>

So there you have it, three ways to add content to your content. Of course this is not just limited to add a newsletter subscription form, there are many uses for this.

Let me know if you use another method, and what practical use you found implementing one, or all of these snippets.


Leave a Reply