functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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( array(
  48. 'header' => __( 'Header', 'textbook' ),
  49. 'footer' => __( 'Footer', 'textbook' ),
  50. ) );
  51. /*
  52. * Switch default core markup for search form, comment form, and comments
  53. * to output valid HTML5.
  54. */
  55. add_theme_support( 'html5', array(
  56. 'search-form',
  57. 'comment-form',
  58. 'comment-list',
  59. 'gallery',
  60. 'caption',
  61. ) );
  62. // Add support for responsive embeds.
  63. add_theme_support( 'responsive-embeds' );
  64. // Add support for custom color scheme.
  65. add_theme_support( 'editor-color-palette', array(
  66. array(
  67. 'name' => esc_html__( 'Red', 'textbook' ),
  68. 'slug' => 'red',
  69. 'color' => '#ce4639',
  70. ),
  71. array(
  72. 'name' => esc_html__( 'Green', 'textbook' ),
  73. 'slug' => 'green',
  74. 'color' => '#26866d',
  75. ),
  76. array(
  77. 'name' => esc_html__( 'Purple', 'textbook' ),
  78. 'slug' => 'purple',
  79. 'color' => '#8b66d6',
  80. ),
  81. array(
  82. 'name' => esc_html__( 'Dark Gray', 'textbook' ),
  83. 'slug' => 'dark-gray',
  84. 'color' => '#222',
  85. ),
  86. array(
  87. 'name' => esc_html__( 'Medium Gray', 'textbook' ),
  88. 'slug' => 'medium-gray',
  89. 'color' => '#777',
  90. ),
  91. array(
  92. 'name' => esc_html__( 'Light Gray', 'textbook' ),
  93. 'slug' => 'light-gray',
  94. 'color' => '#e1dfdc',
  95. ),
  96. array(
  97. 'name' => esc_html__( 'White', 'textbook' ),
  98. 'slug' => 'white',
  99. 'color' => '#fff',
  100. ),
  101. ) );
  102. }
  103. endif;
  104. add_action( 'after_setup_theme', 'textbook_setup' );
  105. /**
  106. * Set the content width in pixels, based on the theme's design and stylesheet.
  107. *
  108. * Priority 0 to make it available to lower priority callbacks.
  109. *
  110. * @global int $content_width
  111. */
  112. function textbook_content_width() {
  113. $GLOBALS['content_width'] = apply_filters( 'textbook_content_width', 900 );
  114. }
  115. add_action( 'after_setup_theme', 'textbook_content_width', 0 );
  116. /**
  117. * Set theme date format to TextBook suggested default.
  118. */
  119. function textbook_update_date_format() {
  120. update_option( 'date_format', 'j M Y' );
  121. }
  122. add_action( 'after_switch_theme', 'textbook_update_date_format', 0 );
  123. /**
  124. * Register widget area.
  125. *
  126. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  127. */
  128. function textbook_widgets_init() {
  129. register_sidebar( array(
  130. 'name' => esc_html__( 'Sidebar', 'textbook' ),
  131. 'id' => 'sidebar-1',
  132. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  133. 'after_widget' => '</section>',
  134. 'before_title' => '<h2 class="widget-title">',
  135. 'after_title' => '</h2>',
  136. ) );
  137. register_sidebar( array(
  138. 'name' => esc_html__( 'Footer Sidebar', 'textbook' ),
  139. 'id' => 'sidebar-2',
  140. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  141. 'after_widget' => '</section>',
  142. 'before_title' => '<h2 class="widget-title">',
  143. 'after_title' => '</h2>',
  144. ) );
  145. }
  146. add_action( 'widgets_init', 'textbook_widgets_init' );
  147. /**
  148. * Output Libre Franklin Pro Google Font URL
  149. *
  150. * @link https://fonts.google.com/specimen/Libre+Franklin/
  151. * @return string
  152. */
  153. function textbook_libre_franklin_url() {
  154. $libre_franklin_font_url = '';
  155. /* translators: If there are characters in your language that are not supported
  156. * by Libre Franklin, translate this to 'off'. Do not translate into your own language.
  157. */
  158. if ( 'off' !== esc_html_x( 'on', 'Libre Franklin font: on or off', 'textbook' ) ) {
  159. $subsets = 'latin,latin-ext';
  160. /* translators: To add an additional Libre Franklin character subset specific to your language,
  161. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  162. */
  163. $subset = esc_html_x( 'no-subset', 'Libre Franklin font: add new subset (greek, cyrillic, vietnamese)', 'textbook' );
  164. if ( 'cyrillic' === $subset ) {
  165. $subsets .= ',cyrillic,cyrillic-ext';
  166. } elseif ( 'greek' === $subset ) {
  167. $subsets .= ',greek,greek-ext';
  168. } elseif ( 'vietnamese' === $subset ) {
  169. $subsets .= ',vietnamese';
  170. }
  171. $libre_franklin_font_url = add_query_arg( 'family', 'Libre Franklin:300,500,600,300italic,600italic', '//fonts.googleapis.com/css' );
  172. $libre_franklin_font_url = add_query_arg( 'subset', $subsets, $libre_franklin_font_url );
  173. }
  174. return $libre_franklin_font_url;
  175. }
  176. /**
  177. * Output Playfair Display Google Font URL
  178. *
  179. * @link https://fonts.google.com/specimen/Playfair+Display+SC/
  180. * @return string
  181. */
  182. function textbook_playfair_display_url() {
  183. $playfair_display_font_url = '';
  184. /* translators: If there are characters in your language that are not supported
  185. * by Playfair Display SC, translate this to 'off'. Do not translate into your own language.
  186. */
  187. if ( 'off' !== esc_html_x( 'on', 'Playfair Display font: on or off', 'textbook' ) ) {
  188. $subsets = 'latin,latin-ext';
  189. /* translators: To add an additional Playfair Display SC character subset specific to your language,
  190. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  191. */
  192. $subset = esc_html_x( 'no-subset', 'Playfair Display font: add new subset (greek, cyrillic, vietnamese)', 'textbook' );
  193. if ( 'cyrillic' === $subset ) {
  194. $subsets .= ',cyrillic,cyrillic-ext';
  195. } elseif ( 'greek' === $subset ) {
  196. $subsets .= ',greek,greek-ext';
  197. } elseif ( 'vietnamese' === $subset ) {
  198. $subsets .= ',vietnamese';
  199. }
  200. $playfair_display_font_url = add_query_arg( 'family', 'Playfair Display:400i', '//fonts.googleapis.com/css' );
  201. $playfair_display_font_url = add_query_arg( 'subset', $subsets, $playfair_display_font_url );
  202. }
  203. return $playfair_display_font_url;
  204. }
  205. /**
  206. * Output Playfair Display Small Caps Google Font URL
  207. *
  208. * @link https://fonts.google.com/specimen/Playfair+Display+SC/
  209. * @return string
  210. */
  211. function textbook_playfair_display_sc_url() {
  212. $playfair_display_sc_font_url = '';
  213. /* translators: If there are characters in your language that are not supported
  214. * by Playfair Display SC, translate this to 'off'. Do not translate into your own language.
  215. */
  216. if ( 'off' !== esc_html_x( 'on', 'Playfair Display SC font: on or off', 'textbook' ) ) {
  217. $subsets = 'latin,latin-ext';
  218. /* translators: To add an additional Playfair Display SC character subset specific to your language,
  219. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  220. */
  221. $subset = esc_html_x( 'no-subset', 'Playfair Display SC font: add new subset (greek, cyrillic, vietnamese)', 'textbook' );
  222. if ( 'cyrillic' === $subset ) {
  223. $subsets .= ',cyrillic,cyrillic-ext';
  224. } elseif ( 'greek' === $subset ) {
  225. $subsets .= ',greek,greek-ext';
  226. } elseif ( 'vietnamese' === $subset ) {
  227. $subsets .= ',vietnamese';
  228. }
  229. $playfair_display_sc_font_url = add_query_arg( 'family', 'Playfair Display SC:700', '//fonts.googleapis.com/css' );
  230. $playfair_display_sc_font_url = add_query_arg( 'subset', $subsets, $playfair_display_sc_font_url );
  231. }
  232. return $playfair_display_sc_font_url;
  233. }
  234. /*
  235. * Enqueue scripts and styles.
  236. */
  237. function textbook_scripts() {
  238. /*
  239. * Styles
  240. */
  241. // Libre Franklin font
  242. wp_register_style( 'textbook-libre-franklin', textbook_libre_franklin_url(), array(), null );
  243. wp_enqueue_style( 'textbook-libre-franklin' );
  244. // Playfair Display font
  245. wp_register_style( 'textbook-playfair-display', textbook_playfair_display_url(), array(), null );
  246. wp_enqueue_style( 'textbook-playfair-display' );
  247. // Playfair Display Small Caps font
  248. wp_register_style( 'textbook-playfair-display-sc', textbook_playfair_display_sc_url(), array(), null );
  249. wp_enqueue_style( 'textbook-playfair-display-sc' );
  250. // Textbook Stylesheet
  251. wp_enqueue_style( 'textbook-style', get_stylesheet_uri() );
  252. // Block stylesheets
  253. wp_enqueue_style( 'textbook-block-style', get_template_directory_uri() . '/assets/css/blocks.css', array( 'textbook-style' ), '20181018' );
  254. /*
  255. * Scripts
  256. */
  257. // Flexslider
  258. wp_enqueue_script( 'textbook-flexslider', get_template_directory_uri() . '/assets/js/jquery.flexslider.js', array( 'jquery' ), '2.6.1', true );
  259. // General functions
  260. wp_enqueue_script( 'textbook-functions', get_template_directory_uri() . '/assets/js/functions.js', array( 'jquery' ), '20160624', true );
  261. // Navigation
  262. wp_enqueue_script( 'textbook-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array( 'jquery' ), '20151215', true );
  263. // Skip link
  264. wp_enqueue_script( 'textbook-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );
  265. // Threaded comments
  266. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  267. wp_enqueue_script( 'comment-reply' );
  268. }
  269. // Menu toggle text
  270. wp_localize_script( 'textbook-navigation', 'textBookMenuToggleText', array(
  271. 'menu' => esc_html__( 'Menu', 'textbook' ),
  272. 'close' => esc_html__( 'Close', 'textbook' ),
  273. ) );
  274. // Expand collapse menu text
  275. wp_localize_script( 'textbook-navigation', 'textBookScreenReaderText', array(
  276. 'expand' => esc_html__( 'expand child menu', 'textbook' ),
  277. 'collapse' => esc_html__( 'collapse child menu', 'textbook' ),
  278. ) );
  279. // Search toggle text
  280. wp_localize_script( 'textbook-navigation', 'textBookSearchToggleText', array(
  281. 'search' => esc_html__( 'Search', 'textbook' ),
  282. 'close' => esc_html__( 'Close', 'textbook' )
  283. ) );
  284. }
  285. add_action( 'wp_enqueue_scripts', 'textbook_scripts' );
  286. /**
  287. * Enqueue editor styles for Gutenberg
  288. */
  289. function textbook_block_editor_styles() {
  290. // Block styles.
  291. wp_enqueue_style( 'textbook-block-editor-style', get_template_directory_uri() . '/assets/css/editor-blocks.css' );
  292. // Libre Franklin font
  293. wp_register_style( 'textbook-libre-franklin', textbook_libre_franklin_url(), array(), null );
  294. wp_enqueue_style( 'textbook-libre-franklin' );
  295. // Playfair Display font
  296. wp_register_style( 'textbook-playfair-display', textbook_playfair_display_url(), array(), null );
  297. wp_enqueue_style( 'textbook-playfair-display' );
  298. // Playfair Display Small Caps font
  299. wp_register_style( 'textbook-playfair-display-sc', textbook_playfair_display_sc_url(), array(), null );
  300. wp_enqueue_style( 'textbook-playfair-display-sc' );
  301. }
  302. add_action( 'enqueue_block_editor_assets', 'textbook_block_editor_styles' );
  303. /*
  304. * Check whether the browser supports JavaScript.
  305. */
  306. function textbook_html_js_class () {
  307. echo '<script>document.documentElement.className = document.documentElement.className.replace("no-js","js");</script>'. "\n";
  308. }
  309. add_action( 'wp_head', 'textbook_html_js_class', 1 );
  310. /*
  311. * Implement the Custom Header feature.
  312. */
  313. require get_template_directory() . '/inc/custom-header.php';
  314. /*
  315. * Custom template tags for this theme.
  316. */
  317. require get_template_directory() . '/inc/template-tags.php';
  318. /*
  319. * Custom functions that act independently of the theme templates.
  320. */
  321. require get_template_directory() . '/inc/extras.php';
  322. /*
  323. * Customizer additions.
  324. */
  325. require get_template_directory() . '/inc/customizer.php';
  326. /*
  327. * Load Jetpack compatibility file.
  328. */
  329. require get_template_directory() . '/inc/jetpack.php';