functions.php 10 KB

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