Skip to content

Instantly share code, notes, and snippets.

@bigdawggi
Last active March 29, 2016 20:08
Show Gist options
  • Save bigdawggi/6b1b6c332a00dee799dcc5fc653bf481 to your computer and use it in GitHub Desktop.
Save bigdawggi/6b1b6c332a00dee799dcc5fc653bf481 to your computer and use it in GitHub Desktop.
Helper function for loading a template in WP with some arbitrary data passed to it
<?php
/**
* Allows the passing of variables to a template file when loaded. Should help
* to remove the need for unnecessarily putting data into the $_GLOBAL scope.
*
* Usage:
* // This example will expose two variables to the loaded template:
* // $type = 'Foo', and $url = '//example.com/test.jpg'
* tmbr_load_template( 'partials/map.php', array(
* 'type' => 'Foo',
* 'url' => '//example.com/test.jpg'
* ) );
*
* @param string $filename Name of the file that you would typically pass to `get_template_part()` **WITH THE EXTENSION**
* @param array $data array of information to be utilized in the template
* @return null This echos out the template, does not return it
*/
function tmbr_load_template( $filename, $data = array() ) {
$file = locate_template( $filename );
if( $file ){
extract( $data );
include( $file );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment