functions.php 10 KB

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