The the_content
filter makes it possible to modify the content of a post, page or other post type. It is probably one of the most commonly-used — and misused — filter hooks in the WordPress code base.
Misuse of the_content
is rampant largely due to the geography of a post’s content on the page. Developers want something to show up at the end of the post so sometimes they’ll use this hook to accomplish that placement. Unfortunately, whatever it is they’re hooking onto the beginning or end of the_content
often has little or nothing to do with the post itself.
It’s one of those filters where you can look to ‘how Core does it’ to really see its intended purpose. Out of the box, Core filters the_content
in about 10 different places. This is to accomplish much of the behind-the-scenes post cleanup users never really see, which include:
- oEmbed handling
- converting smilies to images
- removing unwanted characters
- auto-adding paragraph tags
- processing shortcodes
- and more
In all such cases, Core filters the post content itself, but doesn’t tack on unrelated stuff.
the_content
is evaluated in:
It accepts a single argument, a string containing the post’s content.
Example:
Let’s say you’re one of those people who just can’t get out of the habit of using two spaces after periods and you’ve been writing on your blog for years. The following will filter the_content
to convert those pesky double spaces to singles.
View the code example on Gist.