functions.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * Varia functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package WordPress
  8. * @subpackage Varia
  9. * @since 1.0.0
  10. */
  11. /**
  12. * Varia 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( 'varia_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 varia_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 Varia, use a find and replace
  31. * to change 'varia' to the name of your theme in all the template files.
  32. */
  33. load_theme_textdomain( 'varia', 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', 'varia' ),
  54. 'menu-2' => __( 'Footer Menu', 'varia' ),
  55. 'social' => __( 'Social Links Menu', 'varia' ),
  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. )
  71. );
  72. /**
  73. * Add support for core custom logo.
  74. *
  75. * @link https://codex.wordpress.org/Theme_Logo
  76. */
  77. add_theme_support(
  78. 'custom-logo',
  79. array(
  80. 'height' => 96,
  81. 'width' => 100,
  82. 'flex-width' => true,
  83. 'flex-height' => true,
  84. 'header-text' => array( 'site-title', 'site-description' ),
  85. )
  86. );
  87. // Add theme support for selective refresh for widgets.
  88. add_theme_support( 'customize-selective-refresh-widgets' );
  89. // Add support for Block Styles.
  90. add_theme_support( 'wp-block-styles' );
  91. // Add support for full and wide align images.
  92. add_theme_support( 'align-wide' );
  93. // Add support for editor styles.
  94. add_theme_support( 'editor-styles' );
  95. // Enqueue editor styles.
  96. add_editor_style( 'style-editor.css' );
  97. // add_editor_style( 'child-theme-alt-style.css' ); // this will get loaded in a child theme
  98. // Add custom editor font sizes.
  99. add_theme_support(
  100. 'editor-font-sizes',
  101. array(
  102. array(
  103. 'name' => __( 'Small', 'varia' ),
  104. 'shortName' => __( 'S', 'varia' ),
  105. 'size' => 15,
  106. 'slug' => 'small',
  107. ),
  108. array(
  109. 'name' => __( 'Normal', 'varia' ),
  110. 'shortName' => __( 'M', 'varia' ),
  111. 'size' => 18,
  112. 'slug' => 'normal',
  113. ),
  114. array(
  115. 'name' => __( 'Large', 'varia' ),
  116. 'shortName' => __( 'L', 'varia' ),
  117. 'size' => 25.92,
  118. 'slug' => 'large',
  119. ),
  120. array(
  121. 'name' => __( 'Huge', 'varia' ),
  122. 'shortName' => __( 'XL', 'varia' ),
  123. 'size' => 31.105,
  124. 'slug' => 'huge',
  125. ),
  126. )
  127. );
  128. /*
  129. * Get customizer colors and add them to the editor color palettes
  130. *
  131. * - if the customizer color is empty, use the default
  132. */
  133. $colors_array = get_theme_mod( 'colors_manager' ); // color annotations array()
  134. $primary = ! empty( $colors_array ) ? $colors_array['colors']['link'] : '#FF0000'; // $config-global--color-primary-default;
  135. $secondary = ! empty( $colors_array ) ? $colors_array['colors']['fg1'] : '#FFFFFF'; // $config-global--color-secondary-default;
  136. $foreground = ! empty( $colors_array ) ? $colors_array['colors']['txt'] : '#444444'; // $config-global--color-foreground-default;
  137. $background = ! empty( $colors_array ) ? $colors_array['colors']['bg'] : '#0000FF'; // $config-global--color-background-default;
  138. // Editor color palette.
  139. add_theme_support(
  140. 'editor-color-palette',
  141. array(
  142. array(
  143. 'name' => __( 'Primary', 'varia' ),
  144. 'slug' => 'primary',
  145. 'color' => $primary,
  146. ),
  147. array(
  148. 'name' => __( 'Secondary', 'varia' ),
  149. 'slug' => 'secondary',
  150. 'color' => $secondary,
  151. ),
  152. array(
  153. 'name' => __( 'Foreground', 'varia' ),
  154. 'slug' => 'foreground',
  155. 'color' => $foreground,
  156. ),
  157. array(
  158. 'name' => __( 'Background', 'varia' ),
  159. 'slug' => 'background',
  160. 'color' => $background,
  161. ),
  162. )
  163. );
  164. // Add support for responsive embedded content.
  165. add_theme_support( 'responsive-embeds' );
  166. // Add support for Global Styles.
  167. add_theme_support(
  168. 'jetpack-global-styles',
  169. [
  170. 'enable_theme_default' => true,
  171. ]
  172. );
  173. }
  174. endif;
  175. add_action( 'after_setup_theme', 'varia_setup' );
  176. /**
  177. * Register widget area.
  178. *
  179. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  180. */
  181. function varia_widgets_init() {
  182. register_sidebar(
  183. array(
  184. 'name' => __( 'Footer', 'varia' ),
  185. 'id' => 'sidebar-1',
  186. 'description' => __( 'Add widgets here to appear in your footer.', 'varia' ),
  187. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  188. 'after_widget' => '</section>',
  189. 'before_title' => '<h2 class="widget-title">',
  190. 'after_title' => '</h2>',
  191. )
  192. );
  193. }
  194. add_action( 'widgets_init', 'varia_widgets_init' );
  195. /**
  196. * Set the content width in pixels, based on the theme's design and stylesheet.
  197. *
  198. * Priority 0 to make it available to lower priority callbacks.
  199. *
  200. * @global int $content_width Content width.
  201. */
  202. function varia_content_width() {
  203. // This variable is intended to be overruled from themes.
  204. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  205. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  206. $GLOBALS['content_width'] = apply_filters( 'varia_content_width', 750 );
  207. }
  208. add_action( 'after_setup_theme', 'varia_content_width', 0 );
  209. /**
  210. * Enqueue scripts and styles.
  211. */
  212. function varia_scripts() {
  213. // Theme styles
  214. wp_enqueue_style( 'varia-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  215. // RTL styles
  216. wp_style_add_data( 'varia-style', 'rtl', 'replace' );
  217. // Print styles
  218. wp_enqueue_style( 'varia-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  219. // Threaded comment reply styles
  220. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  221. wp_enqueue_script( 'comment-reply' );
  222. }
  223. }
  224. add_action( 'wp_enqueue_scripts', 'varia_scripts' );
  225. /**
  226. * Fix skip link focus in IE11.
  227. *
  228. * This does not enqueue the script because it is tiny and because it is only for IE11,
  229. * thus it does not warrant having an entire dedicated blocking script being loaded.
  230. *
  231. * @link https://git.io/vWdr2
  232. */
  233. function varia_skip_link_focus_fix() {
  234. // Prevent outputting skip-link-focus-fix in AMP since the AMP framework has it built-in,
  235. // per <https://github.com/ampproject/amphtml/issues/18671>.
  236. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
  237. return;
  238. }
  239. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  240. ?>
  241. <script>
  242. /(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);
  243. </script>
  244. <?php
  245. }
  246. add_action( 'wp_print_footer_scripts', 'varia_skip_link_focus_fix' );
  247. /**
  248. * Enqueue block editor content-width styles.
  249. * - These need to be enqueued separately to include the selectors
  250. * that live outside of `.editor-styles-wrapper`
  251. *
  252. */
  253. function varia_editor_content_width() {
  254. wp_enqueue_style( 'varia-editor-content-width-style', get_theme_file_uri( '/style-editor-content-width.css' ), false, wp_get_theme()->get( 'Version' ), 'all' );
  255. }
  256. add_action( 'enqueue_block_editor_assets', 'varia_editor_content_width' );
  257. /**
  258. * SVG Icons class.
  259. */
  260. require get_template_directory() . '/classes/class-varia-svg-icons.php';
  261. /**
  262. * Custom Comment Walker template.
  263. */
  264. require get_template_directory() . '/classes/class-varia-walker-comment.php';
  265. /**
  266. * Enhance the theme by hooking into WordPress.
  267. */
  268. require get_template_directory() . '/inc/template-functions.php';
  269. /**
  270. * SVG Icons related functions.
  271. */
  272. require get_template_directory() . '/inc/icon-functions.php';
  273. /**
  274. * Custom template tags for the theme.
  275. */
  276. require get_template_directory() . '/inc/template-tags.php';
  277. /**
  278. * Load WooCommerce compatibility file.
  279. */
  280. if ( class_exists( 'WooCommerce' ) ) {
  281. require get_template_directory() . '/inc/woocommerce.php';
  282. }