the_content
Filters a post's content before it's shown. Use it to add or change text before or after the post body.
When it fires
WordPress applies the_content when a theme calls the_content() and when the Post Content block renders, and also for feeds, REST API responses and automatic excerpts. Core's own filters run on it too: blocks are rendered at priority 9, paragraphs are added at 10 and shortcodes run at 11.
You receive the content as HTML and must return it, changed or not.
Parameters
$contentstring |
Content of the current post. |
|---|
What to return
The content as an HTML string. Always return $content, even when you don't change it.
Example: Add a note after single blog posts
add_filter( 'the_content', function ( $content ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$note = sprintf(
'<p class="post-note">%s</p>',
esc_html__( 'Thanks for reading. Questions? Leave a comment below.', 'my-snippets' )
);
return $content . $note;
} );
Good to know
- Check in_the_loop() and is_main_query() so your text isn't added to excerpts, sidebars or other posts shown on the page.
- Nothing escapes your output for you. Escape any text or URLs you add.
- Page builders and custom templates that don't call the_content() won't run this filter.