functions.php 7.2 KB

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