Skip to content

Instantly share code, notes, and snippets.

@alexkingorg
Created May 27, 2012 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexkingorg/2815114 to your computer and use it in GitHub Desktop.
Save alexkingorg/2815114 to your computer and use it in GitHub Desktop.
cfct_template_file() and Globals
<?php
/**
* Include a specific file based on directory and filename
*
* @param string $dir Directory the file will be in
* @param string $file Filename
* @param array $data pass in data to be extracted for use by the template
*
**/
function cfct_template_file($dir, $file, $data = array()) {
// ** WHICH OF THESE LOOKS BEST? **
// option #1 - bring in globals that appear most useful:
global $id, $post, $wp_query, $wpdb, $blog_id, $pagenow, $current_user, $p, $post_parent, $pagename, $page_id, $posts;
// option #2 - extract globals to make them all available (within current scope)
extract($GLOBALS);
// option #3 - bring in all globals
foreach ($GLOBALS as $k => $v) {
global $$k;
}
// ** END OPTIONS **
$path = '';
if (!empty($file)) {
$file = basename($file, '.php');
/* Check for file in the child theme first
var name is deliberately funny. Avoids inadvertantly
overwriting path variable with extract() below. */
$_cfct_filepath = STYLESHEETPATH.'/'.$dir.'/'.$file.'.php';
if (!file_exists($_cfct_filepath)) {
$_cfct_filepath = CFCT_PATH.$dir.'/'.$file.'.php';
}
}
if (file_exists($_cfct_filepath)) {
/* Extract $data as late as possible, so we don't accidentally overwrite
local function vars */
extract($data);
include($_cfct_filepath);
}
else {
cfct_die('Error loading '.$file.' '.__LINE__);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment