Written by on

Automatically Add Content to Your WordPress Posts and Pages

On many sites these days, you will find a standard text at the bottom of all WordPress posts. This is a great place to remind your readers about something you want them to do, a special promotion you are running, etc.

For example, on all the iThemes posts, we have an email subscription box. There are several ways to add content to your post, without having to do so manually. Here are three different ways you can do it. I would love to hear your preferred method, and why.

We will have a look at:

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. For instance, if you are creating a site for less technical users, who need to be able to change 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 the single post page, single.php. If you’re a developer, this isn’t much of a downside. If you are more of a beginner, this isn’t hard. You just need to be careful to follow the instructions exactly.

Step 1: Register the sidebar, the widget area by adding the following code to your functions.php:

        'name' => 'Bottom of Post',
        'description' => 'My new widget area to appear at the bottom of a post',
        'before_widget' => '<div id="bottom_post">',
        'after_widget' => '</div>',
        'before_title' => '<h4 class="widget-title">',
        'after_title' => '</h4>'

This creates a new 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



Step 2: 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 the_content('Read More'); ?>

<?php   if ( function_exists(dynamic_sidebar('Bottom of Post')) ) :
            dynamic_sidebar('Bottom of Post'); 

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. (A shortcode to insert a subscription box might look like this: [subscribe].)

This method requires you to add code to your functions.php file. Please note, when you overwrite your functions.php file, such as when you upgrade your theme, your modifications will be lost unless you take steps to preserve them. 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.

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://ithemes.com/feed/">Stay up to date, and subscribe to our RSS feed!</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:

add_filter('widget_text', 'do_shortcode');

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. (But if the thought of changing the core WordPress functionality makes you nervous, you may want to try one of the first two methods.)

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.

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://ithemes.com/feed/">Stay up to date, and subscribe to our RSS feed!</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.

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.

Which of these three solutions do you like best? Or do you have another way you prefer? Leave a comment below and let us know.


  1. Thanks for these great strategies. What would be the filter code if we wanted the content after the single post to be different for each category.

    ie. Category 1 – show subscribe box, Category 2 – show banner for a trial membership, Category 3 – show free report link….

    • Hi,

      you could retrieve the category from the post, and pass that category as a parameter in the shortcode string (e.g. [subscribe category=”category 1″], and act upon that parameter in the shortcode function in functions.php to display the desired message.

      A useful article with samples on shortcodes here.

      There’s a bit more coding required, so in this case, I would choose the option to use widgets to add content, and use the Widget Logic plugin to control which content shows up where based on the category. That way, there is no additional coding required, and you have a flexible dynamic solution.

  2. Perhaps I’m being dumb… but how do you edit filters? Are they files or can they be accessed by the Editor?

    I’m new to WordPress…. sorry.

  3. here is another solution – inserting the following codes into functions.php:

    function add_before_content($content) {
    if ( ‘page’ == $post->post_type ) return $content .’Default page content.’;
    if ( ‘post’ == $post->post_type ) return $content .’Default post content.’;
    add_filter(‘the_content’, add_before_content);


    function add_before_content($content) {
    return ‘Default Message’.$content;
    add_filter(‘the_content’, add_before_content);

  4. All My post usually uses various plugin based data manipulations (Like visual layout, grouping, plugin css, etc)
    And now I want to render a post content in my admin page.
    On using following code:

    $post = get_post(123);
    $content = apply_filters(‘the_content’, $post->post_content);
    echo $content;

    It just displaying a formated text content and I can’t see my plugin based alignments, layouts which I usually can see it on normal post.

    So How to get post content for admin panel all formatted along with all plugin based manipulation done?

  5. thanks a lot for this trick

    and what if i want to add a different content to different posts

    i mean it acts like a commenting system

    but comments will be added to the bottom of the post content , so it will look like a part of the content

    and every editor of my site has this widget and can put their own content

    thanks in advance :)



Sale ends Nov. 30! Save 50% OFF our entire library of WordPress tools Get the coupon