PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/functions.php

https://github.com/tomtom10/required-foundation
PHP | 528 lines | 289 code | 63 blank | 176 comment | 53 complexity | 23fd3b36f0bae7de2c351dc567e3da96 MD5 | raw file
  1. <?php
  2. /**
  3. * required+ Foundation functions and definitions
  4. *
  5. * Sets up the theme and provides some helper functions. Some helper functions
  6. * are used in the theme as custom template tags. Others are attached to action and
  7. * filter hooks in WordPress to change core functionality.
  8. *
  9. * The first function, required_themesetup(), sets up the theme by registering support
  10. * for various features in WordPress, such as post thumbnails, navigation menus, and the like.
  11. *
  12. * When using a child theme (see http://codex.wordpress.org/Theme_Development and
  13. * http://codex.wordpress.org/Child_Themes), you can override certain functions
  14. * (those wrapped in a function_exists() call) by defining them first in your child theme's
  15. * functions.php file. The child theme's functions.php file is included before the parent
  16. * theme's file, so the child theme functions would be used.
  17. *
  18. * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
  19. * to a filter or action hook. The hook can be removed by using remove_action() or
  20. * remove_filter() and you can attach your own function to the hook.
  21. *
  22. * We can remove the parent theme's hook only after it is attached, which means we need to
  23. * wait until setting up the child theme:
  24. *
  25. * <code>
  26. * add_action( 'after_setup_theme', 'my_child_theme_setup' );
  27. * function my_child_theme_setup() {
  28. * // We are providing our own filter for excerpt_length (or using the unfiltered value)
  29. * remove_filter( 'excerpt_length', 'required_excerpt_length' );
  30. * ...
  31. * }
  32. * </code>
  33. *
  34. * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
  35. *
  36. * @package required+ Foundation
  37. * @since required+ Foundation 0.1.0
  38. */
  39. if ( ! defined( '__DIR__' ) ) define( '__DIR__' , dirname( __FILE__ ) );
  40. define( 'FOUNDATION_VERSION', '3.2.2' ); // Version of ZURB Foundation
  41. if ( ! isset( $content_width ) )
  42. $content_width = 657;
  43. /**
  44. * Return the current theme version or parent theme version
  45. *
  46. * @since required+ Foundation 0.6.0
  47. *
  48. * @param boolean $parent By default we get the parent theme
  49. * @return int Version of the theme
  50. */
  51. function required_get_theme_version( $parent = true ) {
  52. // Name of the parent theme forder
  53. $stylesheet = 'required-foundation';
  54. if ( ! $parent ) {
  55. $stylesheet = get_stylesheet();
  56. }
  57. // Get the current theme with the new WP_Theme_API
  58. $current_theme = wp_get_theme( $stylesheet );
  59. return $current_theme->Version;
  60. }
  61. /**
  62. * We add our own nice functions to the theme if you want to change let's say req-scripts.php
  63. * just create your own /includes/req-scripts.php in your child theme and it get's overloaded.
  64. *
  65. * @package required+ Foundation
  66. * @since required+ Foundation 0.1.0
  67. */
  68. require( get_template_directory() . '/includes/req-custom-header.php' ); // optional custom header support
  69. require( get_template_directory() . '/includes/req-foundation.php' ); // make foundation work in WordPress
  70. require( get_template_directory() . '/includes/req-scripts.php' ); // register the scripts we need the correct way
  71. require( get_template_directory() . '/includes/req-shortcodes.php' ); // we got wonderful shortcodes for you
  72. require( get_template_directory() . '/includes/req-mce.php' ); // using the power of tinyMCE to add more stuff for you to layout
  73. require( get_template_directory() . '/includes/req-plugin-support.php' ); // Support for your beloved plugins
  74. /**
  75. * Add some love to the footer, of course you can replace that:
  76. * <code>
  77. * remove_action( 'required_credits', 'required_sample_credits' );
  78. * </code>
  79. */
  80. add_action( 'required_credits', 'required_sample_credits' );
  81. function required_sample_credits() {
  82. _e( '<p>This site runs on the <a href="http://themes.required.ch/" title="required+ Themes">required+ Foundation</a> Theme. Based on the awesome <a href="http://foundation.zurb.com/" title="Rapid prototyping and building library from ZURB.">Foundation</a> Framework by the humble folks at <a href="http://www.zurb.com/" title="Design for people">ZURB</a>.</p>', 'requiredfoundation' );
  83. }
  84. /**
  85. * Tell WordPress to run required_themesetup() when the 'after_setup_theme' hook is run.
  86. */
  87. add_action( 'after_setup_theme', 'required_themesetup' );
  88. if ( ! function_exists( 'required_themesetup' ) ):
  89. /**
  90. * Sets up theme defaults and registers support for various WordPress features.
  91. *
  92. * Note that this function is hooked into the after_setup_theme hook, which runs
  93. * before the init hook. The init hook is too late for some features, such as indicating
  94. * support post thumbnails.
  95. *
  96. * To override required_themesetup() in a child theme, add your own required_themesetup to your child theme's
  97. * functions.php file.
  98. *
  99. * @uses load_theme_textdomain() For translation/localization support.
  100. * @uses add_editor_style() To style the visual editor.
  101. * @uses add_theme_support() To add support for post thumbnails, automatic feed links, and Post Formats.
  102. * @uses register_nav_menus() To add support for navigation menus.
  103. * @uses add_custom_background() To add support for a custom background.
  104. * @uses add_custom_image_header() To add support for a custom header.
  105. * @uses register_default_headers() To register the default custom header images provided with the theme.
  106. * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
  107. *
  108. * @since required+ Foundation 0.1.0
  109. */
  110. function required_themesetup() {
  111. /* Make required+ Foundation available for translation.
  112. * Translations can be added to the /languages/ directory.
  113. * If you're building a theme based on required+ Foundation, use a find and replace
  114. * to change 'required' to the name of your theme in all the template files.
  115. */
  116. load_theme_textdomain( 'requiredfoundation', get_template_directory() . '/languages' );
  117. // This theme styles the visual editor with editor-style.css to match the theme style.
  118. add_editor_style();
  119. // Add default posts and comments RSS feed links to <head>.
  120. add_theme_support( 'automatic-feed-links' );
  121. // This theme uses wp_nav_menu() in two locations by default.
  122. add_theme_support('menus');
  123. register_nav_menus( array(
  124. 'primary' => __( 'Primary Menu', 'requiredfoundation' ),
  125. 'secondary' => __( 'Secondary Menu', 'requiredfoundation' )
  126. ) );
  127. // Add support for a variety of post formats
  128. add_theme_support( 'post-formats', array( 'link', 'status', 'quote', 'image' ) );
  129. // Add support for custom backgrounds. (The wp-head-callback is to make sure nothing happens, when we remove the action in the child theme)
  130. add_theme_support( 'custom-background', array( 'default-color' => 'ffffff', 'wp-head-callback' => '_custom_background_cb' ) );
  131. // This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
  132. add_theme_support( 'post-thumbnails' );
  133. }
  134. endif; // required_setup
  135. /**
  136. * Creates a nicely formatted and more specific title element text
  137. * for output in head of document, based on current view.
  138. *
  139. * @since required+ Foundation 0.5.0
  140. *
  141. * @param string $title Default title text for current view.
  142. * @param string $sep Optional separator.
  143. * @return string Filtered title.
  144. */
  145. function required_wp_title( $title, $sep ) {
  146. global $paged, $page;
  147. if ( is_feed() )
  148. return $title;
  149. // Add the site name.
  150. $title .= get_bloginfo( 'name' );
  151. // Add the site description for the home/front page.
  152. $site_description = get_bloginfo( 'description', 'display' );
  153. if ( $site_description && ( is_home() || is_front_page() ) )
  154. $title = "$title $sep $site_description";
  155. // Add a page number if necessary.
  156. if ( $paged >= 2 || $page >= 2 )
  157. $title = "$title $sep " . sprintf( __( 'Page %s', 'requiredfoundation' ), max( $paged, $page ) );
  158. return $title;
  159. }
  160. add_filter( 'wp_title', 'required_wp_title', 10, 2 );
  161. /**
  162. * Sets the post excerpt length to 40 words.
  163. *
  164. * To override this length in a child theme, remove the filter and add your own
  165. * function tied to the excerpt_length filter hook.
  166. */
  167. function required_excerpt_length( $length ) {
  168. return 40;
  169. }
  170. add_filter( 'excerpt_length', 'required_excerpt_length' );
  171. if ( ! function_exists( 'required_continue_reading_link' ) ) :
  172. /**
  173. * Returns a "Continue Reading" link for excerpts
  174. */
  175. function required_continue_reading_link() {
  176. return ' <a class="read-more" href="'. esc_url( get_permalink() ) . '">' . __( '&hellip; Continue reading &rarr;', 'requiredfoundation' ) . '</a>';
  177. }
  178. endif;
  179. /**
  180. * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and required_continue_reading_link().
  181. *
  182. * To override this in a child theme, remove the filter and add your own
  183. * function tied to the excerpt_more filter hook.
  184. */
  185. function required_auto_excerpt_more( $more ) {
  186. return required_continue_reading_link();
  187. }
  188. add_filter( 'excerpt_more', 'required_auto_excerpt_more' );
  189. /**
  190. * Adds a pretty "Continue Reading" link to custom post excerpts.
  191. *
  192. * To override this link in a child theme, remove the filter and add your own
  193. * function tied to the get_the_excerpt filter hook.
  194. */
  195. function required_custom_excerpt_more( $output ) {
  196. if ( has_excerpt() && ! is_attachment() ) {
  197. $output .= required_continue_reading_link();
  198. }
  199. return $output;
  200. }
  201. add_filter( 'get_the_excerpt', 'required_custom_excerpt_more' );
  202. function required_widgets_init() {
  203. register_sidebar( array(
  204. 'name' => __( 'Main Sidebar', 'requiredfoundation' ),
  205. 'id' => 'sidebar-main',
  206. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  207. 'after_widget' => "</aside>",
  208. 'before_title' => '<h4 class="widget-title">',
  209. 'after_title' => '</h4>',
  210. ) );
  211. register_sidebar( array(
  212. 'name' => __( 'Footer Area One', 'requiredfoundation' ),
  213. 'id' => 'sidebar-footer-1',
  214. 'description' => __( 'An optional widget area for your site footer', 'requiredfoundation' ),
  215. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  216. 'after_widget' => "</aside>",
  217. 'before_title' => '<h4 class="widget-title">',
  218. 'after_title' => '</h4>',
  219. ) );
  220. register_sidebar( array(
  221. 'name' => __( 'Footer Area Two', 'requiredfoundation' ),
  222. 'id' => 'sidebar-footer-2',
  223. 'description' => __( 'An optional widget area for your site footer', 'requiredfoundation' ),
  224. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  225. 'after_widget' => "</aside>",
  226. 'before_title' => '<h4 class="widget-title">',
  227. 'after_title' => '</h4>',
  228. ) );
  229. register_sidebar( array(
  230. 'name' => __( 'Footer Area Three', 'requiredfoundation' ),
  231. 'id' => 'sidebar-footer-3',
  232. 'description' => __( 'An optional widget area for your site footer', 'requiredfoundation' ),
  233. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  234. 'after_widget' => "</aside>",
  235. 'before_title' => '<h4 class="widget-title">',
  236. 'after_title' => '</h4>',
  237. ) );
  238. }
  239. add_action( 'widgets_init', 'required_widgets_init' );
  240. if ( ! function_exists( 'required_single_content_nav' ) ) :
  241. /**
  242. * Display navigation to next/previous pages when applicable
  243. */
  244. function required_single_content_nav( ) {
  245. ?>
  246. <nav class="nav-single">
  247. <h3 class="assistive-text"><?php _e( 'Post navigation', 'requiredfoundation' ); ?></h3>
  248. <span class="nav-previous"><?php previous_post_link( '%link', '&larr; %title' ); ?></span>
  249. <span class="nav-next"><?php next_post_link( '%link', '%title &rarr;' ); ?></span>
  250. </nav><!-- .nav-single -->
  251. <?php
  252. }
  253. endif; // required_content_nav
  254. /**
  255. * Manage the layout of the footer sidebars, get it? Pretty clever huh?
  256. * Just kidding ;-)
  257. *
  258. * @return string
  259. * @since required+ Foundation 0.1.0
  260. **/
  261. function required_footer_sidebar_columns() {
  262. // default value
  263. $required_columns = 'four columns';
  264. // only the first sidebar is active, go full-width
  265. if ( is_active_sidebar( 'sidebar-footer-1' )
  266. && ! is_active_sidebar( 'sidebar-footer-2' )
  267. && ! is_active_sidebar( 'sidebar-footer-3') ) {
  268. $required_columns = 'twelve columns';
  269. }
  270. // the first one is disabled, go half-half
  271. else if ( ! is_active_sidebar( 'sidebar-footer-1' )
  272. && is_active_sidebar( 'sidebar-footer-2')
  273. && is_active_sidebar( 'sidebar-footer-3' ) ) {
  274. $required_columns = 'six columns';
  275. }
  276. // the last one is disabled, go eight-four
  277. else if ( ! is_active_sidebar( 'sidebar-footer-3' )
  278. && is_active_sidebar( 'sidebar-footer-2' )
  279. && is_active_sidebar( 'sidebar-footer-1' ) ) {
  280. $required_columns = 'eight columns';
  281. }
  282. // the middle on is disabled, go four-eight
  283. else if ( ! is_active_sidebar( 'sidebar-footer-2' )
  284. && is_active_sidebar( 'sidebar-footer-3' )
  285. && is_active_sidebar( 'sidebar-footer-1' ) ) {
  286. $required_columns = 'four columns reverse';
  287. }
  288. return $required_columns;
  289. }
  290. if ( ! function_exists( 'required_comment' ) ) :
  291. /**
  292. * Template for comments and pingbacks.
  293. *
  294. * To override this walker in a child theme without modifying the comments template
  295. * simply create your own required_comment(), and that function will be used instead.
  296. *
  297. * Used as a callback by wp_list_comments() for displaying the comments.
  298. *
  299. * @since required+ Foundation 0.1.0
  300. */
  301. function required_comment( $comment, $args, $depth ) {
  302. $GLOBALS['comment'] = $comment;
  303. switch ( $comment->comment_type ) :
  304. case 'pingback' :
  305. case 'trackback' :
  306. ?>
  307. <li class="post pingback">
  308. <p><?php _e( 'Pingback:', 'requiredfoundation' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'requiredfoundation' ), '<span class="edit-link">', '</span>' ); ?></p>
  309. <?php
  310. break;
  311. default :
  312. ?>
  313. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  314. <article id="comment-<?php comment_ID(); ?>" class="comment panel">
  315. <header class="comment-meta">
  316. <div class="comment-author vcard">
  317. <?php
  318. $avatar_size = 48;
  319. echo get_avatar( $comment, $avatar_size );
  320. /* translators: 1: comment author, 2: date and time */
  321. printf( __( '<h6>%1$s on %2$s <span class="says">said:</span></h6>', 'requiredfoundation' ),
  322. sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
  323. sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
  324. esc_url( get_comment_link( $comment->comment_ID ) ),
  325. get_comment_time( 'c' ),
  326. /* translators: 1: date, 2: time */
  327. sprintf( __( '%1$s at %2$s', 'requiredfoundation' ), get_comment_date(), get_comment_time() )
  328. )
  329. );
  330. ?>
  331. </div><!-- .comment-author .vcard -->
  332. <?php if ( $comment->comment_approved == '0' ) : ?>
  333. <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'requiredfoundation' ); ?></em>
  334. <?php endif; ?>
  335. </header>
  336. <div class="comment-content"><?php comment_text(); ?> <?php edit_comment_link( __( 'Edit', 'requiredfoundation' ), '<span class="edit-link">', '</span>' ); ?></div>
  337. <div class="comment-reply">
  338. <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>&darr;</span>', 'requiredfoundation' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  339. </div><!-- .reply -->
  340. </article><!-- #comment-## -->
  341. <?php
  342. break;
  343. endswitch;
  344. }
  345. endif; // ends check for required_comment()
  346. if ( ! function_exists( 'required_posted_on' ) ) :
  347. /**
  348. * Prints HTML with meta information for the current post-date/time and author.
  349. * Create your own required_posted_on to override in a child theme
  350. *
  351. * @since required+ Foundation 0.3.0
  352. */
  353. function required_posted_on() {
  354. printf( __( '<h6>Posted by <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span> on <a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a></h6>', 'requiredfoundation' ),
  355. esc_url( get_permalink() ),
  356. esc_attr( get_the_time() ),
  357. esc_attr( get_the_date( 'c' ) ),
  358. esc_html( get_the_date() ),
  359. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  360. sprintf( esc_attr__( 'View all posts by %s', 'requiredfoundation' ), get_the_author() ),
  361. esc_html( get_the_author() )
  362. );
  363. }
  364. endif;
  365. /**
  366. * Adds two classes to the array of body classes.
  367. * The first is if the site has only had one author with published posts.
  368. * The second is if a singular post being displayed
  369. *
  370. * @since required+ Foundation 0.1.0
  371. */
  372. function required_body_classes( $classes ) {
  373. if ( ! is_multi_author() ) {
  374. $classes[] = 'single-author';
  375. }
  376. if ( is_singular() && ! is_home() && ! is_page_template( 'fulldwidth-page.php' ) && ! is_page_template( 'left-sidebar-page.php' ) )
  377. $classes[] = 'singular';
  378. if ( is_page_template( 'page-templates/off-canvas-page.php' ) ) {
  379. $classes[] = 'off-canvas';
  380. }
  381. return $classes;
  382. }
  383. add_filter( 'body_class', 'required_body_classes' );
  384. if ( ! function_exists( 'required_archive_title' ) ) :
  385. /**
  386. * Nice archive titles
  387. *
  388. * @return string
  389. * @since required+ Foundation 0.1.0
  390. **/
  391. remove_filter('term_description','wpautop');
  392. function required_archive_title () {
  393. if ( is_category() ) {
  394. $category_description = category_description();
  395. echo $panelbool = ! empty( $category_description ) ? '<div class="panel clearfix">' : ''; ?>
  396. <header class="page-header">
  397. <h3 class="page-title"><?php
  398. printf( __( 'Category Archives: %s', 'requiredfoundation' ), '<span>' . single_cat_title( '', false ) . '</span>' );
  399. ?></h3>
  400. <?php
  401. if ( ! empty( $category_description ) )
  402. echo apply_filters( 'category_archive_meta', '<p class="category-archive-meta lead">' . $category_description . '</p>' );
  403. ?>
  404. </header>
  405. <?php echo $panelbool = ! empty( $category_description ) ? '</div>' : '';
  406. }
  407. if ( is_tag() ) {
  408. $tag_description = tag_description();
  409. echo $panelbool = ! empty( $tag_description ) ? '<div class="panel clearfix">' : ''; ?>
  410. <header class="page-header">
  411. <h3 class="page-title"><?php
  412. printf( __( 'Tag Archives: %s', 'requiredfoundation' ), '<span>' . single_tag_title( '', false ) . '</span>' );
  413. ?></h3>
  414. <?php
  415. if ( ! empty( $tag_description ) )
  416. echo apply_filters( 'tag_archive_meta', '<p class="lead tag-archive-meta">' . $tag_description . '</p>' );
  417. ?>
  418. </header>
  419. <?php echo $panelbool = ! empty( $tag_description ) ? '</div>' : '';
  420. }
  421. if ( is_author() ) {
  422. // If a user has filled out their description, show a bio on their entries.
  423. if ( get_the_author_meta( 'description' ) ) : ?>
  424. <header class="page-header">
  425. <div id="author-info" class="panel clearfix">
  426. <h3 class="page-title author"><?php printf( __( 'Author Archives: %s', 'requiredfoundation' ), '<span class="vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( "ID" ) ) ) . '" title="' . esc_attr( get_the_author() ) . '" rel="me">' . get_the_author() . '</a></span>' ); ?></h3>
  427. <div id="author-avatar">
  428. <?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'required_author_bio_avatar_size', 72 ) ); ?>
  429. </div><!-- #author-avatar -->
  430. <div id="author-description">
  431. <h4><?php printf( __( 'About %s', 'requiredfoundation' ), get_the_author() ); ?></h4>
  432. <p class="lead"><?php the_author_meta( 'description' ); ?></p>
  433. </div><!-- #author-description -->
  434. </div><!-- #entry-author-info -->
  435. <?php else : ?>
  436. <header class="page-header">
  437. <h3 class="page-title author"><?php printf( __( 'Author Archives: %s', 'requiredfoundation' ), '<span class="vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( "ID" ) ) ) . '" title="' . esc_attr( get_the_author() ) . '" rel="me">' . get_the_author() . '</a></span>' ); ?></h3>
  438. </header>
  439. <?php endif;
  440. }
  441. if ( is_archive() ) { ?>
  442. <header class="page-header">
  443. <h3 class="page-title">
  444. <?php if ( is_day() ) : ?>
  445. <?php printf( __( 'Daily Archives: %s', 'requiredfoundation' ), '<span>' . get_the_date() . '</span>' ); ?>
  446. <?php elseif ( is_month() ) : ?>
  447. <?php printf( __( 'Monthly Archives: %s', 'requiredfoundation' ), '<span>' . get_the_date( _x( 'F Y', 'monthly archives date format', 'requiredfoundation' ) ) . '</span>' ); ?>
  448. <?php elseif ( is_year() ) : ?>
  449. <?php printf( __( 'Yearly Archives: %s', 'requiredfoundation' ), '<span>' . get_the_date( _x( 'Y', 'yearly archives date format', 'requiredfoundation' ) ) . '</span>' ); ?>
  450. <?php elseif ( ! is_author() && ! is_category() && ! is_tag() ) : ?>
  451. <?php _e( 'Blog Archives', 'requiredfoundation' ); ?>
  452. <?php endif; ?>
  453. </h3>
  454. </header><?php
  455. }
  456. if ( is_search() ) {
  457. ?>
  458. <header class="page-header">
  459. <h3 class="page-title"><?php printf( __( 'Search Results for: %s', 'requiredfoundation' ), '<span>' . get_search_query() . '</span>' ); ?></h3>
  460. </header>
  461. <?php
  462. }
  463. }
  464. endif;
  465. ?>