apply_filters( 'the_content', string $content )
Filters the post content.
Contents
Parameters
-
$content
string -
Content of the current post.
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', 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;
}
Source
File: wp-includes/post-template.php
.
View all references
$content = apply_filters( 'the_content', $content );
Changelog
Version | Description |
---|---|
0.71 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
You can choose whether $content will have Shortcodes processed before or after your
the_content
function has executed by setting the Priority inadd_filter
. The default (10) will process Shortcodes after you have returned $content at the end of your Filter function. A large figure, such as 99, will already have processed Shortcodes before your Filter function is passed $content.Note that
the_content
filter isn’t used whenget_the_content()
function is called.Example
Consolidate multiple string replacement filters
String replacements are a common reason for filtering
the_content
and with PHP7 you can now consolidate multiple callback functions usingpreg_replace_callback_array();
if it makes sense to.Example:
the info at the bottom of the hook is wrong for most of the hooks :
Used by 7 functions | Uses 0 functions (At least it could be 0 function … ah ah ah !)
HERE is what i have for WP 5.1 :
* (7 is right, 0 is wrong)
* and i am not counting apply_filters_ref_array (happily there is no for “the_content” )
apply_filter
the_content comment.php 2656
the_content feed.php 191
the_content formatting.php 3692
the_content post-template.php 247
the_content class-wp-rest-attachments-controller.php 310
the_content class-wp-rest-posts-controller.php 1532
the_content class-wp-rest-revisions-controller.php 545
add_filter
the_content blocks.php 269
the_content blocks.php 296
the_content class-wp-embed.php 32
the_content class-wp-embed.php 39
the_content default-filters.php 142
the_content default-filters.php 172
the_content default-filters.php 173
the_content default-filters.php 174
the_content default-filters.php 175
the_content default-filters.php 176
the_content default-filters.php 177
the_content default-filters.php 178
the_content default-filters.php 519
There should be an indication of the WP version.
my doc (WP 5.1) is available here (https://blog.mailpress.org/hooks/wordpress-5-1/) !
This is also a good hook to use if you want to display custom fields from a plugin (including ACF fields).
The following example is from Jeff Starr’s plugins course.
Using the example to add classes to
p
tag &h2
tag.A useful hook to filter out vulgar words from your site and keep things nice and tidy for your site visitors.
The usage example is misleading. Unless you’re not interested in pages, it should be
You should also add a sufficiently high priority to the filter to hook on
the_content
before rendering happens:This should go in functions.php.
It took me half a day to figure out why my filter was doing nothing.
Top ↑
Feedback
Thanks, the example was edited accordingly. — By Jb Audras —
The usage example is NOT misleading. If condition is checking for is_singular() which is true for post, page, custom post type or attachment (doc). Your suggestion to use (is_single() || is_page()) instead of is_singular() is not optimal. To your second though about higher priority to the filer in order to be applied before page is rendered. I guess you had other callbacks bind to ‘the_content’ hook with higher priority than yours (probably, you had a default 10) which overwrote your changes and it seemed to you that the page was rendered before your filter was applied, which again is a wrong perception of what ‘the_content’ hook is doing. All what it does is written on the top of this page: “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.” There is no way how this filter can be applied after the page is rendered. — By fylh —
Using the example below verifies that if the ALT attribute of any image inserted in the content is empty, get the file name.
Top ↑
Feedback
The note will not work with content with accent.. there is a problem with encoding.. should use :
— By keusta —