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