0

I am using a query argument detail to check if a custom template should be loaded. This works fine, but I also need to add some variables that can be accessed from within the template. Here I am using add_action('template_include' array($this, 'templateInclude'):

public function templateInclude($template)
{
    if (get_query_var('detail', false) !== false) {
        // Check theme directory first.
        $newTemplate = locate_template(array('detail.php'));
        if ($newTemplate != '')
            return $newTemplate;

        // Check plugin directory next.
        $newTemplate = plugin_dir_path(__FILE__) . 'templates/detail.php';
        if (file_exists($newTemplate)) {
            return $newTemplate;
        }
    }

    // Return default template.
    return $template;
}

The above works, but of course I am not able to actually set any variables for the template.

I tried using set_query_var() to set a variable that could be accessed using get_query_var(), but this does not really seem to be the best solution.

I also tried using add_action('template_redirect' array($this, 'templateRedirect'), which works but this neither seems to be a really good solution:

public function templateRedirect() {
    if (get_query_var('detail')) {
        $detailId = get_query_var('detail', null);
        // Set a variable 'data' that can be used inside the template
        $data = get_detail($detailId);
        include plugin_dir_path(__FILE__) . 'templates/detail.php';
        exit;
    }
}

Would be really great if someone has some nice way of adding custom variables for a specific template.

  • Sounds like you need global variables that are access from multiple places. I would instantiate a singleton class with static functions to get and set variables. – Jeffrey von Grumbkow Dec 28 '18 at 14:32
  • I only need to access the variable inside the template file. So the question is how to pass a variable to the actual template. If I included the file using a normal require_once then I could achieve this by doing $data = get_detail($detailId); require_once 'template.php'; – Cyclonecode Dec 28 '18 at 20:28
  • Jeffrey von Grumbkow's answer still applies. There are multiple places (1) the plugin setting the variables, and (2) your template file. carlalexander.ca/singletons-in-wordpress – tmdesigned Jan 13 at 12:17
  • @the_hobbes - use a singleton to setup global variables does not seem to be the best way of achieving this, especially since the variables just needs to be available and accessed in the custom template? – Cyclonecode Jan 13 at 12:30
  • :shrug: You have two places that need access to something. Either reduce that (i.e. let your template file figure out how to get the data itself) or give them a shared resource. FWIW a singleton is not creating global variables per se. It's a shared resource class that both places can get to. – tmdesigned Jan 13 at 12:47

Your Answer

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

Browse other questions tagged or ask your own question.