Adding a fixed text or element to all posts. The “the_content” filter

We often need to add a fixed text or html element to the text of all posts: a signature, a link, a little logo, even a form. In fact, there are some ways to do this task but perhaps the easiest way is to use the the_content filter.

Basic schema of operations for the_content filter

The the_content filter –in general any WordPress filter– is a kind of funnel through which WordPress passes the post_content before being displayed on screen. Using this funnel (filter), ie, adding operations inside it, we can change de content of the post_content field for all Posts but for doing this we have to do two operations.

  1. we need to create a function to do this operation and,
  2. we have to inform to WordPress that we want that when “he” is using the funnel, please, additionally executes our function.

Creating a filter function for Post Content

To create a function for a WordPress filter, first we need to know what kind of data passes through the filter, in this case, if we check the technical information about the the_content filter we can see that it is a funnel that only receives one kind of data: the content of the post as a string; so our function must receive one variable and must return one variable. Remember, our function will be executed inside a funnel so all input data must become in output data. Based on this view our function will have the following basic structure:

<?php
function filter_for_content ( $content ) {

     /*
      * here, the changes to do with the $content var
      */

     return $content;

}
?>

the function receives one variable called $content, then applies the changes to variable, and finally, returns the variable. See the schema of our filter function in next graph.
A filter function schema
I our case, we could create a function for adding a fixed text at the end of content, for example our signature:

<?php
function filter_for_content ( $content ) {

     $content = $content . '<p class="signature">' .
                           '<span class="author-name">Father Vito Cornelius, PhD.</span> .
                           '<span class="author-studies">Extraterrestrial Lifeforms Expert</span>' .
                           '</p>';

     return $content;

}
?>

Adding our filter function to WordPress the_content filter

Once we have written the filter function, it’s time to add it to the the_content and for this operation we’ll use another WordPress function called add_filter so, we’ll go to add one more line to our former code. (See the add_filter function technical documentation in WordPress.org).

<?php
function filter_for_content ( $content ) {

     $content = $content . '<p class="signature">' .
                           '<span class="author-name">Father Vito Cornelius, PhD.</span> .
                           '<span class="degree">Extraterrestrial lifeforms expert</span>' .
                           '</p>';

     return $content;

}
add_filter('the_content', 'filter_for_content', 10, 1 );
?>

With this last instruction we change the behaviour of the the_content WordPress filter so that now, all data received for the_content filter will also pass throught our function. The new schema for input/output is shown at this next graph.

2016.04.28.01.the_content with an additional function

The last step is to add the former code inside the functions.php file of the theme and from that moment, all the post will be displayed adding our signature like in this next example.

2016.04.28.01.A graphical example

Of course, it’s probably that you want to write a couple of new additional CSS lines in your style.css file for styling this new element of the post_content.

In summary, this is an ease technique that allows us to change completely the data of the post_content field adding (or substracting) any element, text, photo, button, etc. Try it!

Have a nice WordPressing!

4 Comments on Adding a fixed text or element to all posts. The “the_content” filter

Leave a Reply to How to modify the FavIcon links of the function wp_site_icon in WordPress? | What about WordPress? Cancel Replay

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>