functions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Timber starter-theme
  4. * https://github.com/timber/starter-theme
  5. *
  6. * @package WordPress
  7. * @subpackage Timber
  8. * @since Timber 0.1
  9. */
  10. /**
  11. * If you are installing Timber as a Composer dependency in your theme, you'll need this block
  12. * to load your dependencies and initialize Timber. If you are using Timber via the WordPress.org
  13. * plug-in, you can safely delete this block.
  14. */
  15. $composer_autoload = __DIR__ . '/vendor/autoload.php';
  16. if ( file_exists( $composer_autoload ) ) {
  17. require_once $composer_autoload;
  18. $timber = new Timber\Timber();
  19. }
  20. /**
  21. * This ensures that Timber is loaded and available as a PHP class.
  22. * If not, it gives an error message to help direct developers on where to activate
  23. */
  24. if ( ! class_exists( 'Timber' ) ) {
  25. add_action(
  26. 'admin_notices',
  27. function() {
  28. echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
  29. }
  30. );
  31. add_filter(
  32. 'template_include',
  33. function( $template ) {
  34. return get_stylesheet_directory() . '/static/no-timber.html';
  35. }
  36. );
  37. return;
  38. }
  39. /**
  40. * Sets the directories (inside your theme) to find .twig files
  41. */
  42. Timber::$dirname = array( 'templates', 'views' );
  43. /**
  44. * By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
  45. * No prob! Just set this value to true
  46. */
  47. Timber::$autoescape = false;
  48. /**
  49. * We're going to configure our theme inside of a subclass of Timber\Site
  50. * You can move this to its own file and include here via php's include("MySite.php")
  51. */
  52. class StarterSite extends Timber\Site {
  53. /** Add timber support. */
  54. public function __construct() {
  55. add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
  56. add_filter( 'timber/context', array( $this, 'add_to_context' ) );
  57. add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
  58. add_action( 'init', array( $this, 'register_post_types' ) );
  59. add_action( 'init', array( $this, 'register_taxonomies' ) );
  60. parent::__construct();
  61. }
  62. /** This is where you can register custom post types. */
  63. public function register_post_types() {
  64. }
  65. /** This is where you can register custom taxonomies. */
  66. public function register_taxonomies() {
  67. }
  68. /** This is where you add some context
  69. *
  70. * @param string $context context['this'] Being the Twig's {{ this }}.
  71. */
  72. public function add_to_context( $context ) {
  73. $context['foo'] = 'bar';
  74. $context['stuff'] = 'I am a value set in your functions.php file';
  75. $context['notes'] = 'These values are available everytime you call Timber::context();';
  76. $context['menu'] = new Timber\Menu();
  77. $context['site'] = $this;
  78. return $context;
  79. }
  80. public function theme_supports() {
  81. // Add default posts and comments RSS feed links to head.
  82. add_theme_support( 'automatic-feed-links' );
  83. /*
  84. * Let WordPress manage the document title.
  85. * By adding theme support, we declare that this theme does not use a
  86. * hard-coded <title> tag in the document head, and expect WordPress to
  87. * provide it for us.
  88. */
  89. add_theme_support( 'title-tag' );
  90. /*
  91. * Enable support for Post Thumbnails on posts and pages.
  92. *
  93. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  94. */
  95. add_theme_support( 'post-thumbnails' );
  96. /*
  97. * Switch default core markup for search form, comment form, and comments
  98. * to output valid HTML5.
  99. */
  100. add_theme_support(
  101. 'html5',
  102. array(
  103. 'comment-form',
  104. 'comment-list',
  105. 'gallery',
  106. 'caption',
  107. )
  108. );
  109. /*
  110. * Enable support for Post Formats.
  111. *
  112. * See: https://codex.wordpress.org/Post_Formats
  113. */
  114. add_theme_support(
  115. 'post-formats',
  116. array(
  117. 'aside',
  118. 'image',
  119. 'video',
  120. 'quote',
  121. 'link',
  122. 'gallery',
  123. 'audio',
  124. )
  125. );
  126. add_theme_support( 'menus' );
  127. }
  128. /** This Would return 'foo bar!'.
  129. *
  130. * @param string $text being 'foo', then returned 'foo bar!'.
  131. */
  132. public function myfoo( $text ) {
  133. $text .= ' bar!';
  134. return $text;
  135. }
  136. /** This is where you can add your own functions to twig.
  137. *
  138. * @param string $twig get extension.
  139. */
  140. public function add_to_twig( $twig ) {
  141. $twig->addExtension( new Twig\Extension\StringLoaderExtension() );
  142. $twig->addFilter( new Twig\TwigFilter( 'myfoo', array( $this, 'myfoo' ) ) );
  143. return $twig;
  144. }
  145. }
  146. new StarterSite();