The ideal way to modify the displayed content of all WordPress posts

What will be exemplified here is a way to add content to the footer of posts for viewing without changing the current content of the posts in the database. For this we will use the_content Filter hook. This hook allows you to alter the content for all posts and pages being displayed in the browser. By using this hook you can add your custom content either before, in the middle, or after the content:

<?php
add_filter('the_content', 'myfunc_subscriber_footer');

function myfunc_subscriber_footer($content)
{
    if (is_single())
    {
        $content .= '<h3>Enjoyed this article</h3>?';
        $content .= '<p><a href="http://example.com/feed">Subscribe to my RSS feed</a>!</p>';       
    }

    return $content;
}
?>

Notice that you are also using the is_single() conditional tag (more about conditional tags) to verify that your subscribe text is added only on a single post page. The $content variable stores all of the post or page content, so by appending your subscribe text you are adding it to the bottom of your post content.

In the future, if you decide to change this message you can change it in one place, rather than updating every post in your website.

Share            
X
                                                                                                        

Leave a Reply

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