How to add something to the_content in WordPress using PHP

Sometimes we need to add additional text or elements to the_content in WordPress. This can include links, icons, shout outs, author biographies, and so forth. I wanted to add an Apple Podcasts badge underneath each post in my podcast categories, adding a link to the badge, depending on the category. I’ll elaborate how I did this in a later post.

Right now, let’s have a look at how we can add elements to the content retrieved from WordPress. This should work independently of your theme, and no theme-file-hacking is necessary to accomplish this.

All we need to do is intercept the_content as it is requested, append our elements and return an amended version of the_content. Let’s see how this works.

Intercepting the_content

When the_content is requested from the database by WordPress, we can hook into a filter and intercept it. We can do this by creating a function like this:

By itself this won’t do anything, but it illustrates the principle nicely. Add this to your child theme’s functions.php file, or use it in plugins as appropriate. $content is a variable that contains everything that is printed after the title and before the comments.

Injecting new content

Inside our function, we can do anything we like with the content now. I’ll add some text before and after to demonstrate how this works.

Here we declare two variables with pre and post content text. These could include links and other HTML elements, even inline CSS classes so you can later style those elements independently.

Now we concatenate all variables using the . operator in PHP, putting it all into the $content variable again before returning it. That’s all the magic we need right now.

Since this is PHP, you can do anything you like inside that function, like checking if the content is appended / prepended to the correct category or if other conditions are met.

Thanks to WP Developers for putting me on the right track during my investigations 🙂

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

4 thoughts on “How to add something to the_content in WordPress using PHP

    1. Hi Clive, glad to hear you liked it!

      There’s benefits to both methods, so with this one, the benefit is that changes are applied on the fly and are not persisted to the database. This is useful if you may want to tweak the_content() differently at some point in the future. In my example, I wanted to add a link and icon to a podcast URL, but if that URL ever changes, it wouldn’t be beneficial for me to have it saved to the database.
      But then, every use case is different. For changes to persist in the database, you need to execute a command.

      I’ve only used it for my own options in themes and plugins. As an example, take a look at my 2013 Header Ad plugin on GitHub: https://github.com/versluis/2013-Header-Ad. Examine a file called p2013-header-ad.php, which is the main file that can load and save content from/to the database. That’s not applicable to the_content() though.

      To make a persistent change to the_content() in the database, you can do one of two things:

      Hope this puts you on the right track 🙂

      1. Thanks, using phpMyAdmin changes every record, I am trying to add a #hashtag to a post then when there is a reply add that same hashtag to each comment after it has been made, I think maybe I need to look more closely at the WP Query and the Header-Ad.

Leave a Reply to Clive Cancel reply