functions.php 20 KB

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