W3cubDocs

/WordPress

apply_filters( 'the_content', string $content )

Filters the post content.

Description

Parameters

$content

(string) Content of the current post.

Source

File: wp-includes/post-template.php

View on Trac

Changelog

Version Description
0.71 Introduced.

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.

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' );

function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {
        return $content . "I'm filtering the content inside the main loop";
    }

    return $content;
}

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/hooks/the_content