Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am creating custom plugin and in my plugin file I have this:

add_filter('page_template', 'load_tq_templates'); 
function load_tq_templates() {
    if (is_page( 'transport-quote-1' )) {
        if ( $overridden_template = locate_template( 'tq-1.php' ) ) {
           // locate_template() returns path to file
           // if either the child theme or the parent theme have overridden the template
           load_template( $overridden_template );
        } else {
           // If neither the child nor parent theme have overridden the template,
           // we load the template from the 'templates' sub-directory of the directory this file is in
           load_template( dirname( __FILE__ ) . '/templates/tq-1.php' );
        }
    }       
}

I've created sub-folder "themes" in my plugin directory where I have file tq-1.php looking like this:

<?php get_header(); ?>
<div class="wrapper">
       <h1>THIS IS MY CUSTOM THEME FOR PAGE tq-1</h1>
    <div id="content">
               <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
               <?php the_content(); ?>
               <?php endwhile; endif; ?>
    </div>          
</div>
<?php get_footer(); ?>

Problem is that the file really get's loaded on appropriate page, BUT after it's loaded the active theme's page.php is loaded as well, so I am basically getting duplicated content on front end as tq-1.php is loaded first and then theme's page.php file is loaded second.

How to avoid theme's page.php from being loaded after my template?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The page_template is a filter, not an action. Specifically it's filtering the file path of the template to be included.

So really you shouldn't be loading anything in that callback (it's too early). Instead just return its file path.

add_filter( 'page_template', 'load_tq_templates' ); 
function load_tq_templates( $template ) {

    if ( is_page( 'transport-quote-1' ) ) {

        if ( $overridden_template = locate_template( 'tq-1.php' ) ) {
            $template = $overridden_template;

        } else {
           // If neither the child nor parent theme have overridden the template,
           // we load the template from the 'templates' sub-directory of the directory this file is in
           $template = dirname( __FILE__ ) . '/templates/tq-1.php';
        }

    }

    return $template;       
}
share|improve this answer
    
Thanks Stephen - I guess I was too tired to note that - thanks for pointing that out ! –  Peter May 9 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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