functions.php 12 KB

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