the_contentfilter-hookWP 0.71

Filters post content after it is retrieved from the database but before it is displayed.

Always return the $content variable after processing it; otherwise, users will see an empty page.

Usage

add_filter( 'the_content', 'filter_function_name_11' );
function filter_function_name_11( $content ) {
	// Filter the content...

	return $content;
}

Parameters

$content(string)
String to filter and return.

Examples

#1 Filter the content of a particular page

This example can create a static page whose content is generated based on something such as a $_GET query variable, for example, a specified author in ?the_author=crank:

add_filter( 'the_content', 'my_the_content_filter' );
function my_the_content_filter( $content ){
	// Do nothing unless this is the debug page.
	if( $GLOBALS['post']->post_name != 'debug' )
		return $content;

	// Perform the required action.
	// The URL is assumed to contain the the_author query variable.
	return "This is the author page for: ". $_GET['the_author'];
}

#2 Post icon

This example adds an image before a post; see is_single(). The image is assumed to be named post_icon.png and located in the theme's images directory. The filter uses the lower priority 20 rather than the usual 10, so it runs after other callbacks.

add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ){
	if ( is_single() )
		// Add an image to the beginning of every page.
		$content = sprintf(
			'<img class="post-icon" src="%s/images/post_icon.png" alt="Post icon" title=""/>%s',
			get_bloginfo( 'stylesheet_directory' ),
			$content
		);

	// Return the content.
	return $content;
}

#3 Featured image at the beginning

This example adds a post's featured image before its content. The featured image is selected while editing the post, and the feature must be enabled; see add_theme_support(). It is added only to posts of the post post type:

add_filter( 'the_content', 'featured_image_before_content' );
function featured_image_before_content( $content ) {
	if ( is_singular('post') && has_post_thumbnail()) {
		$thumbnail = get_the_post_thumbnail();

		$content = $thumbnail . $content;
	}

	return $content;
}

#4 Advertisements in posts after a particular number of paragraphs

This example filters content and adds advertisement code after a specified number of paragraphs.

# Insert advertisements after a particular number of paragraphs.
add_filter( 'the_content', 'wpse_ad_content' );

function wpse_ad_content( $content ) {

	if( ! is_single() ){
		return $content;
	}

	// Paragraph number after which to insert the data.
	// Preferably supplied through a meta field.
	$paragraphAfter = 5;
	$return_content = '';
	$ads_block = '<a href="#">ADS</a>';
	$content = explode( "</p>", $content );

	foreach( $content as $i => $value ){

		$return_content .= $value . "</p>";

		if( $i === 0 ){
			$return_content .= $ads_block;
		}

		if( $i === $paragraphAfter ){
			$return_content .= $ads_block;
		}

		if( $i === array_key_last( $content ) ){
			$return_content .= $ads_block;
		}
	}

	return $return_content;
}

#5 Demonstration

To demonstrate the filter, append " The End!" to every post's content:

add_filter('the_content', 'the_end');
function the_end( $text ){
	return $text . ' The End!';
}

Changelog

Since 0.71 Introduced.

Where the hook is called

the_content()
the_content
insert_hooked_blocks_into_rest_response()
the_content
render_block_core_post_content()
the_content
get_the_content_feed()
the_content
WP_REST_Revisions_Controller::prepare_item_for_response()
the_content
WP_REST_Posts_Controller::prepare_item_for_response()
the_content
WP_REST_Attachments_Controller::prepare_item_for_response()
the_content
wp_trim_excerpt()
the_content
do_trackbacks()
the_content
wp-includes/post-template.php 256
$content = apply_filters( 'the_content', $content );
wp-includes/blocks.php 1566-1569
$response->data['content']['rendered'] = apply_filters(
	'the_content',
	$response->data['content']['raw']
);
wp-includes/blocks/post-content.php 50
$content = apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) );
wp-includes/feed.php 196
$content = apply_filters( 'the_content', get_the_content() );
wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php 702
$data['content']['rendered'] = apply_filters( 'the_content', $post->post_content );
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2007
$data['content']['rendered'] = post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content );
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 1438
'rendered' => apply_filters( 'the_content', $post->post_content ),
wp-includes/formatting.php 4054
$text = apply_filters( 'the_content', $text );
wp-includes/comment.php 3373
$excerpt = apply_filters( 'the_content', $post->post_content );

Where the hook is used in WordPress

wp-includes/blocks.php 1562
remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
wp-includes/blocks.php 1573
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
wp-includes/blocks.php 2604
remove_filter( 'the_content', 'wpautop', $priority );
wp-includes/blocks.php 2605
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
wp-includes/blocks.php 2624
add_filter( 'the_content', 'wpautop', $current_priority - 1 );
wp-includes/blocks.php 2625
remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
wp-includes/class-wp-embed.php 33
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
wp-includes/class-wp-embed.php 41
add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
wp-includes/default-filters.php 171
add_filter( $filter, 'capital_P_dangit', 11 );
wp-includes/default-filters.php 201
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', 8 ); // BEFORE do_blocks().
wp-includes/default-filters.php 202
add_filter( 'the_content', 'do_blocks', 9 );
wp-includes/default-filters.php 203
add_filter( 'the_content', 'wptexturize' );
wp-includes/default-filters.php 204
add_filter( 'the_content', 'convert_smilies', 20 );
wp-includes/default-filters.php 205
add_filter( 'the_content', 'wpautop' );
wp-includes/default-filters.php 206
add_filter( 'the_content', 'shortcode_unautop' );
wp-includes/default-filters.php 207
add_filter( 'the_content', 'prepend_attachment' );
wp-includes/default-filters.php 208
add_filter( 'the_content', 'wp_replace_insecure_home_url' );
wp-includes/default-filters.php 209
add_filter( 'the_content', 'do_shortcode', 11 ); // AFTER wpautop().
wp-includes/default-filters.php 210
add_filter( 'the_content', 'wp_filter_content_tags', 12 ); // Runs after do_shortcode().
wp-includes/formatting.php 4045
$filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags', 12 );
wp-includes/formatting.php 4051
$filter_block_removed = remove_filter( 'the_content', 'do_blocks', 9 );
wp-includes/formatting.php 4059
add_filter( 'the_content', 'do_blocks', 9 );
wp-includes/formatting.php 4068
add_filter( 'the_content', 'wp_filter_content_tags', 12 );
wp-includes/template-loader.php 96
remove_filter( 'the_content', 'prepend_attachment' );