functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. /**
  237. * A filter to enable child themes to add/change/omit font families.
  238. *
  239. * @param array $font_families An array of font families to be imploded for the Google Font API
  240. */
  241. $font_families = apply_filters( 'included_google_font_families', $font_families );
  242. $query_args = array(
  243. 'family' => urlencode( implode( '|', $font_families ) ),
  244. 'subset' => urlencode( 'latin,latin-ext' ),
  245. );
  246. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  247. endif;
  248. return $fonts_url;
  249. }
  250. /**
  251. * Enqueue scripts and styles
  252. */
  253. function libretto_scripts() {
  254. // General site stylesheet & JS
  255. wp_enqueue_style( 'libretto-style', get_stylesheet_uri() );
  256. wp_enqueue_script( 'libretto-script', get_template_directory_uri() . '/js/libretto.js', array( 'jquery' ), '20140331' );
  257. // Gutenberg styles
  258. wp_enqueue_style( 'libretto-blocks', get_template_directory_uri() . '/css/blocks.css' );
  259. // Fonts
  260. wp_enqueue_style( 'libretto-fonts', libretto_fonts_url(), array(), null );
  261. wp_enqueue_style( 'libretto-custom-icons', get_template_directory_uri() . '/icons/icons.css', array(), null );
  262. // Navigation
  263. wp_enqueue_script( 'libretto-touche', get_template_directory_uri() . '/js/touche.js', '20150604', true );
  264. wp_enqueue_script( 'libretto-navigation', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), '20150115', true );
  265. wp_enqueue_script( 'libretto-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  266. // Comments
  267. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  268. wp_enqueue_script( 'comment-reply' );
  269. }
  270. }
  271. add_action( 'wp_enqueue_scripts', 'libretto_scripts' );
  272. /**
  273. * Gutenberg Editor Styles
  274. */
  275. function libretto_editor_styles() {
  276. wp_enqueue_style( 'libretto-editor-block-style', get_template_directory_uri() . '/css/editor-blocks.css' );
  277. wp_enqueue_style( 'libretto-fonts', libretto_fonts_url(), array(), null );
  278. }
  279. add_action( 'enqueue_block_editor_assets', 'libretto_editor_styles' );
  280. /**
  281. * Create a reusable array of available sidebars.
  282. * Used in sidebar.php and in inc/jetpack.php to adjust footer area according to usage.
  283. */
  284. function libretto_get_active_sidebars() {
  285. // Loop through all possible sidebar areas to determine if they're active or not
  286. $available_sidebars = array( 'sidebar-1', 'sidebar-2', 'sidebar-3', 'sidebar-4' );
  287. $active_sidebars = array();
  288. foreach ( $available_sidebars as $sidebar_name ) :
  289. if ( is_active_sidebar( $sidebar_name ) ) {
  290. $active_sidebars[] = $sidebar_name;
  291. }
  292. endforeach;
  293. return $active_sidebars;
  294. }
  295. /*
  296. * Get information about the header image for our current page.
  297. *
  298. * We want to use an image in the background of our masthead in two cases:
  299. * if there's a featured image for the post, or if there's a header image set for the site.
  300. * In either case, we're going to need know the dimensions of the image and the URL.
  301. * Usage: libretto_get_header_image( 'height' );
  302. * You can pass a variable to request a particular type of information. Default is URL.
  303. */
  304. function libretto_get_header_image( $request = '' ) {
  305. $libretto_header_image_height = null;
  306. // If there's a featured image set for the post/page, use that
  307. if ( libretto_has_post_thumbnail() && is_single() && libretto_jetpack_featured_image_display() ) :
  308. $libretto_featured_image = libretto_get_attachment_image_src( get_the_ID(), get_post_thumbnail_id( get_the_ID() ), 'libretto-fullpage' );
  309. $libretto_header_image = $libretto_featured_image;
  310. $libretto_header_image_height = 1000;
  311. elseif ( has_post_thumbnail() && is_singular() && libretto_jetpack_featured_image_display() ) :
  312. $libretto_featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'libretto-fullpage' );
  313. $libretto_header_image = $libretto_featured_image[0];
  314. $libretto_header_image_height = $libretto_featured_image[2];
  315. // Otherwise, use the header image
  316. // Otherwise, use the header image
  317. elseif ( get_header_image() ) :
  318. $libretto_header_image = get_header_image();
  319. $libretto_header_image_height = get_custom_header()->height;
  320. endif;
  321. // We'll return different information depending on the parameter passed
  322. // This allows us to use the same function for two things
  323. if ( isset( $libretto_header_image ) ) :
  324. if ( 'height' === $request ) {
  325. return $libretto_header_image_height;
  326. } else {
  327. return $libretto_header_image;
  328. }
  329. endif;
  330. }
  331. /**
  332. * Use a bare ellipsis after post excerpts.
  333. */
  334. function libretto_excerpt_more( $more ) {
  335. return '&hellip;';
  336. }
  337. add_filter( 'excerpt_more', 'libretto_excerpt_more' );
  338. /**
  339. * Add a blockquote tag around the content of a quote post format,
  340. * if the content of the post doesn't already contain one.
  341. */
  342. function libretto_add_blockquote_to_quote( $content ) {
  343. if ( is_singular() || is_archive() || is_home() ) :
  344. // Only run on quote post types, and only for those that don't already contain a blockquote
  345. // Note: we look for "<blockquote" specifically so as to catch instances of blockquotes with additional attributes
  346. if ( 'quote' === get_post_format() && strpos( $content, '<blockquote' ) === false ) {
  347. $content = '<blockquote>' . $content . '</blockquote>';
  348. }
  349. endif;
  350. return $content;
  351. }
  352. add_filter( 'the_content', 'libretto_add_blockquote_to_quote' );
  353. add_filter( 'get_the_excerpt', 'libretto_add_blockquote_to_quote' );
  354. /**
  355. * Implement the Custom Header feature.
  356. */
  357. require get_template_directory() . '/inc/custom-header.php';
  358. /**
  359. * Custom template tags for this theme.
  360. */
  361. require get_template_directory() . '/inc/template-tags.php';
  362. /**
  363. * Custom functions that act independently of the theme templates.
  364. */
  365. require get_template_directory() . '/inc/extras.php';
  366. /**
  367. * Customizer additions.
  368. */
  369. require get_template_directory() . '/inc/customizer.php';
  370. /**
  371. * Load Jetpack compatibility file.
  372. */
  373. require get_template_directory() . '/inc/jetpack.php';