functions.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Livro functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Livro
  8. * @since Livro 1.0
  9. */
  10. if ( ! function_exists( 'livro_support' ) ) :
  11. /**
  12. * Sets up theme defaults and registers support for various WordPress features.
  13. *
  14. * @since Livro 1.0
  15. *
  16. * @return void
  17. */
  18. function livro_support() {
  19. // Add support for block styles.
  20. add_theme_support( 'wp-block-styles' );
  21. // Enqueue editor styles.
  22. add_editor_style( 'style.css' );
  23. }
  24. endif;
  25. add_action( 'after_setup_theme', 'livro_support' );
  26. if ( ! function_exists( 'livro_styles' ) ) :
  27. /**
  28. * Enqueue styles.
  29. *
  30. * @since Livro 1.0
  31. *
  32. * @return void
  33. */
  34. function livro_styles() {
  35. // Register theme stylesheet.
  36. wp_register_style(
  37. 'livro-style',
  38. get_template_directory_uri() . '/style.css',
  39. array(),
  40. wp_get_theme()->get( 'Version' )
  41. );
  42. // Add styles inline.
  43. wp_add_inline_style( 'livro-style', livro_get_font_face_styles() );
  44. // Enqueue theme stylesheet.
  45. wp_enqueue_style( 'livro-style' );
  46. }
  47. endif;
  48. add_action( 'wp_enqueue_scripts', 'livro_styles' );
  49. if ( ! function_exists( 'livro_editor_styles' ) ) :
  50. /**
  51. * Enqueue editor styles.
  52. *
  53. * @since Livro 1.0
  54. *
  55. * @return void
  56. */
  57. function livro_editor_styles() {
  58. // Add styles inline.
  59. wp_add_inline_style( 'wp-block-library', livro_get_font_face_styles() );
  60. }
  61. endif;
  62. add_action( 'admin_init', 'livro_editor_styles' );
  63. if ( ! function_exists( 'livro_get_font_face_styles' ) ) :
  64. /**
  65. * Get font face styles.
  66. * Called by functions livro_styles() and livro_editor_styles() above.
  67. *
  68. * @since Livro 1.0
  69. *
  70. * @return string
  71. */
  72. function livro_get_font_face_styles() {
  73. return "
  74. @font-face{
  75. font-family: 'Newsreader';
  76. font-weight: 200 800;
  77. font-style: normal;
  78. font-stretch: normal;
  79. font-display: swap;
  80. src: url('" . get_theme_file_uri( 'assets/fonts/Newsreader.woff2' ) . "') format('woff2');
  81. }
  82. @font-face{
  83. font-family: 'Newsreader';
  84. font-weight: 200 800;
  85. font-style: italic;
  86. font-stretch: normal;
  87. font-display: swap;
  88. src: url('" . get_theme_file_uri( 'assets/fonts/Newsreader-italic.woff2' ) . "') format('woff2');
  89. }
  90. ";
  91. }
  92. endif;
  93. if ( ! function_exists( 'livro_preload_webfonts' ) ) :
  94. /**
  95. * Preloads the main web font to improve performance.
  96. *
  97. * Only the main web font (font-style: normal) is preloaded here since that font is always relevant (it is used
  98. * on every heading, for example). The other font is only needed if there is any applicable content in italic style,
  99. * and therefore preloading it would in most cases regress performance when that font would otherwise not be loaded
  100. * at all.
  101. *
  102. * @since Livro 1.0
  103. *
  104. * @return void
  105. */
  106. function livro_preload_webfonts() {
  107. ?>
  108. <link rel="preload" href="<?php echo esc_url( get_theme_file_uri( 'assets/fonts/assets/fonts/Newsreader.woff2' ) ); ?>" as="font" type="font/woff2" crossorigin>
  109. <link rel="preload" href="<?php echo esc_url( get_theme_file_uri( 'assets/fonts/assets/fonts/Newsreader-italic.woff2' ) ); ?>" as="font" type="font/woff2" crossorigin>
  110. <?php
  111. }
  112. endif;
  113. add_action( 'wp_head', 'livro_preload_webfonts' );