functions.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // REMOVE EDITOR
  49. function remove_editor() {
  50. remove_post_type_support(
  51. 'page', 'editor'
  52. );
  53. remove_post_type_support(
  54. 'post', 'editor'
  55. );
  56. };
  57. add_action('admin_init', 'remove_editor');
  58. /**
  59. * We're going to configure our theme inside of a subclass of Timber\Site
  60. * You can move this to its own file and include here via php's include("MySite.php")
  61. */
  62. class StarterSite extends Timber\Site {
  63. /** Add timber support. */
  64. public function __construct() {
  65. add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
  66. add_filter( 'timber/context', array( $this, 'add_to_context' ) );
  67. add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
  68. add_action( 'init', array( $this, 'register_post_types' ) );
  69. add_action( 'init', array( $this, 'register_taxonomies' ) );
  70. parent::__construct();
  71. }
  72. /** This is where you can register custom post types. */
  73. public function register_post_types() {
  74. }
  75. /** This is where you can register custom taxonomies. */
  76. public function register_taxonomies() {
  77. }
  78. /** This is where you add some context
  79. *
  80. * @param string $context context['this'] Being the Twig's {{ this }}.
  81. */
  82. public function add_to_context( $context ) {
  83. $context['foo'] = 'bar';
  84. $context['stuff'] = 'I am a value set in your functions.php file';
  85. $context['notes'] = 'These values are available everytime you call Timber::context();';
  86. $context['menu'] = new Timber\Menu();
  87. $context['site'] = $this;
  88. return $context;
  89. }
  90. public function theme_supports() {
  91. // Add default posts and comments RSS feed links to head.
  92. add_theme_support( 'automatic-feed-links' );
  93. /*
  94. * Let WordPress manage the document title.
  95. * By adding theme support, we declare that this theme does not use a
  96. * hard-coded <title> tag in the document head, and expect WordPress to
  97. * provide it for us.
  98. */
  99. add_theme_support( 'title-tag' );
  100. /*
  101. * Enable support for Post Thumbnails on posts and pages.
  102. *
  103. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  104. */
  105. add_theme_support( 'post-thumbnails' );
  106. /*
  107. * Switch default core markup for search form, comment form, and comments
  108. * to output valid HTML5.
  109. */
  110. add_theme_support(
  111. 'html5',
  112. array(
  113. 'comment-form',
  114. 'comment-list',
  115. 'gallery',
  116. 'caption',
  117. )
  118. );
  119. /*
  120. * Enable support for Post Formats.
  121. *
  122. * See: https://codex.wordpress.org/Post_Formats
  123. */
  124. add_theme_support(
  125. 'post-formats',
  126. array(
  127. 'aside',
  128. 'image',
  129. 'video',
  130. 'quote',
  131. 'link',
  132. 'gallery',
  133. 'audio',
  134. )
  135. );
  136. add_theme_support( 'menus' );
  137. }
  138. /** This Would return 'foo bar!'.
  139. *
  140. * @param string $text being 'foo', then returned 'foo bar!'.
  141. */
  142. public function myfoo( $text ) {
  143. $text .= ' bar!';
  144. return $text;
  145. }
  146. /** This is where you can add your own functions to twig.
  147. *
  148. * @param string $twig get extension.
  149. */
  150. public function add_to_twig( $twig ) {
  151. $twig->addExtension( new Twig\Extension\StringLoaderExtension() );
  152. $twig->addFilter( new Twig\TwigFilter( 'myfoo', array( $this, 'myfoo' ) ) );
  153. return $twig;
  154. }
  155. }
  156. new StarterSite();