the_content WordPress Filter Hook

The the_content hook is one of the most important hooks in WordPress. It is used to filter the content of a post before it is displayed on the front end of a WordPress site. There are a number of different filters that can be applied to the content by using this hook, and it is one of the most versatile hooks in the WordPress core.

apply_filters( 'the_content', string $content ) #

Filters the post content.


Parameters

$content

(string)Content of the current post.


Top ↑

More Information

This filter is used to filter the content of a post after it is retrieved from the database and before it is printed to the screen.

Top ↑

Usage

When using this filter it’s important to check if you’re filtering the content in the main query with the conditionals is_main_query() and in_the_loop(). The main post query can be thought of as the primary post loop that displays the main content for a post, page or archive. Without these conditionals you could unintentionally be filtering the content for custom loops in sidebars, footers, or elsewhere.

add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 1 );

function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single Post.
    if ( is_singular() && in_the_loop() && is_main_query() ) {
        return $content . esc_html__( 'I’m filtering the content inside the main loop', 'wporg');
    }

    return $content;
}

 


Top ↑

Source

File: wp-includes/post-template.php

View on Trac



Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.