functions.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * Twenty Fourteen functions and definitions
  4. * Set up the theme and provides some helper functions, which are used in the
  5. * theme as custom template tags. Others are attached to action and filter
  6. * hooks in WordPress to change core functionality.
  7. * When using a child theme you can override certain functions (those wrapped
  8. * in a function_exists() call) by defining them first in your child theme's
  9. * functions.php file. The child theme's functions.php file is included before
  10. * the parent theme's file, so the child theme functions would be used.
  11. * @link http://codex.wordpress.org/Theme_Development
  12. * @link http://codex.wordpress.org/Child_Themes
  13. * Functions that are not pluggable (not wrapped in function_exists()) are
  14. * instead attached to a filter or action hook.
  15. * For more information on hooks, actions, and filters,
  16. * @link http://codex.wordpress.org/Plugin_API
  17. * @package WordPress
  18. * @subpackage Twenty_Fourteen
  19. * @since Twenty Fourteen 1.0
  20. */
  21. /**
  22. * Set up the content width value based on the theme's design.
  23. * @see twentyfourteen_content_width()
  24. * @since Twenty Fourteen 1.0
  25. */
  26. if ( ! isset( $content_width ) ) {
  27. $content_width = 474;
  28. }
  29. if ( ! function_exists( 'twentyfourteen_setup' ) ) :
  30. /**
  31. * Twenty Fourteen setup.
  32. * Set up theme defaults and registers support for various WordPress features.
  33. * Note that this function is hooked into the after_setup_theme hook, which
  34. * runs before the init hook. The init hook is too late for some features, such
  35. * as indicating support post thumbnails.
  36. * @since Twenty Fourteen 1.0
  37. */
  38. function twentyfourteen_setup() {
  39. // Add RSS feed links to <head> for posts and comments.
  40. add_theme_support( 'automatic-feed-links' );
  41. // Enable support for Post Thumbnails, and declare two sizes.
  42. add_theme_support( 'post-thumbnails' );
  43. set_post_thumbnail_size( 672, 372, true );
  44. add_image_size( 'twentyfourteen-full-width', 1038, 576, true );
  45. // This theme uses wp_nav_menu() in two locations.
  46. register_nav_menus( array(
  47. 'primary' => __( 'Top primary menu', 'twentyfourteen' ),
  48. 'secondary' => __( 'Secondary menu in left sidebar', 'twentyfourteen' ),
  49. ) );
  50. /*
  51. * Switch default core markup for search form, comment form, and comments
  52. * to output valid HTML5.
  53. */
  54. add_theme_support( 'html5', array(
  55. 'search-form',
  56. 'comment-form',
  57. 'comment-list',
  58. ) );
  59. }
  60. endif; // twentyfourteen_setup
  61. add_action( 'after_setup_theme', 'twentyfourteen_setup' );
  62. /**
  63. * Enqueue scripts and styles for the front end.
  64. * @since Twenty Fourteen 1.0
  65. * @return void
  66. */
  67. function twentyfourteen_scripts() {
  68. // Load our main stylesheet.
  69. wp_enqueue_style( 'twentyfourteen-style', get_stylesheet_uri() );
  70. // Load the Internet Explorer specific stylesheet.
  71. wp_enqueue_style( 'twentyfourteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfourteen-style' ), '20131205' );
  72. wp_style_add_data( 'twentyfourteen-ie', 'conditional', 'lt IE 9' );
  73. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); }
  74. wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131209', true );
  75. }
  76. add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
  77. /**
  78. * Extend the default WordPress body classes.
  79. * Adds body classes to denote:
  80. * 1. Single or multiple authors.
  81. * 2. Presence of header image.
  82. * 3. Index views.
  83. * 4. Full-width content layout.
  84. * 5. Presence of footer widgets.
  85. * 6. Single views.
  86. * 7. Featured content layout.
  87. * @since Twenty Fourteen 1.0
  88. * @param array $classes A list of existing body class values.
  89. * @return array The filtered body class list.
  90. */
  91. function twentyfourteen_body_classes( $classes ) {
  92. if ( is_multi_author() ) {
  93. $classes[] = 'group-blog';
  94. }
  95. if ( is_archive() || is_search() || is_home() ) {
  96. $classes[] = 'list-view';
  97. }
  98. if ( is_singular() && ! is_front_page() ) {
  99. $classes[] = 'singular';
  100. }
  101. return $classes;
  102. }
  103. add_filter( 'body_class', 'twentyfourteen_body_classes' );
  104. /**
  105. * Extend the default WordPress post classes.
  106. * Adds a post class to denote:
  107. * Non-password protected page with a post thumbnail.
  108. * @since Twenty Fourteen 1.0
  109. * @param array $classes A list of existing post class values.
  110. * @return array The filtered post class list.
  111. */
  112. function twentyfourteen_post_classes( $classes ) {
  113. if ( ! post_password_required() && has_post_thumbnail() ) {
  114. $classes[] = 'has-post-thumbnail';
  115. }
  116. return $classes;
  117. }
  118. add_filter( 'post_class', 'twentyfourteen_post_classes' );
  119. /**
  120. * Create a nicely formatted and more specific title element text for output
  121. * in head of document, based on current view.
  122. * @since Twenty Fourteen 1.0
  123. * @param string $title Default title text for current view.
  124. * @param string $sep Optional separator.
  125. * @return string The filtered title.
  126. */
  127. function twentyfourteen_wp_title( $title, $sep ) {
  128. global $paged, $page;
  129. if ( is_feed() ) {
  130. return $title;
  131. }
  132. // Add the site name.
  133. $title .= get_bloginfo( 'name' );
  134. // Add the site description for the home/front page.
  135. $site_description = get_bloginfo( 'description', 'display' );
  136. if ( $site_description && ( is_home() || is_front_page() ) ) {
  137. $title = "$title $sep $site_description";
  138. }
  139. // Add a page number if necessary.
  140. if ( $paged >= 2 || $page >= 2 ) {
  141. $title = "$title $sep " . sprintf( __( 'Page %s', 'twentyfourteen' ), max( $paged, $page ) );
  142. }
  143. return $title;
  144. }
  145. add_filter( 'wp_title', 'twentyfourteen_wp_title', 10, 2 );
  146. function basic_template_scripts() {
  147. wp_deregister_script( 'jquery' ); // jQuery
  148. wp_register_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js' );
  149. wp_enqueue_script( 'jquery' );
  150. wp_deregister_script( 'jquery-ui' ); // jQuery UI
  151. wp_register_script( 'jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array( 'jquery' ) );
  152. wp_enqueue_script( 'jquery-ui' );
  153. wp_deregister_script( 'nivoslider' ); // nivoSlider
  154. wp_register_script( 'nivoslider', get_bloginfo('template_directory') . '/js/jquery.nivo.slider.pack.js', array( 'jquery' ) );
  155. wp_enqueue_script( 'nivoslider' );
  156. wp_deregister_script( 'climatecontrol-js' ); // Example
  157. wp_register_script( 'climatecontrol-js', get_bloginfo('template_directory') . '/js/climatecontrol.js', array( 'jquery' ) );
  158. wp_enqueue_script( 'climatecontrol-js' );
  159. wp_register_style('nivoslider-css', get_bloginfo('template_directory') . '/css/nivo-slider.css');
  160. wp_enqueue_style('nivoslider-css');
  161. }
  162. add_action('wp_enqueue_scripts', 'basic_template_scripts');
  163. /* Limit string by words not characters */
  164. function limit_words($string, $word_limit) { $words = explode(" ",$string); return implode(" ",array_splice($words,0,$word_limit)); }
  165. function remove_menus () {
  166. global $menu;
  167. $restricted = array(__('Links'), __('Comments'));
  168. end ($menu);
  169. while (prev($menu)){
  170. $value = explode(' ',$menu[key($menu)][0]);
  171. if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
  172. }
  173. }
  174. add_action('admin_menu', 'remove_menus');
  175. add_action("login_head", "my_login_head");
  176. function my_login_head() {
  177. echo "
  178. <style>
  179. body.login #login h1 a {
  180. background: url('".get_bloginfo('template_url')."/images/logo_login.png') no-repeat scroll center top transparent;
  181. height: 181px;
  182. width: 269px;
  183. padding:0 25px;
  184. }
  185. </style>
  186. ";
  187. }
  188. add_filter( 'login_headerurl', 'w4_login_headerurl');
  189. function w4_login_headerurl(){
  190. return home_url('/');
  191. }
  192. function columns_one_third( $atts, $content = null ) { return '<div class="one_third">' . do_shortcode($content) . '</div>'; } add_shortcode('one_third', 'columns_one_third');
  193. function columns_one_third_last( $atts, $content = null ) { return '<div class="one_third last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('one_third_last', 'columns_one_third_last');
  194. function columns_two_third( $atts, $content = null ) { return '<div class="two_third">' . do_shortcode($content) . '</div>'; } add_shortcode('two_third', 'columns_two_third');
  195. function columns_two_third_last( $atts, $content = null ) { return '<div class="two_third last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('two_third_last', 'columns_two_third_last');
  196. function columns_one_half( $atts, $content = null ) { return '<div class="one_half">' . do_shortcode($content) . '</div>'; } add_shortcode('one_half', 'columns_one_half');
  197. function columns_one_half_last( $atts, $content = null ) { return '<div class="one_half last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('one_half_last', 'columns_one_half_last');
  198. function columns_one_fourth( $atts, $content = null ) { return '<div class="one_fourth">' . do_shortcode($content) . '</div>'; } add_shortcode('one_fourth', 'columns_one_fourth');
  199. function columns_one_fourth_last( $atts, $content = null ) { return '<div class="one_fourth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('one_fourth_last', 'columns_one_fourth_last');
  200. function columns_three_fourth( $atts, $content = null ) { return '<div class="three_fourth">' . do_shortcode($content) . '</div>'; } add_shortcode('three_fourth', 'columns_three_fourth');
  201. function columns_three_fourth_last( $atts, $content = null ) { return '<div class="three_fourth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('three_fourth_last', 'columns_three_fourth_last');
  202. function columns_one_fifth( $atts, $content = null ) { return '<div class="one_fifth">' . do_shortcode($content) . '</div>'; } add_shortcode('one_fifth', 'columns_one_fifth');
  203. function columns_one_fifth_last( $atts, $content = null ) { return '<div class="one_fifth last">' . do_shortcode($content) . '</div>
  204. <div class="clearboth"></div>'; } add_shortcode('one_fifth_last', 'columns_one_fifth_last');
  205. function columns_two_fifth( $atts, $content = null ) { return '<div class="two_fifth">' . do_shortcode($content) . '</div>'; } add_shortcode('two_fifth', 'columns_two_fifth');
  206. function columns_two_fifth_last( $atts, $content = null ) { return '<div class="two_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('two_fifth_last', 'columns_two_fifth_last');
  207. function columns_three_fifth( $atts, $content = null ) { return '<div class="three_fifth">' . do_shortcode($content) . '</div>'; } add_shortcode('three_fifth', 'columns_three_fifth');
  208. function columns_three_fifth_last( $atts, $content = null ) { return '<div class="three_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('three_fifth_last', 'columns_three_fifth_last');
  209. function columns_four_fifth( $atts, $content = null ) { return '<div class="four_fifth">' . do_shortcode($content) . '</div>'; } add_shortcode('four_fifth', 'columns_four_fifth');
  210. function columns_four_fifth_last( $atts, $content = null ) { return '<div class="four_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('four_fifth_last', 'columns_four_fifth_last');
  211. function columns_one_sixth( $atts, $content = null ) { return '<div class="one_sixth">' . do_shortcode($content) . '</div>'; } add_shortcode('one_sixth', 'columns_one_sixth');
  212. function columns_one_sixth_last( $atts, $content = null ) { return '<div class="one_sixth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('one_sixth_last', 'columns_one_sixth_last');
  213. function columns_five_sixth( $atts, $content = null ) { return '<div class="five_sixth">' . do_shortcode($content) . '</div>'; } add_shortcode('five_sixth', 'columns_five_sixth');
  214. function columns_five_sixth_last( $atts, $content = null ) { return '<div class="five_sixth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>'; } add_shortcode('five_sixth_last', 'columns_five_sixth_last');
  215. function columns_formatter($content) {
  216. $new_content = '';
  217. /* Matches the contents and the open and closing tags */
  218. $pattern_full = '{(\[raw\].*?\[/raw\])}is';
  219. /* Matches just the contents */
  220. $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
  221. /* Divide content into pieces */
  222. $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
  223. /* Loop over pieces */
  224. foreach ($pieces as $piece) {
  225. /* Look for presence of the shortcode */
  226. if (preg_match($pattern_contents, $piece, $matches)) {
  227. /* Append to content (no formatting) */
  228. $new_content .= $matches[1];
  229. } else {
  230. /* Format and append to content */
  231. $new_content .= wptexturize(wpautop($piece));
  232. }
  233. }
  234. return $new_content;
  235. }
  236. // Remove the 2 main auto-formatters
  237. remove_filter('the_content', 'wpautop');
  238. remove_filter('the_content', 'wptexturize');
  239. // Before displaying for viewing, apply this function
  240. add_filter('the_content', 'columns_formatter', 99);
  241. add_filter('widget_text', 'columns_formatter', 99);
  242. /* Pagination as found here: http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/ */
  243. function pagination($pages = '', $range = 4)
  244. {
  245. $showitems = ($range * 2)+1;
  246. global $paged;
  247. if(empty($paged)) $paged = 1;
  248. if($pages == '') {
  249. global $wp_query;
  250. $pages = $wp_query->max_num_pages;
  251. if(!$pages) { $pages = 1; }
  252. }
  253. if(1 != $pages)
  254. {
  255. echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
  256. if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
  257. if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
  258. for ($i=1; $i <= $pages; $i++) {
  259. if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
  260. echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
  261. }
  262. }
  263. if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
  264. if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
  265. echo "</div>\n";
  266. }
  267. }
  268. /* Added by https://davidawindham.com */
  269. /********************************************
  270. * Functions to clean up code output
  271. *********************************************/
  272. /****************
  273. * remove unnecessary items from the head
  274. *****************/
  275. remove_action('wp_head', 'index_rel_link' );
  276. remove_action('wp_head', 'rel_canonical');
  277. remove_action('wp_head', 'start_post_rel_link', 10, 0 );
  278. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
  279. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );
  280. remove_action('wp_head', 'parent_post_rel_link', 10, 0 );
  281. remove_action('wp_head', 'rsd_link');
  282. remove_action('wp_head', 'wlwmanifest_link');
  283. remove_action('wp_head', 'wp_generator');
  284. remove_action('wp_head', 'feed_links_extra', 3 );
  285. remove_action('wp_head', 'feed_links', 2 );
  286. remove_action('wp_head', 'wp_oembed_add_discovery_links', 10, 0 );
  287. remove_action('wp_head', 'wp_oembed_add_host_js', 10, 0 );
  288. /****************
  289. * remove versions (?v=1.0.4 etc.) from javascript and css
  290. *****************/
  291. add_filter( 'style_loader_src', 'srh_remove_wp_ver_css_js', 9999 );
  292. add_filter( 'script_loader_src', 'srh_remove_wp_ver_css_js', 9999 );
  293. function srh_remove_wp_ver_css_js( $src ) {
  294. if ( strpos( $src, 'ver=' ) )
  295. $src = remove_query_arg( 'ver', $src );
  296. return $src;
  297. }
  298. /****************
  299. * disable emojis
  300. *****************/
  301. add_action( 'init', 'disable_emojis' );
  302. function disable_emojis_tinymce( $plugins ) {
  303. if ( is_array( $plugins ) ) {
  304. return array_diff( $plugins, array( 'wpemoji' ) );
  305. } else {
  306. return array();
  307. }
  308. }
  309. function disable_emojis() {
  310. remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  311. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  312. remove_action( 'wp_print_styles', 'print_emoji_styles' );
  313. remove_action( 'admin_print_styles', 'print_emoji_styles' );
  314. remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
  315. remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
  316. remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
  317. add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
  318. }
  319. class CSS_Menu_Maker_Walker extends Walker {
  320. var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
  321. function start_lvl( &$output, $depth = 0, $args = array() ) {
  322. $indent = str_repeat("\t", $depth);
  323. $output .= "\n$indent<ul>\n";
  324. }
  325. function end_lvl( &$output, $depth = 0, $args = array() ) {
  326. $indent = str_repeat("\t", $depth);
  327. $output .= "$indent</ul>\n";
  328. }
  329. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  330. global $wp_query;
  331. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  332. $class_names = $value = '';
  333. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  334. /* Add active class */
  335. if(in_array('current-menu-item', $classes)) {
  336. $classes[] = 'active';
  337. unset($classes['current-menu-item']);
  338. }
  339. /* Check for children */
  340. $children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
  341. if (!empty($children)) {
  342. $classes[] = 'has-sub';
  343. }
  344. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  345. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  346. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  347. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  348. $output .= $indent . '<li' . $id . $value . $class_names .'>';
  349. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  350. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  351. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  352. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  353. $item_output = $args->before;
  354. $item_output .= '<a'. $attributes .'><span>';
  355. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  356. $item_output .= '</span></a>';
  357. $item_output .= $args->after;
  358. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  359. }
  360. function end_el( &$output, $item, $depth = 0, $args = array() ) {
  361. $output .= "</li>\n";
  362. }
  363. }