functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * Libretto functions and definitions
  4. *
  5. * @package Libretto
  6. */
  7. /**
  8. * Set the content width based on the theme's design and stylesheet.
  9. */
  10. if ( ! isset( $content_width ) ) {
  11. $content_width = 720; /* 680px wide plus a 40px buffer */
  12. }
  13. if ( ! function_exists( 'libretto_setup' ) ) :
  14. /**
  15. * Sets up theme defaults and registers support for various WordPress features.
  16. *
  17. * Note that this function is hooked into the after_setup_theme hook, which runs
  18. * before the init hook. The init hook is too late for some features, such as indicating
  19. * support post thumbnails.
  20. */
  21. function libretto_setup() {
  22. /**
  23. * Make theme available for translation
  24. * Translations can be filed in the /languages/ directory
  25. * If you're building a theme based on Libretto, use a find and replace
  26. * to change 'libretto' to the name of your theme in all the template files
  27. */
  28. load_theme_textdomain( 'libretto', get_template_directory() . '/languages' );
  29. /**
  30. * Add default posts and comments RSS feed links to head
  31. */
  32. add_theme_support( 'automatic-feed-links' );
  33. /**
  34. * Enable support for Post Thumbnails
  35. */
  36. add_theme_support( 'post-thumbnails' );
  37. /**
  38. * Add custom image sizes for:
  39. * 1. The site logo
  40. * 2. Large images that overlap the content area a smidge
  41. * 3. Full-page images (primarily used for featured images)
  42. */
  43. add_image_size( 'libretto-logo', 200, 200, false );
  44. add_image_size( 'libretto-oversized', 900, 600, false );
  45. add_image_size( 'libretto-fullpage', 1600, 1000, false );
  46. // Add support for responsive embeds.
  47. add_theme_support( 'responsive-embeds' );
  48. // Add custom colors to Gutenberg
  49. add_theme_support(
  50. 'editor-color-palette',
  51. array(
  52. array(
  53. 'name' => esc_html__( 'Black', 'libretto' ),
  54. 'slug' => 'black',
  55. 'color' => '#26231e',
  56. ),
  57. array(
  58. 'name' => esc_html__( 'Dark Gray', 'libretto' ),
  59. 'slug' => 'dark-gray',
  60. 'color' => '#787065',
  61. ),
  62. array(
  63. 'name' => esc_html__( 'Medium Gray', 'libretto' ),
  64. 'slug' => 'medium-gray',
  65. 'color' => '#a09a92',
  66. ),
  67. array(
  68. 'name' => esc_html__( 'Light Gray', 'libretto' ),
  69. 'slug' => 'light-gray',
  70. 'color' => '#d9d6d0',
  71. ),
  72. array(
  73. 'name' => esc_html__( 'White', 'libretto' ),
  74. 'slug' => 'white',
  75. 'color' => '#ffffff',
  76. ),
  77. array(
  78. 'name' => esc_html__( 'Red', 'libretto' ),
  79. 'slug' => 'red',
  80. 'color' => '#932817',
  81. ),
  82. array(
  83. 'name' => esc_html__( 'Dark Red', 'libretto' ),
  84. 'slug' => 'dark-red',
  85. 'color' => '#712012',
  86. ),
  87. )
  88. );
  89. /**
  90. * This theme uses wp_nav_menu() in two locations:
  91. * one at the top of the page, and one for social media links in the footer
  92. */
  93. register_nav_menus(
  94. array(
  95. 'primary' => __( 'Primary Menu', 'libretto' ),
  96. 'social' => __( 'Social Media Menu', 'libretto' ),
  97. )
  98. );
  99. /**
  100. * Enable support for Post Formats
  101. */
  102. add_theme_support(
  103. 'post-formats',
  104. array(
  105. 'aside',
  106. 'image',
  107. 'video',
  108. 'quote',
  109. 'link',
  110. 'audio',
  111. 'chat',
  112. 'gallery',
  113. 'status',
  114. )
  115. );
  116. /**
  117. * Enable support for custom background
  118. */
  119. add_theme_support(
  120. 'custom-background',
  121. array(
  122. 'default-color' => '#f2f1ed',
  123. )
  124. );
  125. /**
  126. * Enable support for proper titles
  127. */
  128. add_theme_support( 'title-tag' );
  129. /**
  130. * Use editor admin styles.
  131. */
  132. add_editor_style( array( 'css/editor-style.css', libretto_fonts_url() ) );
  133. /**
  134. * Switch default core markup for search form, comment form, and comments
  135. * to output valid HTML5.
  136. */
  137. add_theme_support(
  138. 'html5',
  139. array(
  140. 'search-form',
  141. 'comment-form',
  142. 'comment-list',
  143. 'gallery',
  144. 'caption',
  145. )
  146. );
  147. } // libretto_setup()
  148. endif;
  149. add_action( 'after_setup_theme', 'libretto_setup' );
  150. /**
  151. * Register widgetized area and update sidebar with default widgets
  152. */
  153. function libretto_widgets_init() {
  154. register_sidebar(
  155. array(
  156. 'name' => __( 'First Footer Sidebar', 'libretto' ),
  157. 'id' => 'sidebar-1',
  158. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  159. 'after_widget' => '</aside>',
  160. 'before_title' => '<h2 class="widget-title">',
  161. 'after_title' => '</h2>',
  162. )
  163. );
  164. register_sidebar(
  165. array(
  166. 'name' => __( 'Second Footer Sidebar', 'libretto' ),
  167. 'id' => 'sidebar-2',
  168. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  169. 'after_widget' => '</aside>',
  170. 'before_title' => '<h2 class="widget-title">',
  171. 'after_title' => '</h2>',
  172. )
  173. );
  174. register_sidebar(
  175. array(
  176. 'name' => __( 'Third Footer Sidebar', 'libretto' ),
  177. 'id' => 'sidebar-3',
  178. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  179. 'after_widget' => '</aside>',
  180. 'before_title' => '<h2 class="widget-title">',
  181. 'after_title' => '</h2>',
  182. )
  183. );
  184. register_sidebar(
  185. array(
  186. 'name' => __( 'Fourth Footer Sidebar', 'libretto' ),
  187. 'id' => 'sidebar-4',
  188. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  189. 'after_widget' => '</aside>',
  190. 'before_title' => '<h2 class="widget-title">',
  191. 'after_title' => '</h2>',
  192. )
  193. );
  194. }
  195. add_action( 'widgets_init', 'libretto_widgets_init' );
  196. /**
  197. * Generate URLs for our custom fonts (via Google)
  198. */
  199. function libretto_fonts_url() {
  200. $fonts_url = '';
  201. /* Translators: If there are characters in your language that are not
  202. * supported by Libre Baskerville, translate this to 'off'. Do not translate
  203. * into your own language.
  204. */
  205. $libre_baskerville = _x( 'on', 'Libre Baskerville font: on or off', 'libretto' );
  206. /* Translators: If there are characters in your language that are not
  207. * supported by Montserrat, translate this to 'off'. Do not translate
  208. * into your own language.
  209. */
  210. $montserrat = _x( 'on', 'Montserrat font: on or off', 'libretto' );
  211. /* Translators: If there are characters in your language that are not
  212. * supported by Playfair Display, translate this to 'off'. Do not translate
  213. * into your own language.
  214. */
  215. $playfair_display = _x( 'on', 'Playfair Display font: on or off', 'libretto' );
  216. /* Translators: If there are characters in your language that are not
  217. * supported by Droid Mono, translate this to 'off'. Do not translate
  218. * into your own language.
  219. */
  220. $droid_mono = _x( 'on', 'Droid Sans Mono font: on or off', 'libretto' );
  221. if ( 'off' !== $libre_baskerville || 'off' !== $montserrat || 'off' !== $playfair_display || 'off' !== $droid_mono ) :
  222. $font_families = array();
  223. if ( 'off' !== $libre_baskerville ) {
  224. $font_families[] = 'Libre Baskerville:400,700,400italic';
  225. }
  226. if ( 'off' !== $playfair_display ) {
  227. $font_families[] = 'Playfair Display:400,700,400italic,700italic';
  228. $font_families[] = 'Playfair Display SC:700,700italic';
  229. }
  230. if ( 'off' !== $montserrat ) {
  231. $font_families[] = 'Montserrat:400';
  232. }
  233. if ( 'off' !== $droid_mono ) {
  234. $font_families[] = 'Droid Sans Mono:400';
  235. }
  236. $query_args = array(
  237. 'family' => urlencode( implode( '|', $font_families ) ),
  238. 'subset' => urlencode( 'latin,latin-ext' ),
  239. );
  240. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  241. endif;
  242. return $fonts_url;
  243. }
  244. /**
  245. * Enqueue scripts and styles
  246. */
  247. function libretto_scripts() {
  248. // General site stylesheet & JS
  249. wp_enqueue_style( 'libretto-style', get_stylesheet_uri() );
  250. wp_enqueue_script( 'libretto-script', get_template_directory_uri() . '/js/libretto.js', array( 'jquery' ), '20140331' );
  251. // Gutenberg styles
  252. wp_enqueue_style( 'libretto-blocks', get_template_directory_uri() . '/css/blocks.css' );
  253. // Fonts
  254. wp_enqueue_style( 'libretto-fonts', libretto_fonts_url(), array(), null );
  255. wp_enqueue_style( 'libretto-custom-icons', get_template_directory_uri() . '/icons/icons.css', array(), null );
  256. // Navigation
  257. wp_enqueue_script( 'libretto-touche', get_template_directory_uri() . '/js/touche.js', '20150604', true );
  258. wp_enqueue_script( 'libretto-navigation', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), '20150115', true );
  259. wp_enqueue_script( 'libretto-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  260. // Comments
  261. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  262. wp_enqueue_script( 'comment-reply' );
  263. }
  264. }
  265. add_action( 'wp_enqueue_scripts', 'libretto_scripts' );
  266. /**
  267. * Gutenberg Editor Styles
  268. */
  269. function libretto_editor_styles() {
  270. wp_enqueue_style( 'libretto-editor-block-style', get_template_directory_uri() . '/css/editor-blocks.css' );
  271. wp_enqueue_style( 'libretto-fonts', libretto_fonts_url(), array(), null );
  272. }
  273. add_action( 'enqueue_block_editor_assets', 'libretto_editor_styles' );
  274. /**
  275. * Create a reusable array of available sidebars.
  276. * Used in sidebar.php and in inc/jetpack.php to adjust footer area according to usage.
  277. */
  278. function libretto_get_active_sidebars() {
  279. // Loop through all possible sidebar areas to determine if they're active or not
  280. $available_sidebars = array( 'sidebar-1', 'sidebar-2', 'sidebar-3', 'sidebar-4' );
  281. $active_sidebars = array();
  282. foreach ( $available_sidebars as $sidebar_name ) :
  283. if ( is_active_sidebar( $sidebar_name ) ) {
  284. $active_sidebars[] = $sidebar_name;
  285. }
  286. endforeach;
  287. return $active_sidebars;
  288. }
  289. /*
  290. * Get information about the header image for our current page.
  291. *
  292. * We want to use an image in the background of our masthead in two cases:
  293. * if there's a featured image for the post, or if there's a header image set for the site.
  294. * In either case, we're going to need know the dimensions of the image and the URL.
  295. * Usage: libretto_get_header_image( 'height' );
  296. * You can pass a variable to request a particular type of information. Default is URL.
  297. */
  298. function libretto_get_header_image( $request = '' ) {
  299. // If there's a featured image set for the post/page, use that
  300. if ( libretto_has_post_thumbnail() && is_single() && libretto_jetpack_featured_image_display() ) :
  301. $libretto_featured_image = libretto_get_attachment_image_src( get_the_ID(), get_post_thumbnail_id( get_the_ID() ), 'libretto-fullpage' );
  302. $libretto_header_image = $libretto_featured_image;
  303. $libretto_header_image_height = 1000;
  304. elseif ( has_post_thumbnail() && is_singular() && libretto_jetpack_featured_image_display() ) :
  305. $libretto_featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'libretto-fullpage' );
  306. $libretto_header_image = $libretto_featured_image[0];
  307. $libretto_header_image_height = $libretto_featured_image[2];
  308. // Otherwise, use the header image
  309. elseif ( get_header_image() ) :
  310. $libretto_header_image = get_header_image();
  311. $libretto_header_image_height = get_custom_header()->height;
  312. endif;
  313. // We'll return different information depending on the parameter passed
  314. // This allows us to use the same function for two things
  315. if ( isset( $libretto_header_image ) ) :
  316. if ( 'height' === $request ) {
  317. return $libretto_header_image_height;
  318. } else {
  319. return $libretto_header_image;
  320. }
  321. endif;
  322. }
  323. /**
  324. * Use a bare ellipsis after post excerpts.
  325. */
  326. function libretto_excerpt_more( $more ) {
  327. return '&hellip;';
  328. }
  329. add_filter( 'excerpt_more', 'libretto_excerpt_more' );
  330. /**
  331. * Add a blockquote tag around the content of a quote post format,
  332. * if the content of the post doesn't already contain one.
  333. */
  334. function libretto_add_blockquote_to_quote( $content ) {
  335. if ( is_singular() || is_archive() || is_home() ) :
  336. // Only run on quote post types, and only for those that don't already contain a blockquote
  337. // Note: we look for "<blockquote" specifically so as to catch instances of blockquotes with additional attributes
  338. if ( 'quote' === get_post_format() && strpos( $content, '<blockquote' ) === false ) {
  339. $content = '<blockquote>' . $content . '</blockquote>';
  340. }
  341. endif;
  342. return $content;
  343. }
  344. add_filter( 'the_content', 'libretto_add_blockquote_to_quote' );
  345. add_filter( 'get_the_excerpt', 'libretto_add_blockquote_to_quote' );
  346. /**
  347. * Implement the Custom Header feature.
  348. */
  349. require get_template_directory() . '/inc/custom-header.php';
  350. /**
  351. * Custom template tags for this theme.
  352. */
  353. require get_template_directory() . '/inc/template-tags.php';
  354. /**
  355. * Custom functions that act independently of the theme templates.
  356. */
  357. require get_template_directory() . '/inc/extras.php';
  358. /**
  359. * Customizer additions.
  360. */
  361. require get_template_directory() . '/inc/customizer.php';
  362. /**
  363. * Load Jetpack compatibility file.
  364. */
  365. require get_template_directory() . '/inc/jetpack.php';