How To Filter WordPress Content

WordPress as CMS, allows you to add the custom content within the WordPress post created within your site. You can add that above or below the post as required. Hence, here we will study the same, ie, how to filter WordPress content to add your own content before/after the post created within your site.

What is WordPress content referring to?

So, first of all, let’s know about what WordPress content is referring to. Well, it is just referring to the posts or pages created in your site, which displays the full content within its single page.

Now, what if you want to add the custom text within all of your WordPress posts and pages? Well, if you want the same content then, you may need to add that content in each page/post created within your site. But, what if we say that you can modify the WordPress feature to include the same text within the posts/page created in your site. Is it doable? Well, it is doable and here, we will cover up about this, ie, how to filter WordPress content.

How to filter WordPress content?

Now, sine you know that WordPress allows you to add custom content within WordPress, now, you may want to know about how to do so. Well, we have got you covered up for the same. For this feature, we will use the filter hook of WordPress the_content to include our own custom content after each of the post/page created within the site either before or after the content.

First of all to filter WordPress content, you need to open up the functions.php file of your theme. Now, after you have opened it up, you can create the function as below to filter WordPress content:

function theme_slug_after_post_content( $content ) {
$output = esc_html__('Disclaimer: This post is from other resources and we get some of the credits on adding this post within our site.', 'theme-textdomain');

return $content . $output;
}

add_filter( 'the_content', 'theme_slug_after_post_content' );

Now, adding the above PHP code in the mentioned file of your theme will add Disclaimer: This post is from other resources and we get some of the credits on adding this post within our site. to each of the post/page created within your site.

But, how about creating that before the post/page created within your site. Well, for that too, we have got you covered up. You just need to place the below PHP code in the mentioned file previously for same:

function theme_slug_before_post_content( $content ) {
if ( has_post_thumbnail() ) {
$thumbnail = get_the_post_thumbnail();
}

return $thumbnail . $content;
}

add_filter( 'the_content', 'theme_slug_before_post_content' );

Here, from above, if the theme does not support the featured image to display before the content then, adding the above PHP code do display the featured image before the post content within your site.

From the above 2 PHP codes, you can see the difference is within the return function, in which what to return first. The value $content returns the content of the post/pages, so, to add the custom content before post content, you need to add the custom value at first before the $content and for after the post/page content, just after that.

Output of the filter WordPress content

Now, after that you have known about how to filter WordPress content, now, we will see the output of the above PHP codes for the same. So, let’s move ahead and see the output.

Here, in the above screenshot, you can see that the first image, ie, left side image is default without any WordPress filter hook. And after we have applied the filter hook for WordPress content, you can see that custom text is being added up after the post content.

You may also want to read:

  1. Add And Display Featured Image In WordPress
  2. How To Limit Excerpt Length In WordPress
  3. Make YouTube And Vimeo Video Responsive In WordPress
  4. Solve Maximum Execution Time Of 60 Seconds Exceeded
  5. Integrate Bootstrap NavBar 4 Into WordPress Theme

Conclusion

If you have successfully followed this tutorial and have became successfully able to filter WordPress content in your custom built in WordPress theme or plugin then, we can assume that you are now more familiar on this feature than before. But, if you are still confused about how to do it and want more help on this then do drop the comment below in the comment box and we will follow it up.

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.