the_content Filter: Modifying Post Content Safely
Use the the_content filter safely: guard with is_singular, in_the_loop and is_main_query, choose the right priority relative to wpautop and shortcodes, avoid infinite loops and duplicate output, handle REST and feeds, and know when a block filter is better.

The the_content filter lets you change a post's HTML before it is displayed, for example to add a call-to-action box, related posts or a disclosure. Always return the content, and guard your callback with is_singular( 'post' ), in_the_loop() and is_main_query() so it only runs on the main post and not in widgets, excerpts, menus or related-post blocks. Pick a priority deliberately: core runs do_blocks at 9, wpautop at 10 and shortcodes at 11.
A safe pattern
add_filter( 'the_content', 'acme_append_cta', 20 );
function acme_append_cta( $content ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$cta = '<aside class="acme-cta"><p>' . esc_html__( 'Need help with your site?', 'acme' )
. ' <a href="' . esc_url( home_url( '/contact/' ) ) . '">' . esc_html__( 'Talk to us', 'acme' ) . '</a></p></aside>';
return $content . $cta;
}
Priority 20 runs after blocks, paragraphs and shortcodes are processed, so your HTML is not wrapped in extra <p> tags.
Why the guards matter
is_singular(): without it, the box appears on every post in archives.in_the_loop(): SEO plugins and themes callapply_filters( 'the_content' )outside the loop to build descriptions or schema; your box would end up in meta tags.is_main_query(): secondary loops (related posts, sidebars) also run the filter.
Inserting content in the middle
add_filter( 'the_content', function ( $content ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$parts = explode( '</p>', $content );
if ( count( $parts ) < 5 ) {
return $content;
}
$box = '<div class="acme-inline-cta">...</div>';
array_splice( $parts, 3, 0, $box ); // after the third paragraph
return implode( '</p>', $parts );
}, 20 );
String splitting is fragile with complex blocks. For block content, consider render_block on core/paragraph with a counter, covered in our block filters guide.
Avoiding infinite loops and duplicates
- Do not call
the_content()orapply_filters( 'the_content', ... )inside your callback for the same post: it re-enters your filter. - If you must render another post's content, temporarily
remove_filter()your callback, then add it back. - Use a static flag to stop the box appearing twice when themes call the filter more than once.
Feeds, REST and excerpts
- Feeds run
the_contenttoo. Checkis_feed()if the box should not appear in RSS. - The REST API returns
content.renderedafter running the filter, so headless front ends receive your additions unless you checkwp_is_serving_rest_request()(WordPress 6.5+). - Auto-generated excerpts strip tags from the content, so added text may show up in excerpts. Wrap additions in elements the excerpt logic removes, or use
get_the_excerptfilters.
When not to use the_content
- For content before or after the post in a theme you control, add it in the template or a block pattern instead.
- For changes to specific blocks, use
render_block. - For permanent edits, change the stored content; filters run on every view.
Common mistakes
- Forgetting to return
$content: the post appears empty. - Echoing inside the filter: output appears above the post.
- No guards: boxes in archives, widgets and meta descriptions.
- Heavy queries per call: cache results; the filter can run many times per page.
- Unescaped dynamic values.
Frequently asked questions
Does the_content run in block themes?
Yes. The Post Content block applies the filter when rendering.
Why is my HTML wrapped in <p> tags?
Your filter runs before wpautop at priority 10. Use a higher number.
Can I target pages instead of posts?
Use is_singular( 'page' ) or is_page( 'pricing' ).
Conclusion
Guard with singular, loop and main-query checks, choose a priority after formatting, return the content and avoid re-entry. See the the_content reference and the in_the_loop() reference. Related: wp_head and wp_footer, template_redirect, hooks explained and plugin development.
Need content features added cleanly? See our customization service.
Save this practical guide
Add us as a preferred Google source, or ask an AI assistant to summarize this page and link back to the original guide when it is useful.
