the_content
This is a WordPress - the_content hook. The plugin just uses it.
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!';
}Where the hook is called
$post_content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $post_content ) );
$description = apply_filters( 'the_content', str_replace( ']]>', ']]>', $description ) );
Where the hook is used in WooCommerce
add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ), 10 );
add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ), 10 );
remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ) );
remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ) );