@robin-w or another mod – can you please delete:
https://bbpress.org/forums/topic/template-in-block-themes/#post-232249 (#post-232249)
https://bbpress.org/forums/topic/template-in-block-themes/#post-232250 (#post-232250)
https://bbpress.org/forums/topic/template-in-block-themes/#post-232256 (#post-232256)
They had issues and take up a lot of wasted space.
Seriously done this time! No more changes/revisions! It is what it is!
Posting a much cleaner revision of the code above.
- This one will work with either the BBPress Style Pack plugin template (looks great!) or the default BBPress template (looks horrible!).
- It will optionally include the required ‘template-canvas.php’ only if it’s missing.
- It will also only affect BBPress pages, not the rest of the site.
- It will also only affect FSE Block themes.
- If using default BBPress templates, it includes checks for if on forum root index page and loads extras/archive-forum.php (to prevent double-listing of the forum index), and loads extras/page-front-forums.php for “the rest of bbpress” (to prevent “nothing found” errors)
// function to include the wp-includes/template-canvas.php file if needed
function fse_bbpress_template( $template ) {
if ( is_bbpress() ) {
$template = ABSPATH . WPINC . '/template-canvas.php';
}
return $template;
}
// function to include the bbpress forum template file if needed
function fse_bbp_theme_compat( $template ) {
if ( is_bbpress() ) {
// BBPress Style Pack Plugin Forums Index Template
if ( defined( 'BSP_PLUGIN_DIR' ) ) {
$template = BSP_PLUGIN_DIR . '/templates/bbpress.php';
} else {
// Default BBPress
// if current page is bbpress forum root page, load archive-forum to prevent double-listing of index
if ( isset( get_queried_object()->name ) ) {
if ( get_queried_object()->name == 'forum' ) {
$template = WP_CONTENT_DIR . '/plugins/bbpress/templates/default/extras/archive-forum.php';
}
// else, load page-front-forums or the rest of bbpress is broken with empty pages
} else {
$template = WP_CONTENT_DIR . '/plugins/bbpress/templates/default/extras/page-front-forums.php';
}
}
}
return $template;
}
// main function for handling which theme file needs to be included
if ( ! function_exists( 'fse_bbpress_support' ) ) {
function fse_bbpress_support() {
// get current theme dir
$theme_dir = get_template_directory();
/*
* Detect if FSE theme or traditional.
* FSE Block themes require a theme.json file.
* Use that to check instead of theme name or parent theme name.
* Perhaps a better method is available, but this works for now
*/
if ( file_exists( $theme_dir . '/theme.json' ) ) {
// include wp-includes/template-canvas.php only if needed
if ( !basename( get_page_template() ) == 'template-canvas.php' ) {
add_filter( 'template_include', 'fse_bbpress_template' );
}
// include either the BSP template, or default BBPress template
add_filter ( 'bbp_template_include_theme_compat', 'fse_bbp_theme_compat' );
}
}
}
add_action( 'after_setup_theme', 'fse_bbpress_support' );
One issue with EVERY default BBPress template is that the header/footer/sidebar are trying to be pulled from template files that don’t exist in block themes. They’re being included, but don’t look right. I’m not sure of the best solution for that, other than to use the BBPress Style Pack Plugin which takes care of that and looks great.