Skip to content

Instantly share code, notes, and snippets.

@imath
Last active December 14, 2015 10:59
Show Gist options
  • Save imath/5076324 to your computer and use it in GitHub Desktop.
Save imath/5076324 to your computer and use it in GitHub Desktop.
This is a quick and dirty attempt in order to use the custom page template (an Admin can define in the WP Editor template select box) with BuddyPress 1.7 Theme Compat. I think the main challenge is not in loading the custom template, but it's too understand how the theme is rendering the layout. For instance : twentyeleven and twentytwelve are u…
<?php
/*
beginning of the code to paste in the functions.php of the active theme (twentyten, twentyeleven or twentytwelve)
If you want to adapt it to your theme, you'll need to check if it uses some body_class to render its layout and eventually
adapt lines 70 to 101.
*/
class Imath_WP_Editor_Template
{
public $found_template = false;
public $editor_template = false;
function __construct()
{
add_action( 'bp_setup_theme_compat', array( $this, 'prepare_template' ) );
}
function prepare_template()
{
$is_bp_component = bp_current_component();
if( empty( $is_bp_component ) )
return;
$page_id = get_queried_object_id();
if( !empty( $page_id ) ) {
$possible_template_locations = bp_get_template_stack();
$page_template = get_page_template_slug( $page_id );
if( empty( $page_template ) )
return;
$this->editor_template = $page_template;
foreach( $possible_template_locations as $template_location ) {
if( file_exists( $template_location . '/' . $page_template ) )
$this->found_template = $template_location . '/' . $page_template;
}
}
if( !empty( $this->found_template ) ) {
add_filter( 'bp_template_include_theme_compat', array( $this, 'include_theme_compat' ), 10, 1 );
/* if the theme is not using body class to render its templates,
you don't need this hook */
add_filter( 'body_class', array( $this, 'body_class' ), 99, 1 );
}
}
function include_theme_compat( $template )
{
if( !empty( $this->found_template ) )
return $this->found_template;
else
return $template;
}
function body_class( $classes )
{
if( empty( $this->editor_template ) )
return $classes;
/*
that's where the trouble begins, as depending on themes, some classes are
added or not in body class to render the layout
*/
$theme = get_template();
switch ( $theme ) {
case 'twentyten' :
/* twentyten doesn't need a specific body class for its full width page */
break;
case 'twentyeleven' :
/* twentyeleven needs that the singular class is not in body_class for its
sidebar-page.php template */
if( $this->editor_template == 'sidebar-page.php' ) {
foreach( (array) $classes as $key => $class ) {
if( $class == 'singular' )
unset( $classes[$key] );
}
}
break;
case 'twentytwelve' :
/* twentytwelve needs to add the full-with class to body_class for its
page-templates/full-width.php template
But for its page-templates/front-page.php it's not front-page but template-front-page ?!?
*/
if( $this->editor_template == 'page-templates/full-width.php' )
$classes[] = 'full-width';
break;
default :
break;
}
/********************************************************************************************
If themes are using the body_class to render their custom templates,
i think it can be possible, if they choose a class name from the template file name
e.g : if template file = full-width.php then body_class = full-width
If so, the above switch part is not required, and we can simply make something like this :
$body_class_parse = explode( '/', $this->editor_template );
$template_name = str_replace( '.php', '', array_pop( $body_class_parse ) );
if( !empty( $template_name ) )
$classes[] = $template_name;
********************************************************************************************/
return $classes;
}
}
new Imath_WP_Editor_Template();
/* end of the code to paste */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment