Skip to content

Instantly share code, notes, and snippets.

@tlehtimaki
Created January 4, 2018 08:15
Show Gist options
  • Save tlehtimaki/7b24a7d921f616c7ffef93164447bfef to your computer and use it in GitHub Desktop.
Save tlehtimaki/7b24a7d921f616c7ffef93164447bfef to your computer and use it in GitHub Desktop.
Pass variables to template in WordPress
<?php
public static function get_template($template, $variables = array()) {
// Pass variables as a key-value array: array('varname' => 'varvalue')
// and conditionally check for them in templates like this:
// $varname = !empty($varname) ? $varname : NULL
foreach ($variables as $key => $value) {
// Create variables from parameters
${$key} = $value;
}
require(locate_template($template));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment