functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * components functions and definitions.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Affinity
  8. */
  9. if ( ! function_exists( 'affinity_setup' ) ) :
  10. /**
  11. * Sets up theme defaults and registers support for various WordPress features.
  12. *
  13. * Note that this function is hooked into the aftercomponentsetup_theme hook, which
  14. * runs before the init hook. The init hook is too late for some features, such
  15. * as indicating support for post thumbnails.
  16. */
  17. function affinity_setup() {
  18. /*
  19. * Make theme available for translation.
  20. * Translations can be filed in the /languages/ directory.
  21. * If you're building a theme based on components, use a find and replace
  22. * to change 'affinity' to the name of your theme in all the template files.
  23. */
  24. load_theme_textdomain( 'affinity', get_template_directory() . '/languages' );
  25. // Add default posts and comments RSS feed links to head.
  26. add_theme_support( 'automatic-feed-links' );
  27. /*
  28. * Let WordPress manage the document title.
  29. * By adding theme support, we declare that this theme does not use a
  30. * hard-coded <title> tag in the document head, and expect WordPress to
  31. * provide it for us.
  32. */
  33. add_theme_support( 'title-tag' );
  34. /*
  35. * Add editor styles
  36. */
  37. add_editor_style();
  38. /*
  39. * Add support for responsive embeds.
  40. */
  41. add_theme_support( 'responsive-embeds' );
  42. /**
  43. * Add support for core custom logo
  44. * - also see fallback in inc/jetpack.php
  45. */
  46. add_theme_support(
  47. 'custom-logo',
  48. array(
  49. 'height' => 800,
  50. 'width' => 250,
  51. 'flex-width' => true,
  52. 'flex-height' => true,
  53. )
  54. );
  55. /*
  56. * Enable support for Post Thumbnails on posts and pages.
  57. *
  58. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  59. */
  60. add_theme_support( 'post-thumbnails' );
  61. add_image_size( 'affinity-featured', 1500, 9999 );
  62. // This theme uses wp_nav_menu() in one location.
  63. register_nav_menus(
  64. array(
  65. 'top' => esc_html__( 'Top', 'affinity' ),
  66. )
  67. );
  68. /*
  69. * Switch default core markup for search form, comment form, and comments
  70. * to output valid HTML5.
  71. */
  72. add_theme_support(
  73. 'html5',
  74. array(
  75. 'search-form',
  76. 'comment-form',
  77. 'comment-list',
  78. 'gallery',
  79. 'caption',
  80. )
  81. );
  82. // Set up the WordPress core custom background feature.
  83. add_theme_support(
  84. 'custom-background',
  85. apply_filters(
  86. 'affinity_custom_background_args',
  87. array(
  88. 'default-color' => 'ffffff',
  89. 'default-image' => '',
  90. )
  91. )
  92. );
  93. }
  94. endif;
  95. add_action( 'after_setup_theme', 'affinity_setup' );
  96. /**
  97. * Look for Jetpack Site Logos if no core Custom Logo is found
  98. *
  99. * @todo Remove after WP 4.7 release
  100. */
  101. function affinity_the_site_logo() {
  102. if ( function_exists( 'the_custom_logo' ) && current_theme_supports( 'custom-logo' ) ) {
  103. the_custom_logo();
  104. } elseif ( function_exists( 'jetpack_the_site_logo' ) ) {
  105. jetpack_the_site_logo();
  106. } else {
  107. return;
  108. }
  109. }
  110. /**
  111. * Set the content width in pixels, based on the theme's design and stylesheet.
  112. *
  113. * Priority 0 to make it available to lower priority callbacks.
  114. *
  115. * @global int $content_width
  116. */
  117. function affinity_content_width() {
  118. // Largest width at mobile breakpoint 46em is 820px
  119. // Actual width at largest possible screen size is 540px
  120. $GLOBALS['content_width'] = apply_filters( 'affinity_content_width', 820 );
  121. }
  122. add_action( 'after_setup_theme', 'affinity_content_width', 0 );
  123. /*
  124. * Adjust $content_width for full-width, front page, and no-sidebar page templates
  125. */
  126. if ( ! function_exists( 'affinity_content_width' ) ) :
  127. function affinity_content_width() {
  128. global $content_width;
  129. if ( is_page_template( 'fullwidth-page.php' ) ) {
  130. $content_width = 1004;
  131. }
  132. if ( is_page_template( 'front-page.php' ) || ! is_active_sidebar( 'sidebar-1' ) ) {
  133. $content_width = 716;
  134. }
  135. }
  136. add_action( 'template_redirect', 'affinity_content_width' );
  137. endif; // if ! function_exists( 'affinity_content_width' )
  138. /**
  139. * Register widget area.
  140. *
  141. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  142. */
  143. function affinity_widgets_init() {
  144. register_sidebar(
  145. array(
  146. 'name' => esc_html__( 'Sidebar', 'affinity' ),
  147. 'id' => 'sidebar-1',
  148. 'description' => '',
  149. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  150. 'after_widget' => '</section>',
  151. 'before_title' => '<h2 class="widget-title">',
  152. 'after_title' => '</h2>',
  153. )
  154. );
  155. register_sidebar(
  156. array(
  157. 'name' => esc_html__( 'Footer Widgets 1', 'affinity' ),
  158. 'id' => 'footer-1',
  159. 'description' => '',
  160. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  161. 'after_widget' => '</section>',
  162. 'before_title' => '<h2 class="widget-title">',
  163. 'after_title' => '</h2>',
  164. )
  165. );
  166. register_sidebar(
  167. array(
  168. 'name' => esc_html__( 'Footer Widgets 2', 'affinity' ),
  169. 'id' => 'footer-2',
  170. 'description' => '',
  171. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  172. 'after_widget' => '</section>',
  173. 'before_title' => '<h2 class="widget-title">',
  174. 'after_title' => '</h2>',
  175. )
  176. );
  177. register_sidebar(
  178. array(
  179. 'name' => esc_html__( 'Footer Widgets 3', 'affinity' ),
  180. 'id' => 'footer-3',
  181. 'description' => '',
  182. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  183. 'after_widget' => '</section>',
  184. 'before_title' => '<h2 class="widget-title">',
  185. 'after_title' => '</h2>',
  186. )
  187. );
  188. }
  189. add_action( 'widgets_init', 'affinity_widgets_init' );
  190. /**
  191. * Register Google Fonts
  192. */
  193. function affinity_fonts_url() {
  194. $fonts_url = '';
  195. /* Translators: If there are characters in your language that are not
  196. * supported by Raleway, translate this to 'off'. Do not translate
  197. * into your own language.
  198. */
  199. $raleway = esc_html_x( 'on', 'Raleway font: on or off', 'affinity' );
  200. /* Translators: If there are characters in your language that are not
  201. * supported by Lora; translate this to 'off'. Do not translate
  202. * into your own language.
  203. */
  204. $lora = esc_html_x( 'on', 'Lora font: on or off', 'affinity' );
  205. if ( 'off' !== $raleway || 'off' !== $lora ) {
  206. $font_families = array();
  207. if ( 'off' !== $raleway ) {
  208. $font_families[] = 'Raleway:400,400italic,700,700italic';
  209. }
  210. if ( 'off' !== $lora ) {
  211. $font_families[] = 'Lora:400,400italic,700,700italic';
  212. }
  213. /**
  214. * A filter to enable child themes to add/change/omit font families.
  215. *
  216. * @param array $font_families An array of font families to be imploded for the Google Font API
  217. */
  218. $font_families = apply_filters( 'included_google_font_families', $font_families );
  219. $query_args = array(
  220. 'family' => urlencode( implode( '|', $font_families ) ),
  221. 'subset' => urlencode( 'latin,latin-ext' ),
  222. );
  223. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  224. }
  225. return $fonts_url;
  226. }
  227. /**
  228. * Enqueue scripts and styles.
  229. */
  230. function affinity_scripts() {
  231. wp_enqueue_style( 'affinity-style', get_stylesheet_uri() );
  232. // Gutenberg styles
  233. wp_enqueue_style( 'affinity-blocks', get_template_directory_uri() . '/blocks.css' );
  234. wp_enqueue_style( 'affinity-fonts', affinity_fonts_url(), array(), null );
  235. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons/genericons.css', array(), '3.4.1' );
  236. if ( is_front_page() ) {
  237. wp_enqueue_script( 'affinity-frontpage', get_template_directory_uri() . '/assets/js/frontpage.js', array( 'jquery' ), '20160324', true );
  238. }
  239. wp_enqueue_script( 'affinity-functions', get_template_directory_uri() . '/assets/js/functions.js', array( 'jquery', 'masonry' ), '20160324', true );
  240. wp_enqueue_script( 'affinity-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), '20151215', true );
  241. wp_enqueue_script( 'affinity-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );
  242. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  243. wp_enqueue_script( 'comment-reply' );
  244. }
  245. }
  246. add_action( 'wp_enqueue_scripts', 'affinity_scripts' );
  247. /**
  248. * Gutenberg Editor Styles
  249. */
  250. function affinity_editor_styles() {
  251. wp_enqueue_style( 'affinity-blocks-editor-style', get_template_directory_uri() . '/editor-blocks.css' );
  252. wp_enqueue_style( 'affinity-fonts', affinity_fonts_url(), array(), null );
  253. }
  254. add_action( 'enqueue_block_editor_assets', 'affinity_editor_styles' );
  255. /* Allow user to adjust opacity of overlay to work with lighter/darker photos */
  256. function affinity_style_options() {
  257. $opacity = get_theme_mod( 'affinity_overlay', '0.7' );
  258. $backgroundscroll = get_theme_mod( 'affinity_scrolling', true );
  259. if ( '0' !== $opacity ) { ?>
  260. <style type="text/css" id="affinity-overlay-opacity">
  261. .custom-header-image {
  262. opacity: <?php echo esc_attr( $opacity ); ?>;
  263. }
  264. .in-panel .custom-header-image {
  265. opacity: 1;
  266. }
  267. @media screen and ( min-width: 48em ) {
  268. .custom-header-image,
  269. .in-panel .custom-header-image {
  270. opacity: <?php echo esc_attr( $opacity ); ?>;
  271. }
  272. }
  273. </style>
  274. <?php
  275. }
  276. if ( true == $backgroundscroll ) {
  277. ?>
  278. <style type="text/css" id="affinity-scrolling-background-images">
  279. @media screen and (min-width: 65em) {
  280. .custom-header-image {
  281. background-attachment: fixed;
  282. background-size: cover;
  283. background-position: center;
  284. }
  285. /* Don't allow scrolling in the Customizer or IE11/Edge */
  286. .affinity-customizer .custom-header-image,
  287. .is-edge .custom-header-image {
  288. background-attachment: scroll;
  289. background-size: cover;
  290. }
  291. }
  292. </style>
  293. <?php
  294. }
  295. }
  296. add_action( 'wp_head', 'affinity_style_options' );
  297. /**
  298. * Implement the Custom Header feature.
  299. */
  300. require get_template_directory() . '/inc/custom-header.php';
  301. /**
  302. * Custom template tags for this theme.
  303. */
  304. require get_template_directory() . '/inc/template-tags.php';
  305. /**
  306. * Custom functions that act independently of the theme templates.
  307. */
  308. require get_template_directory() . '/inc/extras.php';
  309. /**
  310. * Customizer additions.
  311. */
  312. require get_template_directory() . '/inc/customizer.php';
  313. /**
  314. * Load Jetpack compatibility file.
  315. */
  316. require get_template_directory() . '/inc/jetpack.php';