How To Use the_content Function And Filter In WordPress [Guide]

WordPress the_content function

Any WordPress developer knows that in order to display the content of a page/post they can use the_content() function. In this article, we’re going to take a deeper look at this function as well the_content filter that WordPress provides.

Using WordPress the_content Function + Arguments

The function definition according to WordPress.org looks like this:

the_content(string $more_link_text = null, bool $strip_teaser = false)

As you can see, the function accepts 2 arguments:

  • $more_link_text – let’s you use custom text for “Read More” link.
  • $strip_teaser – defines if the teaser content before the $more_link_text should be stripped.

Let’s say you want to display the content and have the “Read More” link say “Continue…”. Here is how you can do it:

<?php

the_content('Continue...');

The WordPress the_content Filter

In some cases, you want to modify the text that is being displayed by the the_content function. Thankfully, WordPress provides a filter that we can use exactly for that purpose.

Here is how the filter is being used by the function:

<?php

$content = apply_filters('the_content', $content);

Adding Custom Hooks To the_content filter

Let’s say we have a function that adds social share links called append_social_share_buttons that appends social share buttons to the post content. Here is how we would use it:

<?php

add_filter('the_content', 'append_social_share_buttons');

function append_social_share_buttons($content) {

    // Your function's logic that appends social share buttons

    return $content;

}

Difference Between the_content and get_the_content Functions

As you can see on the image below, the_content function uses get_the_content internally to do all the heavy lifting.

WordPress the_content function
WordPress the_content function body

So these functions are very similar. However, there are main 2 differences that are worth mentioning:

  1. the_content() function will automatically echo/display the contents of the current post whereas the get_the_content() function will only return the result and not display.
  2. get_the_content() allows you to pass the post object or a post ID and that post will be used to get the contents from. On the other hand, the_content() function will ALWAYS display the contents of the current post.

Wrapping Up

As you can see, you can use the the_content() function to display the contents of the current post and the the_content filter to control what’s being displayed. They can both be used to customize your WordPress website in a way that suits you and your needs.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

FREE TRAINING

How To Pick The Right Platform For Your Dream Course Or Membership Site

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.