functions.php 11 KB

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