functions.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * Twenty Nineteen functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package WordPress
  8. * @subpackage Twenty_Nineteen
  9. * @since 1.0.0
  10. */
  11. /**
  12. * Twenty Nineteen only works in WordPress 4.7 or later.
  13. */
  14. if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
  15. require get_template_directory() . '/inc/back-compat.php';
  16. return;
  17. }
  18. if ( ! function_exists( 'twentynineteen_setup' ) ) :
  19. /**
  20. * Sets up theme defaults and registers support for various WordPress features.
  21. *
  22. * Note that this function is hooked into the after_setup_theme hook, which
  23. * runs before the init hook. The init hook is too late for some features, such
  24. * as indicating support for post thumbnails.
  25. */
  26. function twentynineteen_setup() {
  27. /*
  28. * Make theme available for translation.
  29. * Translations can be filed in the /languages/ directory.
  30. * If you're building a theme based on Twenty Nineteen, use a find and replace
  31. * to change 'twentynineteen' to the name of your theme in all the template files.
  32. */
  33. load_theme_textdomain( 'twentynineteen', get_template_directory() . '/languages' );
  34. // Add default posts and comments RSS feed links to head.
  35. add_theme_support( 'automatic-feed-links' );
  36. /*
  37. * Let WordPress manage the document title.
  38. * By adding theme support, we declare that this theme does not use a
  39. * hard-coded <title> tag in the document head, and expect WordPress to
  40. * provide it for us.
  41. */
  42. add_theme_support( 'title-tag' );
  43. /*
  44. * Enable support for Post Thumbnails on posts and pages.
  45. *
  46. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  47. */
  48. add_theme_support( 'post-thumbnails' );
  49. set_post_thumbnail_size( 1568, 9999 );
  50. // This theme uses wp_nav_menu() in two locations.
  51. register_nav_menus(
  52. array(
  53. 'menu-1' => __( 'Primary', 'twentynineteen' ),
  54. 'footer' => __( 'Footer Menu', 'twentynineteen' ),
  55. 'social' => __( 'Social Links Menu', 'twentynineteen' ),
  56. )
  57. );
  58. /*
  59. * Switch default core markup for search form, comment form, and comments
  60. * to output valid HTML5.
  61. */
  62. add_theme_support(
  63. 'html5',
  64. array(
  65. 'search-form',
  66. 'comment-form',
  67. 'comment-list',
  68. 'gallery',
  69. 'caption',
  70. 'script',
  71. 'style',
  72. )
  73. );
  74. /**
  75. * Add support for core custom logo.
  76. *
  77. * @link https://codex.wordpress.org/Theme_Logo
  78. */
  79. add_theme_support(
  80. 'custom-logo',
  81. array(
  82. 'height' => 190,
  83. 'width' => 190,
  84. 'flex-width' => false,
  85. 'flex-height' => false,
  86. )
  87. );
  88. // Add theme support for selective refresh for widgets.
  89. add_theme_support( 'customize-selective-refresh-widgets' );
  90. // Add support for Block Styles.
  91. add_theme_support( 'wp-block-styles' );
  92. // Add support for full and wide align images.
  93. add_theme_support( 'align-wide' );
  94. // Add support for editor styles.
  95. add_theme_support( 'editor-styles' );
  96. // Enqueue editor styles.
  97. add_editor_style( 'style-editor.css' );
  98. // Add custom editor font sizes.
  99. add_theme_support(
  100. 'editor-font-sizes',
  101. array(
  102. array(
  103. 'name' => __( 'Small', 'twentynineteen' ),
  104. 'shortName' => __( 'S', 'twentynineteen' ),
  105. 'size' => 19.5,
  106. 'slug' => 'small',
  107. ),
  108. array(
  109. 'name' => __( 'Normal', 'twentynineteen' ),
  110. 'shortName' => __( 'M', 'twentynineteen' ),
  111. 'size' => 22,
  112. 'slug' => 'normal',
  113. ),
  114. array(
  115. 'name' => __( 'Large', 'twentynineteen' ),
  116. 'shortName' => __( 'L', 'twentynineteen' ),
  117. 'size' => 36.5,
  118. 'slug' => 'large',
  119. ),
  120. array(
  121. 'name' => __( 'Huge', 'twentynineteen' ),
  122. 'shortName' => __( 'XL', 'twentynineteen' ),
  123. 'size' => 49.5,
  124. 'slug' => 'huge',
  125. ),
  126. )
  127. );
  128. // Editor color palette.
  129. add_theme_support(
  130. 'editor-color-palette',
  131. array(
  132. array(
  133. 'name' => 'default' === get_theme_mod( 'primary_color' ) ? __( 'Blue', 'twentynineteen' ) : null,
  134. 'slug' => 'primary',
  135. 'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 33 ),
  136. ),
  137. array(
  138. 'name' => 'default' === get_theme_mod( 'primary_color' ) ? __( 'Dark Blue', 'twentynineteen' ) : null,
  139. 'slug' => 'secondary',
  140. 'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 23 ),
  141. ),
  142. array(
  143. 'name' => __( 'Dark Gray', 'twentynineteen' ),
  144. 'slug' => 'dark-gray',
  145. 'color' => '#111',
  146. ),
  147. array(
  148. 'name' => __( 'Light Gray', 'twentynineteen' ),
  149. 'slug' => 'light-gray',
  150. 'color' => '#767676',
  151. ),
  152. array(
  153. 'name' => __( 'White', 'twentynineteen' ),
  154. 'slug' => 'white',
  155. 'color' => '#FFF',
  156. ),
  157. )
  158. );
  159. // Add support for responsive embedded content.
  160. add_theme_support( 'responsive-embeds' );
  161. }
  162. endif;
  163. add_action( 'after_setup_theme', 'twentynineteen_setup' );
  164. /**
  165. * Register widget area.
  166. *
  167. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  168. */
  169. function twentynineteen_widgets_init() {
  170. register_sidebar(
  171. array(
  172. 'name' => __( 'Footer', 'twentynineteen' ),
  173. 'id' => 'sidebar-1',
  174. 'description' => __( 'Add widgets here to appear in your footer.', 'twentynineteen' ),
  175. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  176. 'after_widget' => '</section>',
  177. 'before_title' => '<h2 class="widget-title">',
  178. 'after_title' => '</h2>',
  179. )
  180. );
  181. }
  182. add_action( 'widgets_init', 'twentynineteen_widgets_init' );
  183. /**
  184. * Set the content width in pixels, based on the theme's design and stylesheet.
  185. *
  186. * Priority 0 to make it available to lower priority callbacks.
  187. *
  188. * @global int $content_width Content width.
  189. */
  190. function twentynineteen_content_width() {
  191. // This variable is intended to be overruled from themes.
  192. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  193. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  194. $GLOBALS['content_width'] = apply_filters( 'twentynineteen_content_width', 640 );
  195. }
  196. add_action( 'after_setup_theme', 'twentynineteen_content_width', 0 );
  197. /**
  198. * Enqueue scripts and styles.
  199. */
  200. function twentynineteen_scripts() {
  201. wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  202. wp_style_add_data( 'twentynineteen-style', 'rtl', 'replace' );
  203. if ( has_nav_menu( 'menu-1' ) ) {
  204. wp_enqueue_script( 'twentynineteen-priority-menu', get_theme_file_uri( '/js/priority-menu.js' ), array(), '20181214', true );
  205. wp_enqueue_script( 'twentynineteen-touch-navigation', get_theme_file_uri( '/js/touch-keyboard-navigation.js' ), array(), '20181231', true );
  206. }
  207. wp_enqueue_style( 'twentynineteen-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  208. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  209. wp_enqueue_script( 'comment-reply' );
  210. }
  211. }
  212. add_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );
  213. /**
  214. * Fix skip link focus in IE11.
  215. *
  216. * This does not enqueue the script because it is tiny and because it is only for IE11,
  217. * thus it does not warrant having an entire dedicated blocking script being loaded.
  218. *
  219. * @link https://git.io/vWdr2
  220. */
  221. function twentynineteen_skip_link_focus_fix() {
  222. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  223. ?>
  224. <script>
  225. /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
  226. </script>
  227. <?php
  228. }
  229. add_action( 'wp_print_footer_scripts', 'twentynineteen_skip_link_focus_fix' );
  230. /**
  231. * Enqueue supplemental block editor styles.
  232. */
  233. function twentynineteen_editor_customizer_styles() {
  234. wp_enqueue_style( 'twentynineteen-editor-customizer-styles', get_theme_file_uri( '/style-editor-customizer.css' ), false, '1.1', 'all' );
  235. if ( 'custom' === get_theme_mod( 'primary_color' ) ) {
  236. // Include color patterns.
  237. require_once get_parent_theme_file_path( '/inc/color-patterns.php' );
  238. wp_add_inline_style( 'twentynineteen-editor-customizer-styles', twentynineteen_custom_colors_css() );
  239. }
  240. }
  241. add_action( 'enqueue_block_editor_assets', 'twentynineteen_editor_customizer_styles' );
  242. /**
  243. * Display custom color CSS in customizer and on frontend.
  244. */
  245. function twentynineteen_colors_css_wrap() {
  246. // Only include custom colors in customizer or frontend.
  247. if ( ( ! is_customize_preview() && 'default' === get_theme_mod( 'primary_color', 'default' ) ) || is_admin() ) {
  248. return;
  249. }
  250. require_once get_parent_theme_file_path( '/inc/color-patterns.php' );
  251. $primary_color = 199;
  252. if ( 'default' !== get_theme_mod( 'primary_color', 'default' ) ) {
  253. $primary_color = get_theme_mod( 'primary_color_hue', 199 );
  254. }
  255. ?>
  256. <style type="text/css" id="custom-theme-colors" <?php echo is_customize_preview() ? 'data-hue="' . absint( $primary_color ) . '"' : ''; ?>>
  257. <?php echo twentynineteen_custom_colors_css(); ?>
  258. </style>
  259. <?php
  260. }
  261. add_action( 'wp_head', 'twentynineteen_colors_css_wrap' );
  262. /**
  263. * SVG Icons class.
  264. */
  265. require get_template_directory() . '/classes/class-twentynineteen-svg-icons.php';
  266. /**
  267. * Custom Comment Walker template.
  268. */
  269. require get_template_directory() . '/classes/class-twentynineteen-walker-comment.php';
  270. /**
  271. * Enhance the theme by hooking into WordPress.
  272. */
  273. require get_template_directory() . '/inc/template-functions.php';
  274. /**
  275. * SVG Icons related functions.
  276. */
  277. require get_template_directory() . '/inc/icon-functions.php';
  278. /**
  279. * Custom template tags for the theme.
  280. */
  281. require get_template_directory() . '/inc/template-tags.php';
  282. /**
  283. * Customizer additions.
  284. */
  285. require get_template_directory() . '/inc/customizer.php';