How to adjust post content before echoing it to the screen? - React to WordPress

How to adjust post content before echoing it to the screen?

This one will be quick, I will show you how to adjust, change, nor filter the post’s content before echoing it to the screen.

It is fairly straightforward , you just need to use a WordPress filter for that

add_filter( 'the_content', 'filter_function_name' );

filter_function_name( $content ) {

    if ( is_single() ) {
        $content = str_replace( '<p>', '<p class="p-class">', $content );

    return $content; // a must - return content in any case


Here, I wanted to show you a little sneak peak of what is possible to achieve with the_content filter.

First, I have hooked a function filter_function_name to the “the_content” filter. Our function will receive 1 parameter – which is our post content.

Second, inside of the function, I checked if it is a single post page, using WordPress function is_single()

Third, if it is single returns true, I will replace all of the normal <p> tags with <p class="p-class">, Basically adding a class with the name p-class, to every paragraph inside of our post’s content. It was done using the php function str_replace.

Fourth and last, I am returning $content. It is a very important step – doesn’t matter if you changed the content or not, you always need to return it.

Add this code to your functions.php file inside of your active theme and your done!

That’s it for this snippet of code. Simple and easy way of adjusting post content before outputting it to the user.


One comment on “How to adjust post content before echoing it to the screen?”

Leave a Reply

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