functions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. // Theme setup
  3. add_action( 'after_setup_theme', 'hemingway_setup' );
  4. function hemingway_setup() {
  5. // Automatic feed
  6. add_theme_support( 'automatic-feed-links' );
  7. // Custom background
  8. add_theme_support( 'custom-background' );
  9. // Post thumbnails
  10. add_theme_support( 'post-thumbnails' );
  11. add_image_size( 'post-image', 676, 9999 );
  12. // Post formats
  13. add_theme_support( 'post-formats', array( 'video', 'aside', 'quote' ) );
  14. // Custom header
  15. $args = array(
  16. 'width' => 1280,
  17. 'height' => 416,
  18. 'default-image' => get_template_directory_uri() . '/images/header.jpg',
  19. 'uploads' => true,
  20. 'header-text' => false
  21. );
  22. add_theme_support( 'custom-header', $args );
  23. // Title tag
  24. add_theme_support( 'title-tag' );
  25. // Add nav menu
  26. register_nav_menu( 'primary', 'Primary Menu' );
  27. // Make the theme translation ready
  28. load_theme_textdomain('hemingway', get_template_directory() . '/languages');
  29. $locale = get_locale();
  30. $locale_file = get_template_directory() . "/languages/$locale.php";
  31. if ( is_readable($locale_file) )
  32. require_once($locale_file);
  33. }
  34. // Enqueue Javascript files
  35. function hemingway_load_javascript_files() {
  36. if ( !is_admin() ) {
  37. wp_enqueue_script( 'hemingway_global', get_template_directory_uri().'/js/global.js', array('jquery'), '', true );
  38. if ( is_singular() ) wp_enqueue_script( "comment-reply" );
  39. }
  40. }
  41. add_action( 'wp_enqueue_scripts', 'hemingway_load_javascript_files' );
  42. // Enqueue styles
  43. function hemingway_load_style() {
  44. if ( !is_admin() ) {
  45. wp_enqueue_style( 'hemingway_googleFonts', '//fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Raleway:700,400' );
  46. wp_enqueue_style( 'hemingway_style', get_stylesheet_uri() );
  47. }
  48. }
  49. add_action('wp_print_styles', 'hemingway_load_style');
  50. // Add editor styles
  51. function hemingway_add_editor_styles() {
  52. add_editor_style( 'hemingway-editor-style.css' );
  53. $font_url = '//fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Raleway:700,400';
  54. add_editor_style( str_replace( ',', '%2C', $font_url ) );
  55. }
  56. add_action( 'init', 'hemingway_add_editor_styles' );
  57. // Add footer widget areas
  58. add_action( 'widgets_init', 'hemingway_sidebar_reg' );
  59. function hemingway_sidebar_reg() {
  60. register_sidebar(array(
  61. 'name' => __( 'Footer A', 'hemingway' ),
  62. 'id' => 'footer-a',
  63. 'description' => __( 'Widgets in this area will be shown in the left column in the footer.', 'hemingway' ),
  64. 'before_title' => '<h3 class="widget-title">',
  65. 'after_title' => '</h3>',
  66. 'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
  67. 'after_widget' => '</div><div class="clear"></div></div>'
  68. ));
  69. register_sidebar(array(
  70. 'name' => __( 'Footer B', 'hemingway' ),
  71. 'id' => 'footer-b',
  72. 'description' => __( 'Widgets in this area will be shown in the middle column in the footer.', 'hemingway' ),
  73. 'before_title' => '<h3 class="widget-title">',
  74. 'after_title' => '</h3>',
  75. 'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
  76. 'after_widget' => '</div><div class="clear"></div></div>'
  77. ));
  78. register_sidebar(array(
  79. 'name' => __( 'Footer C', 'hemingway' ),
  80. 'id' => 'footer-c',
  81. 'description' => __( 'Widgets in this area will be shown in the right column in the footer.', 'hemingway' ),
  82. 'before_title' => '<h3 class="widget-title">',
  83. 'after_title' => '</h3>',
  84. 'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
  85. 'after_widget' => '</div><div class="clear"></div></div>'
  86. ));
  87. register_sidebar(array(
  88. 'name' => __( 'Sidebar', 'hemingway' ),
  89. 'id' => 'sidebar',
  90. 'description' => __( 'Widgets in this area will be shown in the sidebar.', 'hemingway' ),
  91. 'before_title' => '<h3 class="widget-title">',
  92. 'after_title' => '</h3>',
  93. 'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
  94. 'after_widget' => '</div><div class="clear"></div></div>'
  95. ));
  96. }
  97. // Add theme widgets
  98. require_once (get_template_directory() . "/widgets/dribbble-widget.php");
  99. require_once (get_template_directory() . "/widgets/flickr-widget.php");
  100. require_once (get_template_directory() . "/widgets/video-widget.php");
  101. require_once (get_template_directory() . "/widgets/social-widget.php");
  102. // Set content-width
  103. if ( ! isset( $content_width ) ) $content_width = 676;
  104. // Add classes to next_posts_link and previous_posts_link
  105. add_filter('next_posts_link_attributes', 'hemingway_posts_link_attributes_1');
  106. add_filter('previous_posts_link_attributes', 'hemingway_posts_link_attributes_2');
  107. function hemingway_posts_link_attributes_1() {
  108. return 'class="post-nav-older"';
  109. }
  110. function hemingway_posts_link_attributes_2() {
  111. return 'class="post-nav-newer"';
  112. }
  113. // Menu walker adding "has-children" class to menu li's with children menu items
  114. class hemingway_nav_walker extends Walker_Nav_Menu {
  115. function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  116. $id_field = $this->db_fields['id'];
  117. if ( !empty( $children_elements[ $element->$id_field ] ) ) {
  118. $element->classes[] = 'has-children';
  119. }
  120. Walker_Nav_Menu::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  121. }
  122. }
  123. // Add class to body if the post/page has a featured image
  124. add_action('body_class', 'hemingway_if_featured_image_class' );
  125. function hemingway_if_featured_image_class($classes) {
  126. global $post;
  127. if ( isset( $post ) && has_post_thumbnail() ) {
  128. $classes[] = 'has-featured-image';
  129. }
  130. return $classes;
  131. }
  132. // Custom more-link text
  133. add_filter( 'the_content_more_link', 'hemingway_custom_more_link', 10, 2 );
  134. function hemingway_custom_more_link( $more_link, $more_link_text ) {
  135. return str_replace( $more_link_text, __('Continue reading', 'hemingway'), $more_link );
  136. }
  137. // Style the admin area
  138. function hemingway_custom_colors() {
  139. echo '<style type="text/css">
  140. #postimagediv #set-post-thumbnail img {
  141. max-width: 100%;
  142. height: auto;
  143. }
  144. </style>';
  145. }
  146. add_action('admin_head', 'hemingway_custom_colors');
  147. // hemingway comment function
  148. if ( ! function_exists( 'hemingway_comment' ) ) :
  149. function hemingway_comment( $comment, $args, $depth ) {
  150. $GLOBALS['comment'] = $comment;
  151. switch ( $comment->comment_type ) :
  152. case 'pingback' :
  153. case 'trackback' :
  154. ?>
  155. <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
  156. <?php __( 'Pingback:', 'hemingway' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'hemingway' ), '<span class="edit-link">', '</span>' ); ?>
  157. </li>
  158. <?php
  159. break;
  160. default :
  161. global $post;
  162. ?>
  163. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  164. <div id="comment-<?php comment_ID(); ?>" class="comment">
  165. <div class="comment-meta comment-author vcard">
  166. <?php echo get_avatar( $comment, 120 ); ?>
  167. <div class="comment-meta-content">
  168. <?php printf( '<cite class="fn">%1$s %2$s</cite>',
  169. get_comment_author_link(),
  170. ( $comment->user_id === $post->post_author ) ? '<span class="post-author"> ' . __( '(Post author)', 'hemingway' ) . '</span>' : ''
  171. ); ?>
  172. <p><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>"><?php echo get_comment_date() . ' at ' . get_comment_time() ?></a></p>
  173. </div> <!-- /comment-meta-content -->
  174. </div> <!-- /comment-meta -->
  175. <div class="comment-content post-content">
  176. <?php if ( '0' == $comment->comment_approved ) : ?>
  177. <p class="comment-awaiting-moderation"><?php _e( 'Awaiting moderation', 'hemingway' ); ?></p>
  178. <?php endif; ?>
  179. <?php comment_text(); ?>
  180. <div class="comment-actions">
  181. <?php edit_comment_link( __( 'Edit', 'hemingway' ), '', '' ); ?>
  182. <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'hemingway' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  183. <div class="clear"></div>
  184. </div> <!-- /comment-actions -->
  185. </div><!-- /comment-content -->
  186. </div><!-- /comment-## -->
  187. <?php
  188. break;
  189. endswitch;
  190. }
  191. endif;
  192. // Hemingway theme options
  193. class Hemingway_Customize {
  194. public static function register ( $wp_customize ) {
  195. //1. Define a new section (if desired) to the Theme Customizer
  196. $wp_customize->add_section( 'hemingway_options',
  197. array(
  198. 'title' => __( 'Hemingway Options', 'hemingway' ), //Visible title of section
  199. 'priority' => 35, //Determines what order this appears in
  200. 'capability' => 'edit_theme_options', //Capability needed to tweak
  201. 'description' => __('Allows you to customize some settings for Hemingway.', 'hemingway'), //Descriptive tooltip
  202. )
  203. );
  204. $wp_customize->add_section( 'hemingway_logo_section' , array(
  205. 'title' => __( 'Logo', 'hemingway' ),
  206. 'priority' => 40,
  207. 'description' => __('Upload a logo to replace the default site name and description in the header','hemingway'),
  208. ) );
  209. //2. Register new settings to the WP database...
  210. $wp_customize->add_setting( 'accent_color', //No need to use a SERIALIZED name, as `theme_mod` settings already live under one db record
  211. array(
  212. 'default' => '#1abc9c', //Default setting/value to save
  213. 'type' => 'theme_mod', //Is this an 'option' or a 'theme_mod'?
  214. 'capability' => 'edit_theme_options', //Optional. Special permissions for accessing this setting.
  215. 'transport' => 'postMessage', //What triggers a refresh of the setting? 'refresh' or 'postMessage' (instant)?
  216. 'sanitize_callback' => 'sanitize_hex_color'
  217. )
  218. );
  219. // Add logo setting and sanitize it
  220. $wp_customize->add_setting( 'hemingway_logo',
  221. array(
  222. 'sanitize_callback' => 'esc_url_raw'
  223. )
  224. );
  225. //3. Finally, we define the control itself (which links a setting to a section and renders the HTML controls)...
  226. $wp_customize->add_control( new WP_Customize_Color_Control( //Instantiate the color control class
  227. $wp_customize, //Pass the $wp_customize object (required)
  228. 'hemingway_accent_color', //Set a unique ID for the control
  229. array(
  230. 'label' => __( 'Accent Color', 'hemingway' ), //Admin-visible name of the control
  231. 'section' => 'colors', //ID of the section this control should render in (can be one of yours, or a WordPress default section)
  232. 'settings' => 'accent_color', //Which setting to load and manipulate (serialized is okay)
  233. 'priority' => 10, //Determines the order this control appears in for the specified section
  234. )
  235. ) );
  236. $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'hemingway_logo', array(
  237. 'label' => __( 'Logo', 'hemingway' ),
  238. 'section' => 'hemingway_logo_section',
  239. 'settings' => 'hemingway_logo',
  240. ) ) );
  241. //4. We can also change built-in settings by modifying properties. For instance, let's make some stuff use live preview JS...
  242. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  243. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  244. }
  245. public static function header_output() {
  246. ?>
  247. <!--Customizer CSS-->
  248. <style type="text/css">
  249. <?php self::generate_css('body::selection', 'background', 'accent_color'); ?>
  250. <?php self::generate_css('body a', 'color', 'accent_color'); ?>
  251. <?php self::generate_css('body a:hover', 'color', 'accent_color'); ?>
  252. <?php self::generate_css('.blog-title a:hover', 'color', 'accent_color'); ?>
  253. <?php self::generate_css('.blog-menu a:hover', 'color', 'accent_color'); ?>
  254. <?php self::generate_css('.blog-search #searchsubmit', 'background-color', 'accent_color'); ?>
  255. <?php self::generate_css('.blog-search #searchsubmit', 'border-color', 'accent_color'); ?>
  256. <?php self::generate_css('.blog-search #searchsubmit:hover', 'background-color', 'accent_color'); ?>
  257. <?php self::generate_css('.blog-search #searchsubmit:hover', 'border-color', 'accent_color'); ?>
  258. <?php self::generate_css('.featured-media .sticky-post', 'background-color', 'accent_color'); ?>
  259. <?php self::generate_css('.post-title a:hover', 'color', 'accent_color'); ?>
  260. <?php self::generate_css('.post-meta a:hover', 'color', 'accent_color'); ?>
  261. <?php self::generate_css('.post-content a', 'color', 'accent_color'); ?>
  262. <?php self::generate_css('.post-content a:hover', 'color', 'accent_color'); ?>
  263. <?php self::generate_css('.blog .format-quote blockquote cite a:hover', 'color', 'accent_color'); ?>
  264. <?php self::generate_css('.post-content a.more-link:hover', 'background-color', 'accent_color'); ?>
  265. <?php self::generate_css('.post-content input[type="submit"]:hover', 'background-color', 'accent_color'); ?>
  266. <?php self::generate_css('.post-content input[type="reset"]:hover', 'background-color', 'accent_color'); ?>
  267. <?php self::generate_css('.post-content input[type="button"]:hover', 'background-color', 'accent_color'); ?>
  268. <?php self::generate_css('.post-content fieldset legend', 'background-color', 'accent_color'); ?>
  269. <?php self::generate_css('.post-content .searchform #searchsubmit', 'background', 'accent_color'); ?>
  270. <?php self::generate_css('.post-content .searchform #searchsubmit', 'border-color', 'accent_color'); ?>
  271. <?php self::generate_css('.post-content .searchform #searchsubmit:hover', 'background', 'accent_color'); ?>
  272. <?php self::generate_css('.post-content .searchform #searchsubmit:hover', 'border-color', 'accent_color'); ?>
  273. <?php self::generate_css('.post-categories a', 'color', 'accent_color'); ?>
  274. <?php self::generate_css('.post-categories a:hover', 'color', 'accent_color'); ?>
  275. <?php self::generate_css('.post-tags a:hover', 'background', 'accent_color'); ?>
  276. <?php self::generate_css('.post-tags a:hover:after', 'border-right-color', 'accent_color'); ?>
  277. <?php self::generate_css('.post-nav a:hover', 'color', 'accent_color'); ?>
  278. <?php self::generate_css('.archive-nav a:hover', 'color', 'accent_color'); ?>
  279. <?php self::generate_css('.logged-in-as a', 'color', 'accent_color'); ?>
  280. <?php self::generate_css('.logged-in-as a:hover', 'color', 'accent_color'); ?>
  281. <?php self::generate_css('.content #respond input[type="submit"]:hover', 'background-color', 'accent_color'); ?>
  282. <?php self::generate_css('.comment-meta-content cite a:hover', 'color', 'accent_color'); ?>
  283. <?php self::generate_css('.comment-meta-content p a:hover', 'color', 'accent_color'); ?>
  284. <?php self::generate_css('.comment-actions a:hover', 'color', 'accent_color'); ?>
  285. <?php self::generate_css('#cancel-comment-reply-link', 'color', 'accent_color'); ?>
  286. <?php self::generate_css('#cancel-comment-reply-link:hover', 'color', 'accent_color'); ?>
  287. <?php self::generate_css('.comment-nav-below a:hover', 'color', 'accent_color'); ?>
  288. <?php self::generate_css('.widget-title a', 'color', 'accent_color'); ?>
  289. <?php self::generate_css('.widget-title a:hover', 'color', 'accent_color'); ?>
  290. <?php self::generate_css('.widget_text a', 'color', 'accent_color'); ?>
  291. <?php self::generate_css('.widget_text a:hover', 'color', 'accent_color'); ?>
  292. <?php self::generate_css('.widget_rss a', 'color', 'accent_color'); ?>
  293. <?php self::generate_css('.widget_rss a:hover', 'color', 'accent_color'); ?>
  294. <?php self::generate_css('.widget_archive a', 'color', 'accent_color'); ?>
  295. <?php self::generate_css('.widget_archive a:hover', 'color', 'accent_color'); ?>
  296. <?php self::generate_css('.widget_meta a', 'color', 'accent_color'); ?>
  297. <?php self::generate_css('.widget_meta a:hover', 'color', 'accent_color'); ?>
  298. <?php self::generate_css('.widget_recent_comments a', 'color', 'accent_color'); ?>
  299. <?php self::generate_css('.widget_recent_comments a:hover', 'color', 'accent_color'); ?>
  300. <?php self::generate_css('.widget_pages a', 'color', 'accent_color'); ?>
  301. <?php self::generate_css('.widget_pages a:hover', 'color', 'accent_color'); ?>
  302. <?php self::generate_css('.widget_links a', 'color', 'accent_color'); ?>
  303. <?php self::generate_css('.widget_links a:hover', 'color', 'accent_color'); ?>
  304. <?php self::generate_css('.widget_recent_entries a', 'color', 'accent_color'); ?>
  305. <?php self::generate_css('.widget_recent_entries a:hover', 'color', 'accent_color'); ?>
  306. <?php self::generate_css('.widget_categories a', 'color', 'accent_color'); ?>
  307. <?php self::generate_css('.widget_categories a:hover', 'color', 'accent_color'); ?>
  308. <?php self::generate_css('.widget_search #searchsubmit', 'background', 'accent_color'); ?>
  309. <?php self::generate_css('.widget_search #searchsubmit', 'border-color', 'accent_color'); ?>
  310. <?php self::generate_css('.widget_search #searchsubmit:hover', 'background', 'accent_color'); ?>
  311. <?php self::generate_css('.widget_search #searchsubmit:hover', 'border-color', 'accent_color'); ?>
  312. <?php self::generate_css('#wp-calendar a', 'color', 'accent_color'); ?>
  313. <?php self::generate_css('#wp-calendar a:hover', 'color', 'accent_color'); ?>
  314. <?php self::generate_css('#wp-calendar tfoot a:hover', 'color', 'accent_color'); ?>
  315. <?php self::generate_css('.dribbble-shot:hover', 'background', 'accent_color'); ?>
  316. <?php self::generate_css('.widgetmore a', 'color', 'accent_color'); ?>
  317. <?php self::generate_css('.widgetmore a:hover', 'color', 'accent_color'); ?>
  318. <?php self::generate_css('.flickr_badge_image a:hover img', 'background', 'accent_color'); ?>
  319. <?php self::generate_css('.footer .flickr_badge_image a:hover img', 'background', 'accent_color'); ?>
  320. <?php self::generate_css('.footer .dribbble-shot:hover img', 'background', 'accent_color'); ?>
  321. <?php self::generate_css('.sidebar .tagcloud a:hover', 'background', 'accent_color'); ?>
  322. <?php self::generate_css('.footer .tagcloud a:hover', 'background', 'accent_color'); ?>
  323. <?php self::generate_css('.credits a:hover', 'color', 'accent_color'); ?>
  324. <?php self::generate_css('body#tinymce.wp-editor a', 'color', 'accent_color'); ?>
  325. <?php self::generate_css('body#tinymce.wp-editor a:hover', 'color', 'accent_color'); ?>
  326. </style>
  327. <!--/Customizer CSS-->
  328. <?php
  329. }
  330. public static function live_preview() {
  331. wp_enqueue_script(
  332. 'hemingway-themecustomizer', // Give the script a unique ID
  333. get_template_directory_uri() . '/js/theme-customizer.js', // Define the path to the JS file
  334. array( 'jquery', 'customize-preview' ), // Define dependencies
  335. '', // Define a version (optional)
  336. true // Specify whether to put in footer (leave this true)
  337. );
  338. }
  339. public static function generate_css( $selector, $style, $mod_name, $prefix='', $postfix='', $echo=true ) {
  340. $return = '';
  341. $mod = get_theme_mod($mod_name);
  342. if ( ! empty( $mod ) ) {
  343. $return = sprintf('%s { %s:%s; }',
  344. $selector,
  345. $style,
  346. $prefix.$mod.$postfix
  347. );
  348. if ( $echo ) {
  349. echo $return;
  350. }
  351. }
  352. return $return;
  353. }
  354. }
  355. // Setup the Theme Customizer settings and controls...
  356. add_action( 'customize_register' , array( 'Hemingway_Customize' , 'register' ) );
  357. // Output custom CSS to live site
  358. add_action( 'wp_head' , array( 'Hemingway_Customize' , 'header_output' ) );
  359. // Enqueue live preview javascript in Theme Customizer admin screen
  360. add_action( 'customize_preview_init' , array( 'Hemingway_Customize' , 'live_preview' ) );
  361. ?>