How to pass variables with get_template_part?

I am developing a custom theme and i like to pass some variables to selected files. I am calling the view files from functions.php.

$var1 = ;
$var2 = ;
etc
include_once('form-views/view-profile.php');//works

//get_template_part('includes/form-views/view','profile');//doesn't work

Now with include it works

3 Answers
3

This is essentially scope visibility issue. include brings code into a current scope, function call creates new closed off scope. In get_template_part() only certain WordPress globals are being made available by load_template() call inside.

While the basic answer is to declare your variables as globals, you might want to ponder your overall architecture a bit — this is typically not a good sign in code.

Leave a Comment