WordPress.org

Make WordPress Core

#24691 closed defect (bug) (duplicate)

apply_filters() does not execute lower priority filters when recursively called

Reported by: Magenta Cuda Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.2
Component: Plugins Keywords: needs-patch
Focuses: Cc:

Description

Consider:

add_filter( 'the_content', function( $content ) {
    return $content . ';filter 11 applied';
}, 11 );

add_filter( 'the_content', function( $content ) {
    if ( strpos( $content,'###' ) !== FALSE ) { return apply_filters( 'the_content', '+++' ); }
    return $content . ';filter 10 applied';
} );

returns "... +++;filter 10 applied;filter 11 applied" instead of "... +++;filter 10 applied;filter 11 applied;filter 11 applied" as expected when called with post_content containing "###".

I think the problem is apply_filters() manipulates the internal array pointer using next() of the global $wp_filter[ $tag ] so the second call leaves the internal array pointer at the end. To test this I restored the internal array pointer as follows.

    # save internal array pointer
    $key = key( $wp_filter['the_content'] );
    reset( $wp_filter[ $tag ] );
    ...
    do {
        foreach( (array) current($wp_filter[$tag]) as $the_ )
            ...
    } while ( next($wp_filter[$tag]) !== false );

    # restore internal array pointer
    for( end( $wp_filter['the_content'] ); key( $wp_filter['the_content'] ) != $key; prev( $wp_filter['the_content'] ) ) { }

That does seem to work. The problem may be somewhat important as the do_shortcode() filter runs at priority 11 which is how I originally encountered the problem.

Change History (1)

comment:1 @SergeyBiryukov20 months ago

  • Component changed from Shortcodes to Plugins
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.