wcpt_get_template
function does not return a value. It only includes the template, which means that the code runs in the context of the function, where your variables are not defined. It’s not a good way to do things that need global variables.
]]>
As you likely know, variables would stay in scope if you were to simply include another file which uses variables already assigned outside of the included file, assuming the variables are in scope with the include statement. As soon as you offload the include statement to a function, the variables external to the function become out of scope. As Joy implies, you could keep the variables in scope by declaring them global. A generally frowned upon technique, even though WP makes extensive use of them. Heavy reliance on globals is an indication of a poorly thought out project.
When a function is not setup to pass the desired variables, you don’t have many options other than globals. You could build a class to contain static properties which would stay in scope, but doing so isn’t really a lot different than using globals. The class just allows us to say “hey, I never use globals!” ??
If the function returned the needed file reference instead of including it itself, then you could directly include the file from where your variables are still in scope.
]]>Aha, I now fully understand it. Did not realize the include is actually part of a function that’s outside the “scope”. So I should take another look at the structure of the template file of my plugin..
Thanks!
Guido
]]>