the_content
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
$content = apply_filters( 'the_content', $content );
$response->data['content']['rendered'] = apply_filters( 'the_content', $response->data['content']['raw'] );
$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) );
$content = apply_filters( 'the_content', get_the_content() );
$data['content']['rendered'] = apply_filters( 'the_content', $post->post_content );
$data['content']['rendered'] = post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content );
'rendered' => apply_filters( 'the_content', $post->post_content ),
$text = apply_filters( 'the_content', $text );
$excerpt = apply_filters( 'the_content', $post->post_content );
Where the hook is used in WordPress
remove_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', $priority );
remove_filter( 'the_content', 'wpautop', $priority );
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
add_filter( 'the_content', 'wpautop', $current_priority - 1 );
remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
add_filter( $filter, 'capital_P_dangit', 11 );
add_filter( 'the_content', 'apply_block_hooks_to_content_from_post_object', 8 ); // BEFORE do_blocks().
add_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'wptexturize' );
add_filter( 'the_content', 'convert_smilies', 20 );
add_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'shortcode_unautop' );
add_filter( 'the_content', 'prepend_attachment' );
add_filter( 'the_content', 'wp_replace_insecure_home_url' );
add_filter( 'the_content', 'do_shortcode', 11 ); // AFTER wpautop().
add_filter( 'the_content', 'wp_filter_content_tags', 12 ); // Runs after do_shortcode().
$filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags', 12 );
$filter_block_removed = remove_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'wp_filter_content_tags', 12 );
remove_filter( 'the_content', 'prepend_attachment' );