functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * TextBook functions and definitions.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package TextBook
  8. */
  9. if ( ! function_exists( 'textbook_setup' ) ) :
  10. /*
  11. * Sets up theme defaults and registers support for various WordPress features.
  12. *
  13. * Note that this function is hooked into the after_setup_theme hook, which
  14. * runs before the init hook. The init hook is too late for some features, such
  15. * as indicating support for post thumbnails.
  16. */
  17. function textbook_setup() {
  18. /*
  19. * Make theme available for translation.
  20. * Translations can be filed in the /languages/ directory.
  21. * If you're building a theme based on components, use a find and replace
  22. * to change 'textbook' to the name of your theme in all the template files.
  23. */
  24. load_theme_textdomain( 'textbook', get_template_directory() . '/languages' );
  25. // Add default posts and comments RSS feed links to head.
  26. add_theme_support( 'automatic-feed-links' );
  27. /*
  28. * Let WordPress manage the document title.
  29. * By adding theme support, we declare that this theme does not use a
  30. * hard-coded <title> tag in the document head, and expect WordPress to
  31. * provide it for us.
  32. */
  33. add_theme_support( 'title-tag' );
  34. /**
  35. * Enable support for Post Thumbnails on posts and pages.
  36. *
  37. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  38. */
  39. add_theme_support( 'post-thumbnails' );
  40. // Adds theme-specific images sizes
  41. add_image_size( 'textbook-thumbnail-avatar', 100, 100, true );
  42. add_image_size( 'textbook-featured-content-image', 600, 600, true );
  43. add_image_size( 'textbook-featured-content-image-full', 1148, 600, true );
  44. /**
  45. * This theme uses wp_nav_menu() in two locations.
  46. */
  47. register_nav_menus(
  48. array(
  49. 'header' => __( 'Header', 'textbook' ),
  50. 'footer' => __( 'Footer', 'textbook' ),
  51. )
  52. );
  53. /*
  54. * Switch default core markup for search form, comment form, and comments
  55. * to output valid HTML5.
  56. */
  57. add_theme_support(
  58. 'html5',
  59. array(
  60. 'search-form',
  61. 'comment-form',
  62. 'comment-list',
  63. 'gallery',
  64. 'caption',
  65. )
  66. );
  67. // Add support for responsive embeds.
  68. add_theme_support( 'responsive-embeds' );
  69. // Add support for custom color scheme.
  70. add_theme_support(
  71. 'editor-color-palette',
  72. array(
  73. array(
  74. 'name' => esc_html__( 'Red', 'textbook' ),
  75. 'slug' => 'red',
  76. 'color' => '#ce4639',
  77. ),
  78. array(
  79. 'name' => esc_html__( 'Green', 'textbook' ),
  80. 'slug' => 'green',
  81. 'color' => '#26866d',
  82. ),
  83. array(
  84. 'name' => esc_html__( 'Purple', 'textbook' ),
  85. 'slug' => 'purple',
  86. 'color' => '#8b66d6',
  87. ),
  88. array(
  89. 'name' => esc_html__( 'Dark Gray', 'textbook' ),
  90. 'slug' => 'dark-gray',
  91. 'color' => '#222',
  92. ),
  93. array(
  94. 'name' => esc_html__( 'Medium Gray', 'textbook' ),
  95. 'slug' => 'medium-gray',
  96. 'color' => '#777',
  97. ),
  98. array(
  99. 'name' => esc_html__( 'Light Gray', 'textbook' ),
  100. 'slug' => 'light-gray',
  101. 'color' => '#e1dfdc',
  102. ),
  103. array(
  104. 'name' => esc_html__( 'White', 'textbook' ),
  105. 'slug' => 'white',
  106. 'color' => '#fff',
  107. ),
  108. )
  109. );
  110. }
  111. endif;
  112. add_action( 'after_setup_theme', 'textbook_setup' );
  113. /**
  114. * Set the content width in pixels, based on the theme's design and stylesheet.
  115. *
  116. * Priority 0 to make it available to lower priority callbacks.
  117. *
  118. * @global int $content_width
  119. */
  120. function textbook_content_width() {
  121. $GLOBALS['content_width'] = apply_filters( 'textbook_content_width', 900 );
  122. }
  123. add_action( 'after_setup_theme', 'textbook_content_width', 0 );
  124. /**
  125. * Set theme date format to TextBook suggested default.
  126. */
  127. function textbook_update_date_format() {
  128. update_option( 'date_format', 'j M Y' );
  129. }
  130. add_action( 'after_switch_theme', 'textbook_update_date_format', 0 );
  131. /**
  132. * Register widget area.
  133. *
  134. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  135. */
  136. function textbook_widgets_init() {
  137. register_sidebar(
  138. array(
  139. 'name' => esc_html__( 'Sidebar', 'textbook' ),
  140. 'id' => 'sidebar-1',
  141. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  142. 'after_widget' => '</section>',
  143. 'before_title' => '<h2 class="widget-title">',
  144. 'after_title' => '</h2>',
  145. )
  146. );
  147. register_sidebar(
  148. array(
  149. 'name' => esc_html__( 'Footer Sidebar', 'textbook' ),
  150. 'id' => 'sidebar-2',
  151. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  152. 'after_widget' => '</section>',
  153. 'before_title' => '<h2 class="widget-title">',
  154. 'after_title' => '</h2>',
  155. )
  156. );
  157. }
  158. add_action( 'widgets_init', 'textbook_widgets_init' );
  159. /**
  160. * Output Libre Franklin Pro Google Font URL
  161. *
  162. * @link https://fonts.google.com/specimen/Libre+Franklin/
  163. * @return string
  164. */
  165. function textbook_libre_franklin_url() {
  166. $libre_franklin_font_url = '';
  167. /* translators: If there are characters in your language that are not supported
  168. * by Libre Franklin, translate this to 'off'. Do not translate into your own language.
  169. */
  170. if ( 'off' !== esc_html_x( 'on', 'Libre Franklin font: on or off', 'textbook' ) ) {
  171. $subsets = 'latin,latin-ext';
  172. /* translators: To add an additional Libre Franklin character subset specific to your language,
  173. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  174. */
  175. $subset = esc_html_x( 'no-subset', 'Libre Franklin font: add new subset (greek, cyrillic, vietnamese)', 'textbook' );
  176. if ( 'cyrillic' === $subset ) {
  177. $subsets .= ',cyrillic,cyrillic-ext';
  178. } elseif ( 'greek' === $subset ) {
  179. $subsets .= ',greek,greek-ext';
  180. } elseif ( 'vietnamese' === $subset ) {
  181. $subsets .= ',vietnamese';
  182. }
  183. $libre_franklin_font_url = add_query_arg( 'family', 'Libre Franklin:300,500,600,300italic,600italic', '//fonts.googleapis.com/css' );
  184. $libre_franklin_font_url = add_query_arg( 'subset', $subsets, $libre_franklin_font_url );
  185. }
  186. return $libre_franklin_font_url;
  187. }
  188. /**
  189. * Output Playfair Display Google Font URL
  190. *
  191. * @link https://fonts.google.com/specimen/Playfair+Display+SC/
  192. * @return string
  193. */
  194. function textbook_playfair_display_url() {
  195. $playfair_display_font_url = '';
  196. /* translators: If there are characters in your language that are not supported
  197. * by Playfair Display SC, translate this to 'off'. Do not translate into your own language.
  198. */
  199. if ( 'off' !== esc_html_x( 'on', 'Playfair Display font: on or off', 'textbook' ) ) {
  200. $subsets = 'latin,latin-ext';
  201. /* translators: To add an additional Playfair Display SC character subset specific to your language,
  202. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  203. */
  204. $subset = esc_html_x( 'no-subset', 'Playfair Display font: add new subset (greek, cyrillic, vietnamese)', 'textbook' );
  205. if ( 'cyrillic' === $subset ) {
  206. $subsets .= ',cyrillic,cyrillic-ext';
  207. } elseif ( 'greek' === $subset ) {
  208. $subsets .= ',greek,greek-ext';
  209. } elseif ( 'vietnamese' === $subset ) {
  210. $subsets .= ',vietnamese';
  211. }
  212. $playfair_display_font_url = add_query_arg( 'family', 'Playfair Display:400i', '//fonts.googleapis.com/css' );
  213. $playfair_display_font_url = add_query_arg( 'subset', $subsets, $playfair_display_font_url );
  214. }
  215. return $playfair_display_font_url;
  216. }
  217. /**
  218. * Output Playfair Display Small Caps Google Font URL
  219. *
  220. * @link https://fonts.google.com/specimen/Playfair+Display+SC/
  221. * @return string
  222. */
  223. function textbook_playfair_display_sc_url() {
  224. $playfair_display_sc_font_url = '';
  225. /* translators: If there are characters in your language that are not supported
  226. * by Playfair Display SC, translate this to 'off'. Do not translate into your own language.
  227. */
  228. if ( 'off' !== esc_html_x( 'on', 'Playfair Display SC font: on or off', 'textbook' ) ) {
  229. $subsets = 'latin,latin-ext';
  230. /* translators: To add an additional Playfair Display SC character subset specific to your language,
  231. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  232. */
  233. $subset = esc_html_x( 'no-subset', 'Playfair Display SC font: add new subset (greek, cyrillic, vietnamese)', 'textbook' );
  234. if ( 'cyrillic' === $subset ) {
  235. $subsets .= ',cyrillic,cyrillic-ext';
  236. } elseif ( 'greek' === $subset ) {
  237. $subsets .= ',greek,greek-ext';
  238. } elseif ( 'vietnamese' === $subset ) {
  239. $subsets .= ',vietnamese';
  240. }
  241. $playfair_display_sc_font_url = add_query_arg( 'family', 'Playfair Display SC:700', '//fonts.googleapis.com/css' );
  242. $playfair_display_sc_font_url = add_query_arg( 'subset', $subsets, $playfair_display_sc_font_url );
  243. }
  244. return $playfair_display_sc_font_url;
  245. }
  246. /*
  247. * Enqueue scripts and styles.
  248. */
  249. function textbook_scripts() {
  250. /*
  251. * Styles
  252. */
  253. // Libre Franklin font
  254. wp_register_style( 'textbook-libre-franklin', textbook_libre_franklin_url(), array(), null );
  255. wp_enqueue_style( 'textbook-libre-franklin' );
  256. // Playfair Display font
  257. wp_register_style( 'textbook-playfair-display', textbook_playfair_display_url(), array(), null );
  258. wp_enqueue_style( 'textbook-playfair-display' );
  259. // Playfair Display Small Caps font
  260. wp_register_style( 'textbook-playfair-display-sc', textbook_playfair_display_sc_url(), array(), null );
  261. wp_enqueue_style( 'textbook-playfair-display-sc' );
  262. // Textbook Stylesheet
  263. wp_enqueue_style( 'textbook-style', get_stylesheet_uri() );
  264. // Block stylesheets
  265. wp_enqueue_style( 'textbook-block-style', get_template_directory_uri() . '/assets/css/blocks.css', array( 'textbook-style' ), '20181018' );
  266. /*
  267. * Scripts
  268. */
  269. // Flexslider
  270. wp_enqueue_script( 'textbook-flexslider', get_template_directory_uri() . '/assets/js/jquery.flexslider.js', array( 'jquery' ), '2.6.1', true );
  271. // General functions
  272. wp_enqueue_script( 'textbook-functions', get_template_directory_uri() . '/assets/js/functions.js', array( 'jquery' ), '20160624', true );
  273. // Navigation
  274. wp_enqueue_script( 'textbook-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array( 'jquery' ), '20151215', true );
  275. // Skip link
  276. wp_enqueue_script( 'textbook-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );
  277. // Threaded comments
  278. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  279. wp_enqueue_script( 'comment-reply' );
  280. }
  281. // Menu toggle text
  282. wp_localize_script(
  283. 'textbook-navigation',
  284. 'textBookMenuToggleText',
  285. array(
  286. 'menu' => esc_html__( 'Menu', 'textbook' ),
  287. 'close' => esc_html__( 'Close', 'textbook' ),
  288. )
  289. );
  290. // Expand collapse menu text
  291. wp_localize_script(
  292. 'textbook-navigation',
  293. 'textBookScreenReaderText',
  294. array(
  295. 'expand' => esc_html__( 'expand child menu', 'textbook' ),
  296. 'collapse' => esc_html__( 'collapse child menu', 'textbook' ),
  297. )
  298. );
  299. // Search toggle text
  300. wp_localize_script(
  301. 'textbook-navigation',
  302. 'textBookSearchToggleText',
  303. array(
  304. 'search' => esc_html__( 'Search', 'textbook' ),
  305. 'close' => esc_html__( 'Close', 'textbook' ),
  306. )
  307. );
  308. }
  309. add_action( 'wp_enqueue_scripts', 'textbook_scripts' );
  310. /**
  311. * Enqueue editor styles for Gutenberg
  312. */
  313. function textbook_block_editor_styles() {
  314. // Block styles.
  315. wp_enqueue_style( 'textbook-block-editor-style', get_template_directory_uri() . '/assets/css/editor-blocks.css' );
  316. // Libre Franklin font
  317. wp_register_style( 'textbook-libre-franklin', textbook_libre_franklin_url(), array(), null );
  318. wp_enqueue_style( 'textbook-libre-franklin' );
  319. // Playfair Display font
  320. wp_register_style( 'textbook-playfair-display', textbook_playfair_display_url(), array(), null );
  321. wp_enqueue_style( 'textbook-playfair-display' );
  322. // Playfair Display Small Caps font
  323. wp_register_style( 'textbook-playfair-display-sc', textbook_playfair_display_sc_url(), array(), null );
  324. wp_enqueue_style( 'textbook-playfair-display-sc' );
  325. }
  326. add_action( 'enqueue_block_editor_assets', 'textbook_block_editor_styles' );
  327. /*
  328. * Check whether the browser supports JavaScript.
  329. */
  330. function textbook_html_js_class() {
  331. echo '<script>document.documentElement.className = document.documentElement.className.replace("no-js","js");</script>' . "\n";
  332. }
  333. add_action( 'wp_head', 'textbook_html_js_class', 1 );
  334. /*
  335. * Implement the Custom Header feature.
  336. */
  337. require get_template_directory() . '/inc/custom-header.php';
  338. /*
  339. * Custom template tags for this theme.
  340. */
  341. require get_template_directory() . '/inc/template-tags.php';
  342. /*
  343. * Custom functions that act independently of the theme templates.
  344. */
  345. require get_template_directory() . '/inc/extras.php';
  346. /*
  347. * Customizer additions.
  348. */
  349. require get_template_directory() . '/inc/customizer.php';
  350. /*
  351. * Load Jetpack compatibility file.
  352. */
  353. require get_template_directory() . '/inc/jetpack.php';