functions.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 core custom logo
  40. * - also see fallback in inc/jetpack.php
  41. */
  42. add_theme_support( 'custom-logo', array(
  43. 'height' => 800,
  44. 'width' => 250,
  45. 'flex-width' => true,
  46. 'flex-height' => true,
  47. ) );
  48. /*
  49. * Enable support for Post Thumbnails on posts and pages.
  50. *
  51. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  52. */
  53. add_theme_support( 'post-thumbnails' );
  54. add_image_size( 'affinity-featured', 1500, 9999 );
  55. // This theme uses wp_nav_menu() in one location.
  56. register_nav_menus( array(
  57. 'top' => esc_html__( 'Top', 'affinity' ),
  58. ) );
  59. /*
  60. * Switch default core markup for search form, comment form, and comments
  61. * to output valid HTML5.
  62. */
  63. add_theme_support( 'html5', array(
  64. 'search-form',
  65. 'comment-form',
  66. 'comment-list',
  67. 'gallery',
  68. 'caption',
  69. ) );
  70. // Set up the WordPress core custom background feature.
  71. add_theme_support( 'custom-background', apply_filters( 'affinity_custom_background_args', array(
  72. 'default-color' => 'ffffff',
  73. 'default-image' => '',
  74. ) ) );
  75. }
  76. endif;
  77. add_action( 'after_setup_theme', 'affinity_setup' );
  78. /**
  79. * Look for Jetpack Site Logos if no core Custom Logo is found
  80. *
  81. * @todo Remove after WP 4.7 release
  82. */
  83. function affinity_the_site_logo() {
  84. if ( function_exists( 'the_custom_logo' ) && current_theme_supports( 'custom-logo' ) ) {
  85. the_custom_logo();
  86. } elseif ( function_exists( 'jetpack_the_site_logo' ) ) {
  87. jetpack_the_site_logo();
  88. } else {
  89. return;
  90. }
  91. }
  92. /**
  93. * Set the content width in pixels, based on the theme's design and stylesheet.
  94. *
  95. * Priority 0 to make it available to lower priority callbacks.
  96. *
  97. * @global int $content_width
  98. */
  99. function affinity_content_width() {
  100. // Largest width at mobile breakpoint 46em is 820px
  101. // Actual width at largest possible screen size is 540px
  102. $GLOBALS['content_width'] = apply_filters( 'affinity_content_width', 820 );
  103. }
  104. add_action( 'after_setup_theme', 'affinity_content_width', 0 );
  105. /*
  106. * Adjust $content_width for full-width, front page, and no-sidebar page templates
  107. */
  108. if ( ! function_exists( 'affinity_content_width' ) ) :
  109. function affinity_content_width() {
  110. global $content_width;
  111. if ( is_page_template( 'fullwidth-page.php' ) ) {
  112. $content_width = 1004;
  113. }
  114. if ( is_page_template( 'front-page.php' ) || ! is_active_sidebar( 'sidebar-1' ) ) {
  115. $content_width = 716;
  116. }
  117. }
  118. add_action( 'template_redirect', 'affinity_content_width' );
  119. endif; // if ! function_exists( 'affinity_content_width' )
  120. /**
  121. * Register widget area.
  122. *
  123. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  124. */
  125. function affinity_widgets_init() {
  126. register_sidebar( array(
  127. 'name' => esc_html__( 'Sidebar', 'affinity' ),
  128. 'id' => 'sidebar-1',
  129. 'description' => '',
  130. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  131. 'after_widget' => '</section>',
  132. 'before_title' => '<h2 class="widget-title">',
  133. 'after_title' => '</h2>',
  134. ) );
  135. register_sidebar( array(
  136. 'name' => esc_html__( 'Footer Widgets 1', 'affinity' ),
  137. 'id' => 'footer-1',
  138. 'description' => '',
  139. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  140. 'after_widget' => '</section>',
  141. 'before_title' => '<h2 class="widget-title">',
  142. 'after_title' => '</h2>',
  143. ) );
  144. register_sidebar( array(
  145. 'name' => esc_html__( 'Footer Widgets 2', 'affinity' ),
  146. 'id' => 'footer-2',
  147. 'description' => '',
  148. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  149. 'after_widget' => '</section>',
  150. 'before_title' => '<h2 class="widget-title">',
  151. 'after_title' => '</h2>',
  152. ) );
  153. register_sidebar( array(
  154. 'name' => esc_html__( 'Footer Widgets 3', 'affinity' ),
  155. 'id' => 'footer-3',
  156. 'description' => '',
  157. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  158. 'after_widget' => '</section>',
  159. 'before_title' => '<h2 class="widget-title">',
  160. 'after_title' => '</h2>',
  161. ) );
  162. }
  163. add_action( 'widgets_init', 'affinity_widgets_init' );
  164. /**
  165. * Register Google Fonts
  166. */
  167. function affinity_fonts_url() {
  168. $fonts_url = '';
  169. /* Translators: If there are characters in your language that are not
  170. * supported by Raleway, translate this to 'off'. Do not translate
  171. * into your own language.
  172. */
  173. $raleway = esc_html_x( 'on', 'Raleway font: on or off', 'affinity' );
  174. /* Translators: If there are characters in your language that are not
  175. * supported by Lora; translate this to 'off'. Do not translate
  176. * into your own language.
  177. */
  178. $lora = esc_html_x( 'on', 'Lora font: on or off', 'affinity' );
  179. if ( 'off' !== $raleway || 'off' !== $lora ) {
  180. $font_families = array();
  181. if ( 'off' !== $raleway ) {
  182. $font_families[] = 'Raleway:400,400italic,700,700italic';
  183. }
  184. if ( 'off' !== $lora ) {
  185. $font_families[] = 'Lora:400,400italic,700,700italic';
  186. }
  187. $query_args = array(
  188. 'family' => urlencode( implode( '|', $font_families ) ),
  189. 'subset' => urlencode( 'latin,latin-ext' ),
  190. );
  191. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  192. }
  193. return $fonts_url;
  194. }
  195. /**
  196. * Enqueue scripts and styles.
  197. */
  198. function affinity_scripts() {
  199. wp_enqueue_style( 'affinity-style', get_stylesheet_uri() );
  200. // Gutenberg styles
  201. wp_enqueue_style( 'affinity-blocks', get_template_directory_uri() . '/blocks.css' );
  202. wp_enqueue_style( 'affinity-fonts', affinity_fonts_url(), array(), null );
  203. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons/genericons.css', array(), '3.4.1' );
  204. if ( is_front_page() ) {
  205. wp_enqueue_script( 'affinity-frontpage', get_template_directory_uri() . '/assets/js/frontpage.js', array( 'jquery' ), '20160324', true );
  206. }
  207. wp_enqueue_script( 'affinity-functions', get_template_directory_uri() . '/assets/js/functions.js', array( 'jquery', 'masonry' ), '20160324', true );
  208. wp_enqueue_script( 'affinity-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), '20151215', true );
  209. wp_enqueue_script( 'affinity-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );
  210. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  211. wp_enqueue_script( 'comment-reply' );
  212. }
  213. }
  214. add_action( 'wp_enqueue_scripts', 'affinity_scripts' );
  215. /**
  216. * Gutenberg Editor Styles
  217. */
  218. function affinity_editor_styles() {
  219. wp_enqueue_style( 'affinity-blocks-editor-style', get_template_directory_uri() . '/editor-blocks.css');
  220. wp_enqueue_style( 'affinity-fonts', affinity_fonts_url(), array(), null );
  221. }
  222. add_action( 'enqueue_block_editor_assets', 'affinity_editor_styles' );
  223. /* Allow user to adjust opacity of overlay to work with lighter/darker photos */
  224. function affinity_style_options() {
  225. $opacity = get_theme_mod( 'affinity_overlay', '0.7' );
  226. $backgroundscroll = get_theme_mod( 'affinity_scrolling', true );
  227. if ( '0' !== $opacity ) { ?>
  228. <style type="text/css" id="affinity-overlay-opacity">
  229. .custom-header-image {
  230. opacity: <?php echo esc_attr( $opacity ); ?>;
  231. }
  232. .in-panel .custom-header-image {
  233. opacity: 1;
  234. }
  235. @media screen and ( min-width: 48em ) {
  236. .custom-header-image,
  237. .in-panel .custom-header-image {
  238. opacity: <?php echo esc_attr( $opacity ); ?>;
  239. }
  240. }
  241. </style>
  242. <?php }
  243. if ( true == $backgroundscroll ) { ?>
  244. <style type="text/css" id="affinity-scrolling-background-images">
  245. @media screen and (min-width: 65em) {
  246. .custom-header-image {
  247. background-attachment: fixed;
  248. background-size: cover;
  249. background-position: center;
  250. }
  251. /* Don't allow scrolling in the Customizer or IE11/Edge */
  252. .affinity-customizer .custom-header-image,
  253. .is-edge .custom-header-image {
  254. background-attachment: scroll;
  255. background-size: cover;
  256. }
  257. }
  258. </style>
  259. <?php }
  260. }
  261. add_action( 'wp_head', 'affinity_style_options' );
  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';