I would like to include two files before and after current template.
I am using template_redirect to do this,
But when I call get_current_template()
inside template_redirect
it returns nothing.
is there a way to solve this ??
here is my code:
// current template file
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
function get_current_template() {
if( !isset( $GLOBALS['current_theme_template'] ) )
return false;
if( $echo )
echo $GLOBALS['current_theme_template'];
else
return $GLOBALS['current_theme_template'];
}
// use template redirect to include file
add_action('template_redirect', 'ra_template_block');
function ra_template_block() {
include_once THEME_DIR.'/blockstop.php';
get_template_part(get_current_template());
include_once THEME_DIR.'/blocks.php';
exit;
}
Way I am doing this
I want to set page layout from a file. I need to include below tags every time in my template file:
get_header()
get_footer()
get_sidebar()
get_template_part('right')
get_template_part('left')
So if I can set them from a specific file, then I dont need to include them in every template file.
template_redirect
runs beforetemplate_include
. You can demonstrate that with this:Or you could just look at the source: http://core.trac.wordpress.org/browser/tags/3.5.2/wp-includes/template-loader.php
Since your code
exit
s intemplate_redirect
theglobal
thatget_current_template
depends upon never gets set, and even if you didn’texit
it would be set too late.You need the template path passed into
template_include
so if you just hook both filters to that hook and give them a priority so that the one always loads before the other this should work, at least insofar as setting and retrieving thatglobal
.Of course, there is no real reason for two filters. One would do just as well.