Skip to:
Content

bbPress.org

Changeset 4972


Ignore:
Timestamp:
05/29/2013 02:24:03 AM (6 years ago)
Author:
johnjamesjacoby
Message:

Tweak theme compatibility to hook to loop_start and loop_end, to maximize compatibility with third party plugins and existing WordPress themes. Props r-a-y for the proof of concept with BuddyPress.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/core/theme-compat.php

    r4954 r4972  
    668668    if ( bbp_is_theme_compat_active() ) {
    669669
    670         // Remove all filters from the_content
    671         bbp_remove_all_filters( 'the_content' );
    672 
    673         // Add a filter on the_content late, which we will later remove
    674         add_filter( 'the_content', 'bbp_replace_the_content' );
     670        // Hook to the beginning of the main post loop to remove all filters
     671        // from the_content as late as possible.
     672        add_action( 'loop_start', 'bbp_theme_compat_main_loop_start',  9999 );
     673
     674        // Hook to the end of the main post loop to restore all filters to
     675        // the_content as early as possible.
     676        add_action( 'loop_end',   'bbp_theme_compat_main_loop_end',   -9999 );
    675677
    676678        // Find the appropriate template file
     
    688690 * Note that we do *not* currently use is_main_query() here. This is because so
    689691 * many existing themes either use query_posts() or fail to use wp_reset_query()
    690  * when running queries before the main loop, causing theme compat to fail.
     692 * when running queries before the main loop, causing theme compat to fail. We
     693 * do use in_the_loop() though, so we can narrow the scope down to the actual
     694 * main page content area.
    691695 *
    692696 * @since bbPress (r3034)
     
    896900    // Return possibly hi-jacked content
    897901    return $content;
     902}
     903
     904
     905/**
     906 * Helper function to conditionally toggle the_content filters in the main
     907 * query loop. Aids with theme compatibility.
     908 *
     909 * @since bbPress (r4972)
     910 * @internal Used only by theme compatibilty
     911 * @see bp_template_include_theme_compat()
     912 * @see bp_theme_compat_main_loop_end()
     913 */
     914function bbp_theme_compat_main_loop_start() {
     915
     916    // Bail if not the main query
     917    if ( ! in_the_loop() )
     918        return;
     919
     920    // Remove all of the filters from the_content
     921    bbp_remove_all_filters( 'the_content' );
     922
     923    // Make sure we replace the content
     924    add_filter( 'the_content', 'bbp_replace_the_content' );
     925}
     926
     927/**
     928 * Helper function to conditionally toggle the_content filters in the main
     929 * query loop. Aids with theme compatibility.
     930 *
     931 * @since bbPress (r4972)
     932 * @internal Used only by theme compatibilty
     933 * @see bbp_template_include_theme_compat()
     934 * @see bbp_theme_compat_main_loop_start()
     935 */
     936function bbp_theme_compat_main_loop_end() {
     937
     938    // Bail if not the main query
     939    if ( ! in_the_loop() )
     940        return;
     941
     942    // Put all the filters back
     943    bbp_restore_all_filters( 'the_content' );
    898944}
    899945
Note: See TracChangeset for help on using the changeset viewer.