• peterbra

    (@peterbra)


    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?

    What I am doing wrong ?

The topic ‘Custom Plugin -> theme filter’ is closed to new replies.