functions.php 8.9 KB

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