functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * components functions and definitions.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Lodestar
  8. */
  9. if ( ! function_exists( 'lodestar_setup' ) ) :
  10. /**
  11. * Sets up theme defaults and registers support for various WordPress features.
  12. *
  13. * Note that this function is hooked into the aftercomponentsetup_theme hook, which
  14. * runs before the init hook. The init hook is too late for some features, such
  15. * as indicating support for post thumbnails.
  16. */
  17. function lodestar_setup() {
  18. /*
  19. * Make theme available for translation.
  20. * Translations can be filed in the /languages/ directory.
  21. * If you're building a theme based on components, use a find and replace
  22. * to change 'lodestar' to the name of your theme in all the template files.
  23. */
  24. load_theme_textdomain( 'lodestar', get_template_directory() . '/languages' );
  25. // Add default posts and comments RSS feed links to head.
  26. add_theme_support( 'automatic-feed-links' );
  27. /*
  28. * Let WordPress manage the document title.
  29. * By adding theme support, we declare that this theme does not use a
  30. * hard-coded <title> tag in the document head, and expect WordPress to
  31. * provide it for us.
  32. */
  33. add_theme_support( 'title-tag' );
  34. /*
  35. * Enable support for Post Thumbnails on posts and pages.
  36. *
  37. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  38. */
  39. add_theme_support( 'post-thumbnails' );
  40. add_image_size( 'lodestar-featured-image', 2000, 1200, true );
  41. add_image_size( 'lodestar-featured-archive-image', 700, 9999 );
  42. add_image_size( 'lodestar-portfolio-image', 2000, 9999 );
  43. add_image_size( 'lodestar-portfolio-archive-image', 900, 600, true );
  44. add_image_size( 'lodestar-thumbnail-avatar', 100, 100, true );
  45. add_image_size( 'lodestar-logo', 2400, 400 );
  46. // This theme uses wp_nav_menu() in one location.
  47. register_nav_menus(
  48. array(
  49. 'top' => esc_html__( 'Top', 'lodestar' ),
  50. )
  51. );
  52. /*
  53. * Switch default core markup for search form, comment form, and comments
  54. * to output valid HTML5.
  55. */
  56. add_theme_support(
  57. 'html5',
  58. array(
  59. 'search-form',
  60. 'comment-form',
  61. 'comment-list',
  62. 'gallery',
  63. 'caption',
  64. )
  65. );
  66. // Set up the WordPress core custom background feature.
  67. add_theme_support(
  68. 'custom-background',
  69. apply_filters(
  70. 'lodestar_custom_background_args',
  71. array(
  72. 'default-color' => 'ffffff',
  73. 'default-image' => '',
  74. )
  75. )
  76. );
  77. // Add support to selectively refresh widgets in Customizer
  78. add_theme_support( 'customize-selective-refresh-widgets' );
  79. // Add theme support for Custom Logo.
  80. add_theme_support(
  81. 'custom-logo',
  82. array(
  83. 'width' => 2400,
  84. 'height' => 400,
  85. 'flex-width' => true,
  86. 'flex-height' => true,
  87. 'header-text' => array( 'site-title', 'site-description' ),
  88. )
  89. );
  90. // Add support for responsive embeds.
  91. add_theme_support( 'responsive-embeds' );
  92. // Add support for full and wide align images.
  93. add_theme_support( 'align-wide' );
  94. }
  95. endif;
  96. add_action( 'after_setup_theme', 'lodestar_setup' );
  97. /**
  98. * Set the content width in pixels, based on the theme's design and stylesheet.
  99. *
  100. * Priority 0 to make it available to lower priority callbacks.
  101. *
  102. * @global int $content_width
  103. */
  104. function lodestar_content_width() {
  105. $GLOBALS['content_width'] = apply_filters( 'lodestar_content_width', 700 );
  106. }
  107. add_action( 'after_setup_theme', 'lodestar_content_width', 0 );
  108. /**
  109. * Custom function that modifies the_custom_logo output slightly
  110. */
  111. function lodestar_the_custom_logo() {
  112. if ( has_custom_logo() ) {
  113. $custom_logo_id = get_theme_mod( 'custom_logo' );
  114. $custom_logo_attr = array(
  115. 'class' => 'site-logo',
  116. 'itemprop' => 'logo',
  117. );
  118. printf(
  119. '<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>',
  120. esc_url( home_url( '/' ) ),
  121. wp_get_attachment_image( $custom_logo_id, 'lodestar-logo', false, $custom_logo_attr )
  122. );
  123. }
  124. }
  125. /**
  126. * Register custom fonts
  127. */
  128. function lodestar_fonts_url() {
  129. $fonts_url = '';
  130. /* Translators: If there are characters in your language that are not
  131. * supported by Work Sans, translate this to 'off'. Do not translate
  132. * into your own language.
  133. */
  134. $work_sans = esc_html_x( 'on', 'work_sans font: on or off', 'lodestar' );
  135. /* Translators: If there are characters in your language that are not
  136. * supported by Karla, translate this to 'off'. Do not translate
  137. * into your own language.
  138. */
  139. $karla = esc_html_x( 'on', 'Karla font: on or off', 'lodestar' );
  140. if ( 'off' !== $work_sans || 'off' !== $karla ) {
  141. $font_families = array();
  142. if ( 'off' !== $work_sans ) {
  143. $font_families[] = 'Work Sans:800';
  144. }
  145. if ( 'off' !== $karla ) {
  146. $font_families[] = 'Karla:400,400italic,700,700italic';
  147. }
  148. /**
  149. * A filter to enable child themes to add/change/omit font families.
  150. *
  151. * @param array $font_families An array of font families to be imploded for the Google Font API
  152. */
  153. $font_families = apply_filters( 'included_google_font_families', $font_families );
  154. $query_args = array(
  155. 'family' => urlencode( implode( '|', $font_families ) ),
  156. 'subset' => urlencode( 'latin,latin-ext' ),
  157. );
  158. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  159. }
  160. return esc_url_raw( $fonts_url );
  161. }
  162. /**
  163. * Register widget area.
  164. *
  165. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  166. */
  167. function lodestar_widgets_init() {
  168. register_sidebar(
  169. array(
  170. 'name' => esc_html__( 'Sidebar', 'lodestar' ),
  171. 'id' => 'sidebar-1',
  172. 'description' => '',
  173. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  174. 'after_widget' => '</section>',
  175. 'before_title' => '<h2 class="widget-title">',
  176. 'after_title' => '</h2>',
  177. )
  178. );
  179. register_sidebar(
  180. array(
  181. 'name' => esc_html__( 'Footer 1', 'lodestar' ),
  182. 'id' => 'sidebar-2',
  183. 'description' => '',
  184. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  185. 'after_widget' => '</section>',
  186. 'before_title' => '<h2 class="widget-title">',
  187. 'after_title' => '</h2>',
  188. )
  189. );
  190. register_sidebar(
  191. array(
  192. 'name' => esc_html__( 'Footer 2', 'lodestar' ),
  193. 'id' => 'sidebar-3',
  194. 'description' => '',
  195. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  196. 'after_widget' => '</section>',
  197. 'before_title' => '<h2 class="widget-title">',
  198. 'after_title' => '</h2>',
  199. )
  200. );
  201. register_sidebar(
  202. array(
  203. 'name' => esc_html__( 'Footer 3', 'lodestar' ),
  204. 'id' => 'sidebar-4',
  205. 'description' => '',
  206. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  207. 'after_widget' => '</section>',
  208. 'before_title' => '<h2 class="widget-title">',
  209. 'after_title' => '</h2>',
  210. )
  211. );
  212. // echo esc_attr( lodestar_widget_column_class( 'sidebar-1' ) );
  213. }
  214. add_action( 'widgets_init', 'lodestar_widgets_init' );
  215. /**
  216. * Wrap avatars in div for easier styling
  217. */
  218. function lodestar_get_avatar( $avatar ) {
  219. if ( ! is_admin() ) {
  220. $avatar = '<span class="avatar-container">' . $avatar . '</span>';
  221. }
  222. return $avatar;
  223. }
  224. add_filter( 'get_avatar', 'lodestar_get_avatar', 10, 5 );
  225. /**
  226. * Use front-page.php when Front page displayes is set to a static page.
  227. *
  228. * @param string $template front-page.php.
  229. *
  230. * @return string The template to be used: blank if is_home() is true (defaults to index.php), else $template.
  231. */
  232. function lodestar_front_page_template( $template ) {
  233. return is_home() ? '' : $template;
  234. }
  235. add_filter( 'frontpage_template', 'lodestar_front_page_template' );
  236. /**
  237. * Enqueue scripts and styles.
  238. */
  239. function lodestar_scripts() {
  240. wp_enqueue_style( 'lodestar-style', get_stylesheet_uri() );
  241. // Theme block stylesheet.
  242. wp_enqueue_style( 'lodestar-block-style', get_theme_file_uri( '/assets/css/blocks.css' ), array( 'lodestar-style' ), '1.0' );
  243. wp_enqueue_style( 'lodestar_fonts_url', lodestar_fonts_url(), array(), null );
  244. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/assets/fonts/genericons.css', array(), null );
  245. wp_enqueue_script( 'lodestar-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), '20151215', true );
  246. wp_enqueue_script( 'lodestar-global', get_template_directory_uri() . '/assets/js/global.js', array( 'jquery' ), '20151215', true );
  247. wp_enqueue_script( 'lodestar-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151215', true );
  248. // Scroll effects (only loaded on front page in Customizer)
  249. if ( lodestar_is_frontpage() && is_customize_preview() ) :
  250. wp_enqueue_script( 'scrollTo', get_template_directory_uri() . '/assets/js/jquery.scrollTo.js', array( 'jquery' ), '20151030', true );
  251. endif;
  252. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  253. wp_enqueue_script( 'comment-reply' );
  254. }
  255. // Portfolio navigation (only loaded on Portfolio Template)
  256. if ( is_page_template( 'templates/portfolio-page.php' ) ) :
  257. wp_enqueue_script( 'lazyload', get_template_directory_uri() . '/assets/js/jquery.lazyload.js', array( 'jquery' ), '20160719', true );
  258. wp_enqueue_script( 'isotope', get_template_directory_uri() . '/assets/js/isotope.pkgd.js', array( 'jquery' ), '20160719', true );
  259. wp_enqueue_script( 'lodestar-portfolio-page', get_template_directory_uri() . '/assets/js/portfolio-page.js', array( 'jquery', 'isotope', 'lazyload' ), '20160719', true );
  260. endif;
  261. }
  262. add_action( 'wp_enqueue_scripts', 'lodestar_scripts' );
  263. /**
  264. * Enqueue editor styles for Gutenberg
  265. */
  266. function lodestar_block_editor_styles() {
  267. // Block styles.
  268. wp_enqueue_style( 'lodestar-block-editor-style', get_theme_file_uri( '/assets/css/editor-blocks.css' ) );
  269. // Fonts.
  270. wp_enqueue_style( 'lodestar-fonts-url', lodestar_fonts_url(), array(), null );
  271. }
  272. add_action( 'enqueue_block_editor_assets', 'lodestar_block_editor_styles' );
  273. /**
  274. * Implement the Custom Header feature.
  275. */
  276. require get_template_directory() . '/inc/custom-header.php';
  277. /**
  278. * Custom template tags for this theme.
  279. */
  280. require get_template_directory() . '/inc/template-tags.php';
  281. /**
  282. * Custom functions that act independently of the theme templates.
  283. */
  284. require get_template_directory() . '/inc/extras.php';
  285. /**
  286. * Customizer additions.
  287. */
  288. require get_template_directory() . '/inc/customizer.php';
  289. /**
  290. * Load Jetpack compatibility file.
  291. */
  292. require get_template_directory() . '/inc/jetpack.php';
  293. /**
  294. * Load WooCommerce compatibility file.
  295. */
  296. if ( class_exists( 'WooCommerce' ) ) {
  297. require( get_template_directory() . '/inc/woocommerce.php' );
  298. }