Skip to content
daggerhart edited this page Dec 3, 2011 · 4 revisions

Template Wrangler is my utility system that works similarly to drupal's hook_theme()

Tips & Notes:

  • All keys in the argument array become variables in the template file
  • The Wordpress filter hook is tw_template

Examples

/*
 * Example template architecture
 *
 * @param $templates array Passed from the filter hook from WP
 
          $templates['function_name'] = array(
            'files' => array(
              'first-template-to-look-for.php',
              'second-template-to-look-for.php'
              ),
            'default_path' => '/folder/location/of/default/template',
            'arguments' => array(
              'argument-1' => 'argument-1's default value',
              'argument-2' => 'argument-2's default value',
            )
          )
  
 *  @return array All template arrays filtered so far by Wordpress' filter hook
 */
/*
 * EXAMPLES
 *
// how to include template-wrangler in a plugin
// include Template Wrangler
if(!function_exists('theme')){
  include_once QW_PLUGIN_DIR.'/template-wrangler.inc';
}

// example template hook
function example_template($templates)
{
  // template applied by files
  $templates['example_template'] = array(
    // string of single template suggestion
    // or array of multiple template suggestions
    'files' => array(
        // use argument keys as replacable patterns with the argument's value
        // order is important as the order here is the order inwhich templates are searched for
        'mytemplate-[arg1]-[arg2].php',
        'mytemplate-[arg1].php',
        // a default fall back template name
        'mytemplate.php' 
    ),
    
    // location of the default template if nothing is found
    'default_path' => dirname(__FILE__),
    
    // optional arguments to be passed to the themeing function
    'arguments' => array(
        // must be key => value pairs
        'arg1' => 'arg1_default_value',
        'arg2' => 'arg2_default_value',
    ),
  );
  
  // theme function example
  // since no template files are specified, it will attempt to run a function named theme_link()
  // it will look first for a funtion name THEME_FOLDER_NAME_link()
  // example:  if your active theme is in a folder name my_theme, it will look for my_theme_link()
  $templates['link'] = array(
    'default_path' => dirname(__FILE__),
    'arguments' => array(
      'href' => 'http://google.com',
      'text' => 'your link text',
    )
  );
  
  return $templates;
}
// hook your function into the wordpress filter
add_filter('tw_templates', 'example_template');

// example preprocess function
function example_template_preprocess($template)
{
  // change argument values
  $template['arguments']['arg2'] = '1';
  // add a new argument
  $template['arguments']['new_arg'] = 'brand new argument';
  
  // add a new template suggestions as the first suggestion
  array_unshift($template['files'], 'mytemplate-new-first-suggestion.php');
  // add a new template suggestion as the last suggestion
  $template['files'][] = 'mytemplate-new-final-suggestion.php';
  
  // return the modified template
  return $template;
}

// example theme function
function theme_link($href, $text){
  return "<a href='$href'>$text</a>";
}
Clone this wiki locally