WordPress.org

Ready to get started?Download WordPress

Forums

Problem with template_redirect (2 posts)

  1. grosbouff
    Member
    Posted 6 years ago #

    Hello; I have written a plugin that loads a different template if a variable "tpl" is set in the url :

    <?php
    /*
    Plugin Name: Template Changer
    Description: Having a specific template if $_REQUEST['tpl'] is set.
    Author: Gordie (based upon the script by Kaf Oseo)
    Version: 3
    */
    function template_changer() {
    if ($_REQUEST['tpl']) {
    $tpl_file = TEMPLATEPATH . "/" . $_REQUEST['tpl'] . '.php';
    if ( file_exists($tpl_file) ) {
    include($tpl_file);
    }else{
    return;
    }
    }else{
    return;
    }
    }

    if (!is_admin()) {
    add_action('template_redirect', 'template_changer');
    }
    ?>

    The template loads since it doesn't display anything if the content of my tpl (popup.php actually) is
    <?php exit;?>

    But If I put a real template code;
    It does not work :
    For example; If I duplicate the content of the template "single" (just changing the main class; to check if it works); It shows the page twice : once with popup.php and the other with the regular single template !!!

    How can I do ?

  2. Technokinetics
    Member
    Posted 5 years ago #

    This reply's a little late, but it may help others: Once you've loaded your custom template, instead of "return" you need "exit".

    - Tim

Topic Closed

This topic has been closed to new replies.

About this Topic