bbp_template_include_theme_compat( string $template = '' )

Reset main query vars and filter ‘the_content’ to output a bbPress template part as needed.


Description Description


Parameters Parameters

$template

(Optional)

Default value: ''


Top ↑

Source Source

File: includes/core/theme-compat.php

function bbp_template_include_theme_compat( $template = '' ) {

	/**
	 * Bail if a root template was already found. This prevents unintended
	 * recursive filtering of 'the_content'.
	 *
	 * @link https://bbpress.trac.wordpress.org/ticket/2429
	 */
	if ( bbp_is_template_included() ) {
		return $template;
	}

	/**
	 * If BuddyPress is activated at a network level, the action order is
	 * reversed, which causes the template integration to fail. If we're looking
	 * at a BuddyPress page here, bail to prevent the extra processing.
	 *
	 * This is a bit more brute-force than is probably necessary, but gets the
	 * job done while we work towards something more elegant.
	 */
	if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
		return $template;
	}

	// Define local variable(s)
	$bbp_shortcodes = bbpress()->shortcodes;

	// Bail if shortcodes are unset somehow
	if ( ! is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) {
		return $template;
	}

	/** Users *************************************************************/

	if ( bbp_is_single_user_edit() || bbp_is_single_user() ) {

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => 0,
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => bbp_buffer_template_part( 'content', 'single-user', false ),
			'post_type'      => '',
			'post_title'     => bbp_get_displayed_user_field( 'display_name' ),
			'post_status'    => bbp_get_public_status_id(),
			'is_archive'     => false,
			'comment_status' => 'closed'
		) );

	/** Forums ************************************************************/

	// Forum archive
	} elseif ( bbp_is_forum_archive() ) {

		// Page exists where this archive should be
		$page = bbp_get_page_by_path( bbp_get_root_slug() );

		// Should we replace the content...
		if ( empty( $page->post_content ) ) {

			// Use the topics archive
			if ( 'topics' === bbp_show_on_root() ) {
				$new_content = $bbp_shortcodes->display_topic_index();

			// No page so show the archive
			} else {
				$new_content = $bbp_shortcodes->display_forum_index();
			}

		// ...or use the existing page content?
		} else {
			$new_content = apply_filters( 'the_content', $page->post_content );
		}

		// Should we replace the title...
		if ( empty( $page->post_title ) ) {

			// Use the topics archive
			if ( 'topics' === bbp_show_on_root() ) {
				$new_title = bbp_get_topic_archive_title();

			// No page so show the archive
			} else {
				$new_title = bbp_get_forum_archive_title();
			}

		// ...or use the existing page title?
		} else {
			$new_title = apply_filters( 'the_title',   $page->post_title   );
		}

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => ! empty( $page->ID ) ? $page->ID : 0,
			'post_title'     => $new_title,
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => $new_content,
			'post_type'      => bbp_get_forum_post_type(),
			'post_status'    => bbp_get_public_status_id(),
			'is_archive'     => true,
			'comment_status' => 'closed'
		) );

	// Single Forum
	} elseif ( bbp_is_forum_edit() ) {

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => bbp_get_forum_id(),
			'post_title'     => bbp_get_forum_title(),
			'post_author'    => bbp_get_forum_author_id(),
			'post_date'      => 0,
			'post_content'   => $bbp_shortcodes->display_forum_form(),
			'post_type'      => bbp_get_forum_post_type(),
			'post_status'    => bbp_get_forum_visibility(),
			'is_single'      => true,
			'comment_status' => 'closed'
		) );

		// Lock the forum from other edits
		bbp_set_post_lock( bbp_get_forum_id() );

	} elseif ( bbp_is_single_forum() ) {

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => bbp_get_forum_id(),
			'post_title'     => bbp_get_forum_title(),
			'post_author'    => bbp_get_forum_author_id(),
			'post_date'      => 0,
			'post_content'   => $bbp_shortcodes->display_forum( array( 'id' => bbp_get_forum_id() ) ),
			'post_type'      => bbp_get_forum_post_type(),
			'post_status'    => bbp_get_forum_visibility(),
			'is_single'      => true,
			'comment_status' => 'closed'
		) );

	/** Topics ************************************************************/

	// Topic archive
	} elseif ( bbp_is_topic_archive() ) {

		// Page exists where this archive should be
		$page = bbp_get_page_by_path( bbp_get_topic_archive_slug() );

		// Should we replace the content...
		if ( empty( $page->post_content ) ) {
			$new_content = $bbp_shortcodes->display_topic_index();

		// ...or use the existing page content?
		} else {
			$new_content = apply_filters( 'the_content', $page->post_content );
		}

		// Should we replace the title...
		if ( empty( $page->post_title ) ) {
			$new_title = bbp_get_topic_archive_title();

		// ...or use the existing page title?
		} else {
			$new_title = apply_filters( 'the_title',   $page->post_title   );
		}

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => ! empty( $page->ID ) ? $page->ID : 0,
			'post_title'     => bbp_get_topic_archive_title(),
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => $new_content,
			'post_type'      => bbp_get_topic_post_type(),
			'post_status'    => bbp_get_public_status_id(),
			'is_archive'     => true,
			'comment_status' => 'closed'
		) );

	// Single Topic
	} elseif ( bbp_is_topic_edit() || bbp_is_single_topic() ) {

		// Split
		if ( bbp_is_topic_split() ) {
			$new_content = bbp_buffer_template_part( 'form', 'topic-split', false );

		// Merge
		} elseif ( bbp_is_topic_merge() ) {
			$new_content = bbp_buffer_template_part( 'form', 'topic-merge', false );

		// Edit
		} elseif ( bbp_is_topic_edit() ) {
			$new_content = $bbp_shortcodes->display_topic_form();

			// Lock the topic from other edits
			bbp_set_post_lock( bbp_get_topic_id() );

		// Single
		} else {
			$new_content = $bbp_shortcodes->display_topic( array( 'id' => bbp_get_topic_id() ) );
		}

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => bbp_get_topic_id(),
			'post_title'     => bbp_get_topic_title(),
			'post_author'    => bbp_get_topic_author_id(),
			'post_date'      => 0,
			'post_content'   => $new_content,
			'post_type'      => bbp_get_topic_post_type(),
			'post_status'    => bbp_get_topic_status(),
			'is_single'      => true,
			'comment_status' => 'closed'
		) );

	/** Replies ***********************************************************/

	// Reply archive
	} elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) {

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => 0,
			'post_title'     => esc_html__( 'Replies', 'bbpress' ),
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => $bbp_shortcodes->display_reply_index(),
			'post_type'      => bbp_get_reply_post_type(),
			'post_status'    => bbp_get_public_status_id(),
			'is_archive'     => true,
			'comment_status' => 'closed'
		) );

	// Single Reply
	} elseif ( bbp_is_reply_edit() || bbp_is_single_reply() ) {

		// Move
		if ( bbp_is_reply_move() ) {
			$new_content = bbp_buffer_template_part( 'form', 'reply-move', false );

		// Edit
		} elseif ( bbp_is_reply_edit() ) {
			$new_content = $bbp_shortcodes->display_reply_form();

			// Lock the reply from other edits
			bbp_set_post_lock( bbp_get_reply_id() );

		// Single
		} else {
			$new_content = $bbp_shortcodes->display_reply( array( 'id' => get_the_ID() ) );
		}

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => bbp_get_reply_id(),
			'post_title'     => bbp_get_reply_title(),
			'post_author'    => bbp_get_reply_author_id(),
			'post_date'      => 0,
			'post_content'   => $new_content,
			'post_type'      => bbp_get_reply_post_type(),
			'post_status'    => bbp_get_reply_status(),
			'is_single'      => true,
			'comment_status' => 'closed'
		) );

	/** Views *************************************************************/

	} elseif ( bbp_is_single_view() ) {

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => 0,
			'post_title'     => bbp_get_view_title(),
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => $bbp_shortcodes->display_view( array( 'id' => get_query_var( bbp_get_view_rewrite_id() ) ) ),
			'post_type'      => '',
			'post_status'    => bbp_get_public_status_id(),
			'is_archive'     => true,
			'comment_status' => 'closed'
		) );

	/** Search ************************************************************/

	} elseif ( bbp_is_search() ) {

		// Reset post
		bbp_theme_compat_reset_post( array(
			'ID'             => 0,
			'post_title'     => bbp_get_search_title(),
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => $bbp_shortcodes->display_search( array( 'search' => get_query_var( bbp_get_search_rewrite_id() ) ) ),
			'post_type'      => '',
			'post_status'    => bbp_get_public_status_id(),
			'is_archive'     => true,
			'comment_status' => 'closed'
		) );

	/** Topic Tags ********************************************************/

	// Topic Tag Edit
	} elseif ( bbp_is_topic_tag_edit() || bbp_is_topic_tag() ) {

		// Stash the current term in a new var
		set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) );

		// Show topics of tag
		if ( bbp_is_topic_tag() ) {
			$new_content = $bbp_shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) );

		// Edit topic tag
		} elseif ( bbp_is_topic_tag_edit() ) {
			$new_content = $bbp_shortcodes->display_topic_tag_form();
		}

		// Reset the post with our new title
		bbp_theme_compat_reset_post( array(
			'ID'             => 0,
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => $new_content,
			'post_type'      => '',
			'post_title'     => sprintf( esc_html__( 'Topic Tag: %s', 'bbpress' ), bbp_get_topic_tag_name() ),
			'post_status'    => bbp_get_public_status_id(),
			'is_tax'         => true,
			'is_archive'     => true,
			'comment_status' => 'closed'
		) );
	}

	/**
	 * Bail if the template already matches a bbPress template. This includes
	 * archive-* and single-* WordPress post_type matches (allowing
	 * themes to use the expected format) as well as all bbPress-specific
	 * template files for users, topics, forums, etc...
	 *
	 * We do this after the above checks to prevent incorrect 404 body classes
	 * and header statuses, as well as to set the post global as needed.
	 *
	 * @see https://bbpress.trac.wordpress.org/ticket/1478/
	 */
	if ( bbp_is_template_included() ) {
		return $template;

	/**
	 * If we are relying on the built-in theme compatibility API to load
	 * the proper content, we need to intercept the_content, replace the
	 * output, and display ours instead.
	 *
	 * To do this, we first remove all filters from 'the_content' and hook
	 * our own function into it, which runs a series of checks to determine
	 * the context, and then uses the built in shortcodes to output the
	 * correct results from inside an output buffer.
	 *
	 * Uses bbp_get_theme_compat_templates() to provide fall-backs that
	 * should be coded without superfluous mark-up and logic (prev/next
	 * navigation, comments, date/time, etc...)
	 *
	 * Hook into the 'bbp_get_bbpress_template' to override the array of
	 * possible templates, or 'bbp_bbpress_template' to override the result.
	 */
	} elseif ( bbp_is_theme_compat_active() ) {
		bbp_remove_all_filters( 'the_content' );

		$template = bbp_get_theme_compat_templates();
	}

	// Filter & return
	return apply_filters( 'bbp_template_include_theme_compat', $template );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.