Replies: 0
Hi,
I have a page template that calls some template parts using get_template_part(). On each of the template parts I need to get some data from external sources, some of them being the same for different parts. So, I thought that it would be more efficient if I did all the calls once on the main page template, set the variables using set_query_var() and then use those variables on the template parts using get_query_var(), as per the official example.
Since some of my template parts needed the same variables, I thought that it would be tidier if I could gather all my get_query_vars() on one place instead of repeating them on every template part. So, I started experimenting and I noticed the following:
– If I use set_query_var('my_var', $my_data) on the main page template, then I can use $my_var on my template parts all right, getting the correct results. On that case, though, Code Sniffer was complaining about the “Undefined variable $my_var).
– If I go to the template part and define the variable using $my_var = get_query_var('my_var'), then the complain goes away and the output is correct.
– Experimenting a bit more, I tried to gather all my get_query_var() in one place, in order to keep them better organized. So, I noticed that when I put them on the page template, right after all the set_query_var(), Code Sniffer will still complain for undefined variables on the template parts. If I gather them all together in one of the template parts, it doesn’t matter which one, the complain goes away and everything works fine. In fact, it will work if I put my get_query_var() in other php files as well, as long as they aren’t in the page template holding the set_query_var() (e.g., I tried putting it in functions.php and it still worked).
So, my questions are:
1. Why does the Code Sniffer complain when I have the get_query_var() on the same file with the set_query_var() but not when I put it on a template part?
2. Is it a good practice to pass variables like that or should it be avoided for some reason?
3. Should I somehow “unset” the query_var at the end of the template?
My code works fine, I just wanted to better understand how set_query_var() works and which is considered to be the best practice.
Thanks!