functions.php 9.6 KB

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