functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * Pique functions and definitions
  4. *
  5. * @package Pique
  6. */
  7. if ( ! function_exists( 'pique_setup' ) ) :
  8. /**
  9. * Sets up theme defaults and registers support for various WordPress features.
  10. *
  11. * Note that this function is hooked into the after_setup_theme hook, which
  12. * runs before the init hook. The init hook is too late for some features, such
  13. * as indicating support for post thumbnails.
  14. */
  15. function pique_setup() {
  16. /*
  17. * Make theme available for translation.
  18. * Translations can be filed in the /languages/ directory.
  19. * If you're building a theme based on Pique, use a find and replace
  20. * to change 'pique' to the name of your theme in all the template files
  21. */
  22. load_theme_textdomain( 'pique', get_template_directory() . '/languages' );
  23. // Add default posts and comments RSS feed links to head.
  24. add_theme_support( 'automatic-feed-links' );
  25. /*
  26. * Let WordPress manage the document title.
  27. * By adding theme support, we declare that this theme does not use a
  28. * hard-coded <title> tag in the document head, and expect WordPress to
  29. * provide it for us.
  30. */
  31. add_theme_support( 'title-tag' );
  32. /*
  33. * Enable support for Post Thumbnails on posts and pages.
  34. *
  35. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  36. */
  37. add_theme_support( 'post-thumbnails' );
  38. add_image_size( 'pique-hero', 1400, 1000, true );
  39. add_image_size( 'pique-square', 280, 280, true );
  40. add_image_size( 'pique-header', 1400, 400, true );
  41. add_image_size( 'pique-thumbnail-avatar', 100, 100, true );
  42. // This theme uses wp_nav_menu() in three locations.
  43. register_nav_menus(
  44. array(
  45. 'primary' => esc_html__( 'Primary Menu', 'pique' ),
  46. 'secondary' => esc_html__( 'Secondary Menu', 'pique' ),
  47. )
  48. );
  49. /*
  50. * Switch default core markup for search form, comment form, and comments
  51. * to output valid HTML5.
  52. */
  53. add_theme_support(
  54. 'html5',
  55. array(
  56. 'search-form',
  57. 'comment-form',
  58. 'comment-list',
  59. 'gallery',
  60. 'caption',
  61. )
  62. );
  63. /*
  64. * Enable support for Post Formats.
  65. * See http://codex.wordpress.org/Post_Formats
  66. */
  67. add_theme_support(
  68. 'post-formats',
  69. array(
  70. 'aside',
  71. 'chat',
  72. 'gallery',
  73. 'image',
  74. 'video',
  75. 'quote',
  76. 'link',
  77. 'status',
  78. 'audio',
  79. )
  80. );
  81. // Set up the WordPress core custom background feature.
  82. add_theme_support(
  83. 'custom-background',
  84. apply_filters(
  85. 'pique_custom_background_args',
  86. array(
  87. 'default-color' => 'ffffff',
  88. 'default-image' => '',
  89. )
  90. )
  91. );
  92. // Add support for responsive embeds.
  93. add_theme_support( 'responsive-embeds' );
  94. // Add support for full and wide align images.
  95. add_theme_support( 'align-wide' );
  96. // Add support for custom color scheme.
  97. add_theme_support(
  98. 'editor-color-palette',
  99. array(
  100. array(
  101. 'name' => esc_html__( 'Dark Blue', 'pique' ),
  102. 'slug' => 'dark-blue',
  103. 'color' => '#293940',
  104. ),
  105. array(
  106. 'name' => esc_html__( 'Medium Blue', 'pique' ),
  107. 'slug' => 'medium-blue',
  108. 'color' => '#3c7993',
  109. ),
  110. array(
  111. 'name' => esc_html__( 'Light Blue', 'pique' ),
  112. 'slug' => 'light-blue',
  113. 'color' => '#83b6cc',
  114. ),
  115. array(
  116. 'name' => esc_html__( 'Dark Brown', 'pique' ),
  117. 'slug' => 'dark-brown',
  118. 'color' => '#2d2a26',
  119. ),
  120. array(
  121. 'name' => esc_html__( 'Dark Gray', 'pique' ),
  122. 'slug' => 'dark-gray',
  123. 'color' => '#5d5d5d',
  124. ),
  125. array(
  126. 'name' => esc_html__( 'Medium Gray', 'pique' ),
  127. 'slug' => 'medium-gray',
  128. 'color' => '#a9a9a9',
  129. ),
  130. array(
  131. 'name' => esc_html__( 'White', 'pique' ),
  132. 'slug' => 'white',
  133. 'color' => '#fff',
  134. ),
  135. )
  136. );
  137. }
  138. endif; // pique_setup
  139. add_action( 'after_setup_theme', 'pique_setup' );
  140. /**
  141. * Set the content width in pixels, based on the theme's design and stylesheet.
  142. *
  143. * Priority 0 to make it available to lower priority callbacks.
  144. *
  145. * @global int $content_width
  146. */
  147. function pique_content_width() {
  148. $GLOBALS['content_width'] = apply_filters( 'pique_content_width', 775 );
  149. }
  150. add_action( 'after_setup_theme', 'pique_content_width', 0 );
  151. /**
  152. * Use a larger content width for full-width pages.
  153. */
  154. if ( ! function_exists( 'pique_content_width_tweak' ) ) :
  155. function pique_content_width_tweak() {
  156. if ( is_page_template( 'page-templates/template-full-width.php' ) ) :
  157. global $content_width;
  158. $content_width = 1400; /* pixels */
  159. endif;
  160. }
  161. endif;
  162. add_action( 'template_redirect', 'pique_content_width_tweak' );
  163. /**
  164. * Register widget area.
  165. *
  166. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  167. */
  168. function pique_widgets_init() {
  169. register_sidebar(
  170. array(
  171. 'name' => esc_html__( 'Sidebar', 'pique' ),
  172. 'id' => 'sidebar-1',
  173. 'description' => esc_html__( 'Add widgets here to appear in your sidebar', 'pique' ),
  174. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  175. 'after_widget' => '</aside>',
  176. 'before_title' => '<h2 class="widget-title">',
  177. 'after_title' => '</h2>',
  178. )
  179. );
  180. register_sidebar(
  181. array(
  182. 'name' => esc_html__( 'First Footer Widget Area', 'pique' ),
  183. 'id' => 'sidebar-2',
  184. 'description' => esc_html__( 'Add widgets here to appear in your footer', 'pique' ),
  185. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  186. 'after_widget' => '</aside>',
  187. 'before_title' => '<h2 class="widget-title">',
  188. 'after_title' => '</h2>',
  189. )
  190. );
  191. register_sidebar(
  192. array(
  193. 'name' => esc_html__( 'Second Footer Widget Area', 'pique' ),
  194. 'id' => 'sidebar-3',
  195. 'description' => esc_html__( 'Add widgets here to appear in your footer', 'pique' ),
  196. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  197. 'after_widget' => '</aside>',
  198. 'before_title' => '<h2 class="widget-title">',
  199. 'after_title' => '</h2>',
  200. )
  201. );
  202. register_sidebar(
  203. array(
  204. 'name' => esc_html__( 'Third Footer Widget Area', 'pique' ),
  205. 'id' => 'sidebar-4',
  206. 'description' => esc_html__( 'Add widgets here to appear in your footer', 'pique' ),
  207. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  208. 'after_widget' => '</aside>',
  209. 'before_title' => '<h2 class="widget-title">',
  210. 'after_title' => '</h2>',
  211. )
  212. );
  213. }
  214. add_action( 'widgets_init', 'pique_widgets_init' );
  215. /**
  216. * Register Google Fonts
  217. */
  218. function pique_fonts_url() {
  219. $fonts_url = '';
  220. /* Translators: If there are characters in your language that are not
  221. * supported by Lora, translate this to 'off'. Do not translate
  222. * into your own language.
  223. */
  224. $lora = esc_html_x( 'on', 'Lora font: on or off', 'pique' );
  225. /* Translators: If there are characters in your language that are not
  226. * supported by Karla, translate this to 'off'. Do not translate
  227. * into your own language.
  228. */
  229. $karla = esc_html_x( 'on', 'Karla font: on or off', 'pique' );
  230. if ( 'off' !== $lora || 'off' !== $karla ) :
  231. $font_families = array();
  232. if ( 'off' !== $lora ) {
  233. $font_families[] = 'Lora:400,700,400italic,700italic';
  234. }
  235. if ( 'off' !== $karla ) {
  236. $font_families[] = 'Karla:400,700,400italic,700italic';
  237. }
  238. /**
  239. * A filter to enable child themes to add/change/omit font families.
  240. *
  241. * @param array $font_families An array of font families to be imploded for the Google Font API
  242. */
  243. $font_families = apply_filters( 'included_google_font_families', $font_families );
  244. $query_args = array(
  245. 'family' => urlencode( implode( '|', $font_families ) ),
  246. 'subset' => urlencode( 'latin,latin-ext' ),
  247. );
  248. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  249. endif;
  250. return $fonts_url;
  251. }
  252. /**
  253. * Enqueue Google Fonts for custom headers
  254. */
  255. function pique_admin_scripts() {
  256. wp_enqueue_style( 'pique-fonts', pique_fonts_url(), array(), null );
  257. }
  258. add_action( 'admin_print_styles-appearance_page_custom-header', 'pique_admin_scripts' );
  259. /**
  260. * Enqueue scripts and styles.
  261. */
  262. function pique_scripts() {
  263. wp_enqueue_style( 'pique-style', get_stylesheet_uri(), array(), null, 'screen' );
  264. wp_enqueue_style( 'pique-fonts', pique_fonts_url(), array(), null );
  265. // Block stylesheets
  266. wp_enqueue_style( 'pique-block-style', get_template_directory_uri() . '/assets/css/blocks.css', array( 'pique-style' ), '20181018' );
  267. // Background fix for iOS
  268. if ( is_front_page() || is_home() ) :
  269. wp_enqueue_script( 'pique-background-fix', get_template_directory_uri() . '/assets/js/background-fix.js', array( 'jquery' ), '20170302', true );
  270. endif;
  271. // Header and navigation
  272. wp_enqueue_script( 'waypoints', get_template_directory_uri() . '/assets/js/jquery.waypoints.min.js', array( 'jquery' ), '20150813', true );
  273. wp_enqueue_script( 'pique-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array( 'jquery' ), '20120206', true );
  274. wp_enqueue_script( 'pique-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20130115', true );
  275. wp_enqueue_script( 'pique-header', get_template_directory_uri() . '/assets/js/header.js', array( 'jquery', 'waypoints' ), '20151030', true );
  276. // Scroll effects (only loaded on front page)
  277. if ( pique_is_frontpage() ) :
  278. wp_enqueue_script( 'scrollTo', get_template_directory_uri() . '/assets/js/jquery.scrollTo.min.js', array( 'jquery' ), '20151030', true );
  279. wp_enqueue_script( 'pique-front-page', get_template_directory_uri() . '/assets/js/front-page.js', array( 'scrollTo', 'waypoints' ), '20151030', true );
  280. endif;
  281. // Font icons, because we're retro like that
  282. wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/fonts/font-awesome.min.css', array(), null );
  283. if ( wp_style_is( 'genericons', 'registered' ) ) {
  284. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), null );
  285. } else {
  286. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), null );
  287. }
  288. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  289. wp_enqueue_script( 'comment-reply' );
  290. }
  291. }
  292. add_action( 'wp_enqueue_scripts', 'pique_scripts' );
  293. /**
  294. * Enqueue editor styles for Gutenberg
  295. */
  296. function pique_block_editor_styles() {
  297. // Block styles.
  298. wp_enqueue_style( 'pique-block-editor-style', get_template_directory_uri() . '/assets/css/editor-blocks.css' );
  299. // Font styles.
  300. wp_enqueue_style( 'pique-fonts', pique_fonts_url(), array(), null );
  301. }
  302. add_action( 'enqueue_block_editor_assets', 'pique_block_editor_styles' );
  303. /**
  304. * Filter the front page template so it's bypassed entirely if the user selects
  305. * to display blog posts on their homepage instead of a static page.
  306. */
  307. function pique_filter_front_page_template( $template ) {
  308. return is_home() ? '' : $template;
  309. }
  310. add_filter( 'frontpage_template', 'pique_filter_front_page_template' );
  311. function pique_query_vars( $qvars ) {
  312. $qvars[] = 'pique_panel';
  313. return $qvars;
  314. }
  315. add_filter( 'query_vars', 'pique_query_vars', 10, 1 );
  316. /**
  317. * Get random posts; a simple, more efficient approach.
  318. * MySQL queries that use ORDER BY RAND() can be pretty challenging and slow on large datasets.
  319. * Also it works better with heavy caching.
  320. */
  321. function pique_get_random_posts( $number = 1, $post_type = 'post' ) {
  322. $query = new WP_Query(
  323. array(
  324. 'posts_per_page' => 100,
  325. 'fields' => 'ids',
  326. 'post_type' => $post_type,
  327. )
  328. );
  329. $post_ids = $query->posts;
  330. shuffle( $post_ids );
  331. $post_ids = array_splice( $post_ids, 0, $number );
  332. $random_posts = get_posts(
  333. array(
  334. 'post__in' => $post_ids,
  335. 'numberposts' => count( $post_ids ),
  336. 'post_type' => $post_type,
  337. )
  338. );
  339. return $random_posts;
  340. }
  341. /**
  342. * Implement the Custom Header feature.
  343. */
  344. require get_template_directory() . '/inc/custom-header.php';
  345. /**
  346. * Custom template tags for this theme.
  347. */
  348. require get_template_directory() . '/inc/template-tags.php';
  349. /**
  350. * Custom functions that act independently of the theme templates.
  351. */
  352. require get_template_directory() . '/inc/extras.php';
  353. /**
  354. * Customizer additions.
  355. */
  356. require get_template_directory() . '/inc/customizer.php';
  357. /**
  358. * Load Jetpack compatibility file.
  359. */
  360. require get_template_directory() . '/inc/jetpack.php';
  361. /**
  362. * Load WooCommerce compatibility file.
  363. */
  364. if ( class_exists( 'WooCommerce' ) ) {
  365. require get_template_directory() . '/inc/woocommerce.php';
  366. }