functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * dyad 2 functions and definitions
  4. *
  5. * @package Dyad
  6. */
  7. if ( ! function_exists( 'dyad_2_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 dyad_2_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 dyad, use a find and replace
  20. * to change 'dyad-2' to the name of your theme in all the template files
  21. */
  22. load_theme_textdomain( 'dyad-2', 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. * Add custom logo support
  34. */
  35. add_theme_support(
  36. 'custom-logo',
  37. array(
  38. 'height' => 300,
  39. 'width' => 600,
  40. 'flex-width' => true,
  41. 'header-text' => array(
  42. 'site-title',
  43. 'site-description',
  44. ),
  45. )
  46. );
  47. /*
  48. * Register menus
  49. */
  50. register_nav_menus(
  51. array(
  52. 'menu-1' => esc_html__( 'Primary Menu', 'dyad-2' ),
  53. )
  54. );
  55. /*
  56. * Add Post Format support
  57. */
  58. add_theme_support( 'post-formats', array( 'image' ) );
  59. /*
  60. * Enable support for Post Thumbnails on posts and pages.
  61. *
  62. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  63. */
  64. add_theme_support( 'post-thumbnails' );
  65. /* setting custom image size for thumbnails for dyad 2 */
  66. add_image_size( 'dyad-2-banner', '1800', '720', true );
  67. add_image_size( 'dyad-2-thumbnails', '630', '840', true );
  68. add_image_size( 'dyad-2-thumbnails-horz', '680', '455', true );
  69. add_image_size( 'dyad-2-featured-image', '960', '1280', true );
  70. add_image_size( 'dyad-2-featured-image-horz', '960', '640', true );
  71. /*
  72. * Switch default core markup for search form, comment form, and comments
  73. * to output valid HTML5.
  74. */
  75. add_theme_support(
  76. 'html5',
  77. array(
  78. 'search-form',
  79. 'comment-form',
  80. 'gallery',
  81. 'caption',
  82. )
  83. );
  84. // Load default block styles.
  85. add_theme_support( 'wp-block-styles' );
  86. // Add support for full and wide align images.
  87. add_theme_support( 'align-wide' );
  88. // Add support for responsive embeds.
  89. add_theme_support( 'responsive-embeds' );
  90. // Add support for editor styles.
  91. add_theme_support( 'editor-styles' );
  92. // Editor Styles
  93. add_editor_style( 'css/editor-blocks.css' );
  94. add_editor_style( dyad_2_fonts_url() );
  95. // Add support for custom color scheme.
  96. add_theme_support(
  97. 'editor-color-palette',
  98. array(
  99. array(
  100. 'name' => esc_html__( 'Bright Blue', 'dyad-2' ),
  101. 'slug' => 'bright-blue',
  102. 'color' => '#678db8',
  103. ),
  104. array(
  105. 'name' => esc_html__( 'Yellow', 'dyad-2' ),
  106. 'slug' => 'yellow',
  107. 'color' => '#e7ae01',
  108. ),
  109. array(
  110. 'name' => esc_html__( 'Light Gray-Blue', 'dyad-2' ),
  111. 'slug' => 'light-gray-blue',
  112. 'color' => '#abb7c3',
  113. ),
  114. array(
  115. 'name' => esc_html__( 'Medium Gray', 'dyad-2' ),
  116. 'slug' => 'medium-gray',
  117. 'color' => '#6a6c6e',
  118. ),
  119. array(
  120. 'name' => esc_html__( 'Dark Gray', 'dyad-2' ),
  121. 'slug' => 'dark-gray',
  122. 'color' => '#1a1c1e',
  123. ),
  124. array(
  125. 'name' => esc_html__( 'Dark Gray-Blue', 'dyad-2' ),
  126. 'slug' => 'dark-gray-blue',
  127. 'color' => '#292c2f',
  128. ),
  129. array(
  130. 'name' => esc_html__( 'White', 'dyad-2' ),
  131. 'slug' => 'white',
  132. 'color' => '#fff',
  133. ),
  134. )
  135. );
  136. }
  137. endif; // dyad_2_setup
  138. add_action( 'after_setup_theme', 'dyad_2_setup' );
  139. /**
  140. * Set the content width in pixels, based on the theme's design and stylesheet.
  141. *
  142. * Priority 0 to make it available to lower priority callbacks.
  143. *
  144. * @global int $content_width
  145. */
  146. function dyad_2_content_width() {
  147. $GLOBALS['content_width'] = apply_filters( 'dyad_2_content_width', 1000 );
  148. }
  149. add_action( 'after_setup_theme', 'dyad_2_content_width', 0 );
  150. /**
  151. * Replaces the excerpt "more" text by a link
  152. */
  153. if ( ! function_exists( 'dyad_2_excerpt_continue_reading' ) ) {
  154. function dyad_2_excerpt_continue_reading() {
  155. return '... <div class="link-more"><a href="' . esc_url( get_permalink() ) . '">' . sprintf( esc_html__( 'Read More', 'dyad-2' ), '<span class="screen-reader-text"> "' . get_the_title() . '"</span>' ) . '</a></div>';
  156. }
  157. } // /dyad_2_excerpt_continue_reading
  158. add_filter( 'excerpt_more', 'dyad_2_excerpt_continue_reading' );
  159. /**
  160. * Register widget area.
  161. *
  162. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  163. */
  164. function dyad_2_widgets_init() {
  165. register_sidebar(
  166. array(
  167. 'name' => esc_html__( 'Footer', 'dyad-2' ),
  168. 'id' => 'sidebar-1',
  169. 'description' => esc_html__( 'Displays in footer area.', 'dyad-2' ),
  170. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  171. 'after_widget' => '</aside>',
  172. 'before_title' => '<h3 class="widget-title">',
  173. 'after_title' => '</h3>',
  174. )
  175. );
  176. }
  177. add_action( 'widgets_init', 'dyad_2_widgets_init' );
  178. /**
  179. * Counts the number of widgets in a specific sidebar
  180. *
  181. * @param string $id
  182. * @return integer number of widgets in the sidebar
  183. */
  184. function dyad_2_count_widgets( $id ) {
  185. $count = 0;
  186. $sidebars_widgets = wp_get_sidebars_widgets();
  187. if ( array_key_exists( $id, $sidebars_widgets ) ) {
  188. foreach ( (array) $sidebars_widgets[ $id ] as $value ) {
  189. // Don't count the Cookies or Mailchimp widgets, since they're not visible in the widget area.
  190. if ( strpos( $value, 'eu_cookie_law_widget' ) === false && strpos( $value, 'widget_mailchimp_subscriber_popup' ) === false ) {
  191. $count++;
  192. }
  193. }
  194. }
  195. return $count;
  196. }
  197. /**
  198. * Widget column class helper
  199. *
  200. * @param string $id
  201. * @return string grid class
  202. */
  203. function dyad_2_widget_column_class( $widget_id ) {
  204. $count = dyad_2_count_widgets( $widget_id );
  205. $class = '';
  206. if ( $count >= 4 ) {
  207. $class .= 'widgets-four';
  208. } elseif ( 3 == $count ) {
  209. $class .= 'widgets-three';
  210. } elseif ( 2 == $count ) {
  211. $class .= 'widgets-two';
  212. } else {
  213. $class .= 'widget-one';
  214. }
  215. return $class;
  216. }
  217. /**
  218. * Wrap avatars in div for easier styling
  219. */
  220. function dyad_2_get_avatar( $avatar ) {
  221. if ( ! is_admin() ) {
  222. $avatar = '<span class="avatar-container">' . $avatar . '</span>';
  223. }
  224. return $avatar;
  225. }
  226. add_filter( 'get_avatar', 'dyad_2_get_avatar', 10, 5 );
  227. /**
  228. * Google Fonts
  229. */
  230. function dyad_2_fonts_url() {
  231. $fonts_url = '';
  232. /* Translators: If there are characters in your language that are not
  233. * supported by Lato, translate this to 'off'. Do not translate
  234. * into your own language.
  235. */
  236. $lato = _x( 'on', 'Lato font: on or off', 'dyad-2' );
  237. /* Translators: If there are characters in your language that are not
  238. * supported by Noto Serif, translate this to 'off'. Do not translate
  239. * into your own language.
  240. */
  241. $noto_serif = _x( 'on', 'Noto Serif font: on or off', 'dyad-2' );
  242. if ( 'off' !== $lato || 'off' !== $noto_serif ) {
  243. $font_families = array();
  244. if ( 'off' !== $lato ) {
  245. $font_families[] = 'Lato:400,400italic,700,700italic';
  246. }
  247. if ( 'off' !== $noto_serif ) {
  248. $font_families[] = 'Noto Serif:400,400italic,700,700italic';
  249. }
  250. /**
  251. * A filter to enable child themes to add/change/omit font families.
  252. *
  253. * @param array $font_families An array of font families to be imploded for the Google Font API
  254. */
  255. $font_families = apply_filters( 'included_google_font_families', $font_families );
  256. $query_args = array(
  257. 'family' => urlencode( implode( '|', $font_families ) ),
  258. 'subset' => urlencode( 'latin,latin-ext' ),
  259. );
  260. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  261. }
  262. return esc_url_raw( $fonts_url );
  263. }
  264. /**
  265. * Enqueue scripts and styles.
  266. */
  267. function dyad_2_scripts() {
  268. if ( ! wp_style_is( 'genericons', 'registered' ) ) {
  269. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
  270. }
  271. wp_enqueue_style( 'dyad-2-fonts', dyad_2_fonts_url(), array(), null );
  272. wp_enqueue_script( 'dyad-2-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
  273. wp_enqueue_script( 'dyad-2-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  274. if ( ( is_front_page() || is_home() ) && dyad_2_has_banner_posts( 2 ) ) {
  275. wp_enqueue_script( 'slick', get_template_directory_uri() . '/js/slick.js', array(), '20140523', true );
  276. }
  277. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  278. wp_enqueue_script( 'comment-reply' );
  279. }
  280. wp_enqueue_style( 'dyad-2-style', get_stylesheet_uri(), array( 'genericons' ) );
  281. // Theme block stylesheet.
  282. wp_enqueue_style( 'dyad-2-block-style', get_template_directory_uri() . '/css/blocks.css', array( 'dyad-2-style' ), '20181018' );
  283. wp_enqueue_script( 'dyad-2-global', get_template_directory_uri() . '/js/global.js', array( 'jquery', 'masonry' ), '20151204', true );
  284. }
  285. add_action( 'wp_enqueue_scripts', 'dyad_2_scripts' );
  286. /**
  287. * Enqueue editor styles for Gutenberg
  288. *
  289. */
  290. function dyad_2_block_editor_styles() {
  291. // Block styles.
  292. // wp_enqueue_style( 'dyad-2-block-editor-style', get_template_directory_uri() . '/css/editor-blocks.css' );
  293. // Add custom fonts.
  294. wp_enqueue_style( 'dyad-2-fonts', dyad_2_fonts_url(), array(), null );
  295. // Add Genericons.
  296. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
  297. }
  298. add_action( 'enqueue_block_editor_assets', 'dyad_2_block_editor_styles' );
  299. /**
  300. * Implement the Custom Header feature.
  301. */
  302. require get_template_directory() . '/inc/custom-header.php';
  303. /**
  304. * Custom template tags for this theme.
  305. */
  306. require get_template_directory() . '/inc/template-tags.php';
  307. /**
  308. * Custom functions that act independently of the theme templates.
  309. */
  310. require get_template_directory() . '/inc/extras.php';
  311. /**
  312. * Customizer additions.
  313. */
  314. require get_template_directory() . '/inc/customizer.php';
  315. /**
  316. * Load Jetpack compatibility file.
  317. */
  318. require get_template_directory() . '/inc/jetpack.php';
  319. /**
  320. * Load WooCommerce compatibility file.
  321. */
  322. if ( class_exists( 'WooCommerce' ) ) {
  323. require get_template_directory() . '/inc/woocommerce.php';
  324. }