wppaste
WordPress

apply_filters( 'the_content', string $content )

Since
0.71

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.

Compatibility

WordPress
since 0.71
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Parameters

$contentstring
Content of the current post.

Code examples

Every 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.

Append a call-to-action to single posts

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.

Where this hook fires · 12

  • wp-content/themes/twentyseventeen/template-parts/post/content-audio.php:46file scope
  • wp-content/themes/twentyseventeen/template-parts/post/content-video.php:46file scope
  • wp-content/themes/twentytwentyone/inc/template-functions.php:408twenty_twenty_one_print_first_instance_of_block()
  • wp-includes/blocks.php:1506insert_hooked_blocks_into_rest_response()
  • wp-includes/blocks/post-content.php:50render_block_core_post_content()
  • wp-includes/comment.php:3113do_trackbacks()
  • wp-includes/feed.php:196get_the_content_feed()
  • wp-includes/formatting.php:3983wp_trim_excerpt()
  • wp-includes/post-template.php:256the_content()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:920WP_REST_Attachments_Controller::prepare_item_for_response()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:2007WP_REST_Posts_Controller::prepare_item_for_response()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:683WP_REST_Revisions_Controller::prepare_item_for_response()

Source code

	 * Filters the post content.	 *	 * @since 0.71	 *	 * @param string $content Content of the current post.	 */	$content = apply_filters( 'the_content', $content );	$content = str_replace( ']]>', ']]&gt;', $content );	echo $content;} /** * Retrieves the post content.

Changelog

Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.