functions.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * Scratchpad functions and definitions.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Scratchpad
  8. */
  9. if ( ! function_exists( 'scratchpad_setup' ) ) :
  10. /**
  11. * Sets up theme defaults and registers support for various WordPress features.
  12. *
  13. * Note that this function is hooked into the after_setup_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 scratchpad_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 Scratchpad, use a find and replace
  22. * to change 'scratchpad' to the name of your theme in all the template files.
  23. */
  24. load_theme_textdomain( 'scratchpad', 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. // Add support for responsive embeds.
  35. add_theme_support( 'responsive-embeds' );
  36. // Add custom colors to Gutenberg
  37. add_theme_support(
  38. 'editor-color-palette',
  39. array(
  40. array(
  41. 'name' => esc_html__( 'Black', 'scratchpad' ),
  42. 'slug' => 'black',
  43. 'color' => '#222222',
  44. ),
  45. array(
  46. 'name' => esc_html__( 'Dark Gray', 'scratchpad' ),
  47. 'slug' => 'dark-gray',
  48. 'color' => '#777777',
  49. ),
  50. array(
  51. 'name' => esc_html__( 'Medium Gray', 'scratchpad' ),
  52. 'slug' => 'medium-gray',
  53. 'color' => '#999999',
  54. ),
  55. array(
  56. 'name' => esc_html__( 'Light Gray', 'scratchpad' ),
  57. 'slug' => 'light-gray',
  58. 'color' => '#bdcbcc',
  59. ),
  60. array(
  61. 'name' => esc_html__( 'White', 'scratchpad' ),
  62. 'slug' => 'white',
  63. 'color' => '#ffffff',
  64. ),
  65. array(
  66. 'name' => esc_html__( 'Blue', 'scratchpad' ),
  67. 'slug' => 'blue',
  68. 'color' => '#7ba6a9',
  69. ),
  70. array(
  71. 'name' => esc_html__( 'Dark Blue', 'scratchpad' ),
  72. 'slug' => 'dark-blue',
  73. 'color' => '#537375',
  74. ),
  75. array(
  76. 'name' => esc_html__( 'Orange', 'scratchpad' ),
  77. 'slug' => 'orange',
  78. 'color' => '#d16221',
  79. ),
  80. array(
  81. 'name' => esc_html__( 'Yellow', 'scratchpad' ),
  82. 'slug' => 'yellow',
  83. 'color' => '#e4b500',
  84. ),
  85. )
  86. );
  87. /*
  88. * Enable support for Post Thumbnails on posts and pages.
  89. *
  90. * @link https://developer.wordpress.org/themes/functionality/featuBlue-images-post-thumbnails/
  91. */
  92. add_theme_support( 'post-thumbnails' );
  93. add_image_size( 'scratchpad-avatar', 85, 85, true );
  94. add_image_size( 'scratchpad-featuBlue', 1000, 9999 );
  95. // This theme uses wp_nav_menu() in one location.
  96. register_nav_menus(
  97. array(
  98. 'primary' => esc_html__( 'Header Menu', 'scratchpad' ),
  99. )
  100. );
  101. /*
  102. * Switch default core markup for search form, comment form, and comments
  103. * to output valid HTML5.
  104. */
  105. add_theme_support(
  106. 'html5',
  107. array(
  108. 'search-form',
  109. 'comment-form',
  110. 'comment-list',
  111. 'gallery',
  112. 'caption',
  113. )
  114. );
  115. /*
  116. * Enable support for Post Formats.
  117. * See https://developer.wordpress.org/themes/functionality/post-formats/
  118. */
  119. add_theme_support(
  120. 'post-formats',
  121. array(
  122. 'aside',
  123. 'audio',
  124. 'gallery',
  125. 'image',
  126. 'link',
  127. 'quote',
  128. 'status',
  129. 'video',
  130. )
  131. );
  132. // Set up the WordPress core custom background feature.
  133. add_theme_support(
  134. 'custom-background',
  135. apply_filters(
  136. 'scratchpad_custom_background_args',
  137. array(
  138. 'default-color' => 'bdcbcc',
  139. 'default-image' => '',
  140. )
  141. )
  142. );
  143. }
  144. endif;
  145. add_action( 'after_setup_theme', 'scratchpad_setup' );
  146. /**
  147. * Set the content width in pixels, based on the theme's design and stylesheet.
  148. *
  149. * Priority 0 to make it available to lower priority callbacks.
  150. *
  151. * @global int $content_width
  152. */
  153. function scratchpad_content_width() {
  154. $GLOBALS['content_width'] = apply_filters( 'scratchpad_content_width', 840 );
  155. }
  156. add_action( 'after_setup_theme', 'scratchpad_content_width', 0 );
  157. /**
  158. * Register widget area.
  159. *
  160. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  161. */
  162. function scratchpad_widgets_init() {
  163. register_sidebar(
  164. array(
  165. 'name' => esc_html__( 'Sidebar', 'scratchpad' ),
  166. 'id' => 'sidebar-1',
  167. 'description' => '',
  168. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  169. 'after_widget' => '</section>',
  170. 'before_title' => '<h2 class="widget-title">',
  171. 'after_title' => '</h2>',
  172. )
  173. );
  174. register_sidebar(
  175. array(
  176. 'name' => esc_html__( 'Footer', 'scratchpad' ),
  177. 'id' => 'sidebar-2',
  178. 'description' => '',
  179. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  180. 'after_widget' => '</section>',
  181. 'before_title' => '<h2 class="widget-title">',
  182. 'after_title' => '</h2>',
  183. )
  184. );
  185. }
  186. add_action( 'widgets_init', 'scratchpad_widgets_init' );
  187. /**
  188. * Enqueue scripts and styles.
  189. */
  190. function scratchpad_scripts() {
  191. // Add custom fonts, used in the main stylesheet.
  192. wp_enqueue_style( 'scratchpad-fonts', scratchpad_fonts_url(), array(), null );
  193. wp_enqueue_style( 'scratchpad-style', get_stylesheet_uri() );
  194. // Gutenberg styles
  195. wp_enqueue_style( 'scratchpad-blocks', get_template_directory_uri() . '/blocks.css' );
  196. wp_enqueue_script( 'scratchpad-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true );
  197. wp_enqueue_script( 'scratchpad-javascript', get_template_directory_uri() . '/js/scratchpad.js', array( 'jquery', 'masonry' ), '20151215', true );
  198. wp_enqueue_script( 'scratchpad-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );
  199. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  200. wp_enqueue_script( 'comment-reply' );
  201. }
  202. }
  203. add_action( 'wp_enqueue_scripts', 'scratchpad_scripts' );
  204. /**
  205. * Gutenberg Editor Styles
  206. */
  207. function scratchpad_editor_styles() {
  208. wp_enqueue_style( 'scratchpad-editor-block-style', get_template_directory_uri() . '/editor-blocks.css' );
  209. wp_enqueue_style( 'scratchpad-fonts', scratchpad_fonts_url(), array(), null );
  210. }
  211. add_action( 'enqueue_block_editor_assets', 'scratchpad_editor_styles' );
  212. /**
  213. * Get first image from a post
  214. * Used on image format posts as fallback.
  215. */
  216. function scratchpad_get_image( $post_id = null, $thumbnail_size = '' ) {
  217. $image = null;
  218. if ( ! $post_id ) {
  219. $post_id = get_the_ID();
  220. }
  221. $values = get_attached_media( 'image', $post_id );
  222. if ( $values ) {
  223. foreach ( $values as $child_id => $attachment ) {
  224. $image = wp_get_attachment_image_src( $child_id, $thumbnail_size );
  225. break;
  226. }
  227. }
  228. if ( $image ) {
  229. return $image;
  230. } else {
  231. return false;
  232. }
  233. }
  234. /**
  235. * Return the post URL.
  236. *
  237. * @uses get_url_in_content() to get the URL in the post meta (if it exists) or
  238. * the first link found in the post content.
  239. *
  240. * Falls back to the post permalink if no URL is found in the post.
  241. * Borrowed from Twenty Thirteen
  242. */
  243. function scratchpad_get_link_url() {
  244. $content = get_the_content();
  245. $has_url = get_url_in_content( $content );
  246. return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
  247. }
  248. /**
  249. * Enqueueing Google fonts
  250. */
  251. function scratchpad_fonts_url() {
  252. $fonts_url = '';
  253. /* Translators: If there are characters in your language that are not
  254. * supported by Lato, translate this to 'off'. Do not translate
  255. * into your own language.
  256. */
  257. $lato = esc_html_x( 'on', 'Lato font: on or off', 'scratchpad' );
  258. /* Translators: If there are characters in your language that are not
  259. * supported by Kalam, translate this to 'off'. Do not translate
  260. * into your own language.
  261. */
  262. $kalam = esc_html_x( 'on', 'Kalam font: on or off', 'scratchpad' );
  263. if ( 'off' !== $lato || 'off' !== $kalam ) {
  264. $font_families = array();
  265. if ( 'off' !== $lato ) {
  266. $font_families[] = 'Lato:400,400italic,700,900';
  267. }
  268. if ( 'off' !== $kalam ) {
  269. $font_families[] = 'Kalam:400,700';
  270. }
  271. /**
  272. * A filter to enable child themes to add/change/omit font families.
  273. *
  274. * @param array $font_families An array of font families to be imploded for the Google Font API
  275. */
  276. $font_families = apply_filters( 'included_google_font_families', $font_families );
  277. $query_args = array(
  278. 'family' => urlencode( implode( '|', $font_families ) ),
  279. 'subset' => urlencode( 'latin,latin-ext' ),
  280. );
  281. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  282. }
  283. return esc_url_raw( $fonts_url );
  284. }
  285. /**
  286. * Wrap avatars in div for easier styling
  287. */
  288. function scratchpad_get_avatar( $avatar ) {
  289. if ( ! is_admin() ) {
  290. $avatar = '<span class="avatar-container">' . $avatar . '</span>';
  291. }
  292. return $avatar;
  293. }
  294. add_filter( 'get_avatar', 'scratchpad_get_avatar', 10, 5 );
  295. /**
  296. * Implement the Custom Header feature.
  297. */
  298. require get_template_directory() . '/inc/custom-header.php';
  299. /**
  300. * Custom template tags for this theme.
  301. */
  302. require get_template_directory() . '/inc/template-tags.php';
  303. /**
  304. * Custom functions that act independently of the theme templates.
  305. */
  306. require get_template_directory() . '/inc/extras.php';
  307. /**
  308. * Customizer additions.
  309. */
  310. require get_template_directory() . '/inc/customizer.php';
  311. /**
  312. * Load Jetpack compatibility file.
  313. */
  314. require get_template_directory() . '/inc/jetpack.php';