1

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.

  • got solution, i need to use template_include instead of template_redirect – user1983017 Jul 7 '13 at 4:18
2

template_redirect runs before template_include. You can demonstrate that with this:

add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
  echo __FUNCTION__;
}
// use template redirect to include file
add_action('template_redirect', 'ra_template_block');
function ra_template_block() {
  echo __FUNCTION__;  
}

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 exits in template_redirect the global that get_current_template depends upon never gets set, and even if you didn't exit 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 that global.

// current template file
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
  var_dump($t);
  $GLOBALS['current_theme_template'] = basename($t);
  return $t;
}

// use template redirect to include file
add_action('template_include', 'ra_template_block', 1001);
function ra_template_block() {
//     include_once THEME_DIR.'/blockstop.php';
    var_dump(get_template_part(get_current_template()));      
//     include_once THEME_DIR.'/blocks.php';
    exit;
}

Of course, there is no real reason for two filters. One would do just as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.