Filter the_content modifies post content HTML before it is displayed, letting you append, rewrite, or wrap the body of every rendered post. It also runs for feeds, the REST API, and excerpts trimmed from content, so callbacks usually need conditional guards.
Filters the post content.
$contentstringEvery example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Add to the content only on singular main-query views so the extra markup stays out of archives, feeds, and REST responses.
add_filter( 'the_content', 'myplugin_append_newsletter_cta' );
function myplugin_append_newsletter_cta( $content ) {
if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
$content .= sprintf(
'<p class="newsletter-cta"><a href="%s">%s</a></p>',
esc_url( home_url( '/newsletter/' ) ),
esc_html__( 'Join my newsletter', 'myplugin' )
);
}
return $content;
}Always return $content; a callback that returns nothing blanks every post on the site. Without the in_the_loop()/is_main_query() guard, the same markup leaks into feeds, wp_trim_excerpt(), and REST API rendered content, where this filter also fires.
wp-content/themes/twentyseventeen/template-parts/post/content-audio.php:45file scopewp-content/themes/twentyseventeen/template-parts/post/content-video.php:45file scopewp-content/themes/twentytwentyone/inc/template-functions.php:401twenty_twenty_one_print_first_instance_of_block()wp-includes/blocks.php:1464insert_hooked_blocks_into_rest_response()wp-includes/blocks/post-content.php:50render_block_core_post_content()wp-includes/comment.php:3044do_trackbacks()wp-includes/feed.php:196get_the_content_feed()wp-includes/formatting.php:3984wp_trim_excerpt()wp-includes/post-template.php:256the_content()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:862WP_REST_Attachments_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:1967WP_REST_Posts_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:652WP_REST_Revisions_Controller::prepare_item_for_response() * Filters the post content. * * @since 0.71 * * @param string $content Content of the current post. */ $content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); echo $content;} /** * Retrieves the post content.Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.