Skip to content

Instantly share code, notes, and snippets.

@getdave
Last active August 29, 2015 14:27
Show Gist options
  • Save getdave/4506ff9992ec7809afbb to your computer and use it in GitHub Desktop.
Save getdave/4506ff9992ec7809afbb to your computer and use it in GitHub Desktop.
A useful utility function for WordPress projects which loads a given template using output buffering optionally including data to be passed into template
/**
* Output Buffered Load Template Part
* loads a given template part using output buffering
* optionally including $data to be passed into template
*/
function ob_load_template_part( $template_name, $data ) {
// Optionally provided an assoc array of data to pass to tempalte
// and it will be extracted into variables
if ( is_array( $data ) ) {
extract($data);
}
ob_start();
include( locate_template( $template_name ) );
$var = ob_get_contents();
ob_end_clean();
return $var;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment