WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 16 months ago

#18037 new enhancement

inconsistency in filters for the_content and the_excerpt

Reported by: opajaap Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.2
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

My plugin replaces a keytext by a div tag using a filter:
add_filter('the_content', 'my_proc');

This filter appears also to be run in an excerpt, while my div is stripped in the excerpt.
When my div is stripped i want - instead of that div - to print a notification like - Here belongs my div -.

So, either i need a boolean variable or function that tells me that my filter is currently run by a call to the_excerpt(), OR the filter should not be run on the excerpt if it is not added to the excerpts filter list.

I tried a workaround by adding a new filter to the_excerpt and setting my own global to indicate i am in an excerpt, but unfortunately this filter is run after the the_content filter, despite fiddling with filter priorities. So, in a list (archive) the first excerpt is still in error.

The documentation says about filters:

the_content

applied to the post content retrieved from the database, prior to printing on the screen (also used in some other operations, such as trackbacks).

and

the_excerpt

applied to the post excerpt (or post content, if there is no excerpt) retrieved from the database, prior to printing on the screen (also used in some other operations, such as trackbacks).

It appears that not only the_excerpt filters are run on the content if there is no excerpt, what is fine, but it works also the other way around: the the_content filters are run on the excerpt. This is an inconsistency, and basically my problem.

Change History (3)

comment:1 @opajaap4 years ago

BTW looking at is_archive() or is_search() is not a solution. Many theme(modification)s use the_content() instead of the_excerpt() in which case i would draw the wrong conclusion.

Last edited 4 years ago by opajaap (previous) (diff)

comment:2 @mdgl3 years ago

Agreed the filters on these functions (and their get_* brothers) are a complete mess and we really need to be more consistent. The difficult is, as always backwards compatibility.

Your problem occurs because if there is no explicit excerpt created by the user, WP takes the first bit of text from the full post content instead, applying the filters declared on function the_content(). See function wp_trim_excerpt() in wp-includes/formatting.php which is called as a filter on get_the_excerpt().

There are many similar tickets, some such as #4679 going back a long way!

comment:3 @nacin16 months ago

  • Component changed from Plugins to Posts, Post Types
Note: See TracTickets for help on using tickets.