function.php 4.8 KB

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