PHP code example of hoku / twig-wordpress

1. Go to this page and download the library: Download hoku/twig-wordpress library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hoku / twig-wordpress example snippets


define('TWP___TWIG_ROOT', get_template_directory().'/twig/');
define('TWP___TEMPLATE_PATH', TWP___TWIG_ROOT.'templates/'); // This could be omitted because it's the default

function my_post($post)
{
  $post->content = get_the_content();
}
add_action('the_post', 'my_post');

function my_init($twig, $params)
{
  $twig->addExtension(new Twig_Extension_Debug());
}
add_action('TWP__init', 'my_init', 1, 2);

function my_init($twig, $params)
{
  $params['home'] = get_bloginfo('url');
}
add_action('TWP__init', 'my_init', 1, 2);

function my_environment($environment)
{
  $environment->addFunction('query_posts', new Twig_Function_Function('query_posts'));
}
add_action('TWP__environemnt', 'my_environment');

function my_options($options)
{
  $options['autoescape'] = false;
  return $options;
}
add_filter('TWP__options', 'my_options');

function my_templates_list($templates, $post_type)
{
  if ($post_type == 'page') {
    $templates['my-custom-page-template-without-comment.html.twig'] = __('My Custom Page Template Without Comment');
  }
  return $templates;
}
add_filter('TWP__templates_list', 'my_templates_list', 1, 2);

function my_template($name, $index, $root)
{
  if (date('m/d', time()) == '12/31') {
    return 'happy-new-year.html.twig';
  }
  return $name;
}
add_filter('TWP__template', 'my_template', 1, 3);

function my_404_template($filename)
{
  return 'not-found.html.twig';
}
add_filter('TWP__template_404', 'my_404_template');

function my_taxonomy_template_override($filename)
{
  return 'override/'.$filename;
}
add_filter('TWP__template_taxonomy-override', 'my_taxonomy_template_override');
bash
php composer.phar install