Add custom text at the end of each post in WordPress automatically

Sometimes one of your WordPress projects will need custom text at the end of each post and page. It might be a catchphrase you want to use every time, or it might be a link, a form or anything else that you need to have after your regular content. Instead of hardcoding that into your website, you can create a hook which will work for you.

  1. Open functions.php
  2. Copy and paste the following code:
  3. function text_after_content($content) {
    if(!is_feed() && !is_home()) {
    $content.= "<h2>Want more awesome snippets like this one?</h2>";
    $content.= "<p>Don’t forget to check them at  <a href='http://firstsiteguide.com'>WP Loop</a></p>";
    }
    return $content;
    }
    
    add_filter ('the_content', 'text_after_content');
  4. Change the text assigned to $content variable
  5. Save changes

After this, each and every post and page within your theme will have whatever you write in the $content variable. This function takes the text from your variable and hooks it directly to the_content which means that your text is used every time the content is called into action and that makes it the same as you have written the text manually for each post/pages. Awesome, isn’t it?

As you can see from the second line, the function is checking for feed and home pages. If those pages are recognized, the function will not embed your custom text after the content. If you choose to do so, you can remove the conditions and therefore have the text written in your RSS feeds and/or your homepage as well:

To have the text in RSS feed, replace the 2nd line with the following:

if (!is_home()) {

To have the text shown on your homepage, you will have to remove the second condition:

if (!is_feed()) {

Similarly, if you want to prepopulate your posts, pages and custom post types with some different content which will be automatically set in your editor so you can still work with the text, see how you can do that.

If you want to know learn more about WordPress, check the WordPress basics,  and see more tutorials that will help you level up your skills. We also suggest checking our resources which will help you to become a better webmaster.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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