PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/twentythirteen/functions.php

https://github.com/davodey/WordPress
PHP | 531 lines | 232 code | 69 blank | 230 comment | 49 complexity | 1cdfb3d18f6cf5c929aeb16177bad36d MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Twenty Thirteen functions and definitions
  4. *
  5. * Sets up the theme and provides some helper functions, which are used in the
  6. * theme as custom template tags. Others are attached to action and filter
  7. * hooks in WordPress to change core functionality.
  8. *
  9. * When using a child theme (see http://codex.wordpress.org/Theme_Development
  10. * and http://codex.wordpress.org/Child_Themes), you can override certain
  11. * functions (those wrapped in a function_exists() call) by defining them first
  12. * in your child theme's functions.php file. The child theme's functions.php
  13. * file is included before the parent theme's file, so the child theme
  14. * functions would be used.
  15. *
  16. * Functions that are not pluggable (not wrapped in function_exists()) are
  17. * instead attached to a filter or action hook.
  18. *
  19. * For more information on hooks, actions, and filters, @link http://codex.wordpress.org/Plugin_API
  20. *
  21. * @package WordPress
  22. * @subpackage Twenty_Thirteen
  23. * @since Twenty Thirteen 1.0
  24. */
  25. /*
  26. * Set up the content width value based on the theme's design.
  27. *
  28. * @see twentythirteen_content_width() for template-specific adjustments.
  29. */
  30. if ( ! isset( $content_width ) )
  31. $content_width = 604;
  32. /**
  33. * Add support for a custom header image.
  34. */
  35. require get_template_directory() . '/inc/custom-header.php';
  36. /**
  37. * Twenty Thirteen only works in WordPress 3.6 or later.
  38. */
  39. if ( version_compare( $GLOBALS['wp_version'], '3.6-alpha', '<' ) )
  40. require get_template_directory() . '/inc/back-compat.php';
  41. /**
  42. * Twenty Thirteen setup.
  43. *
  44. * Sets up theme defaults and registers the various WordPress features that
  45. * Twenty Thirteen supports.
  46. *
  47. * @uses load_theme_textdomain() For translation/localization support.
  48. * @uses add_editor_style() To add Visual Editor stylesheets.
  49. * @uses add_theme_support() To add support for automatic feed links, post
  50. * formats, and post thumbnails.
  51. * @uses register_nav_menu() To add support for a navigation menu.
  52. * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
  53. *
  54. * @since Twenty Thirteen 1.0
  55. */
  56. function twentythirteen_setup() {
  57. /*
  58. * Makes Twenty Thirteen available for translation.
  59. *
  60. * Translations can be added to the /languages/ directory.
  61. * If you're building a theme based on Twenty Thirteen, use a find and
  62. * replace to change 'twentythirteen' to the name of your theme in all
  63. * template files.
  64. */
  65. load_theme_textdomain( 'twentythirteen', get_template_directory() . '/languages' );
  66. /*
  67. * This theme styles the visual editor to resemble the theme style,
  68. * specifically font, colors, icons, and column width.
  69. */
  70. add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentythirteen_fonts_url() ) );
  71. // Adds RSS feed links to <head> for posts and comments.
  72. add_theme_support( 'automatic-feed-links' );
  73. /*
  74. * Switches default core markup for search form, comment form,
  75. * and comments to output valid HTML5.
  76. */
  77. add_theme_support( 'html5', array(
  78. 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
  79. ) );
  80. /*
  81. * This theme supports all available post formats by default.
  82. * See http://codex.wordpress.org/Post_Formats
  83. */
  84. add_theme_support( 'post-formats', array(
  85. 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video'
  86. ) );
  87. // This theme uses wp_nav_menu() in one location.
  88. register_nav_menu( 'primary', __( 'Navigation Menu', 'twentythirteen' ) );
  89. /*
  90. * This theme uses a custom image size for featured images, displayed on
  91. * "standard" posts and pages.
  92. */
  93. add_theme_support( 'post-thumbnails' );
  94. set_post_thumbnail_size( 604, 270, true );
  95. // This theme uses its own gallery styles.
  96. add_filter( 'use_default_gallery_style', '__return_false' );
  97. }
  98. add_action( 'after_setup_theme', 'twentythirteen_setup' );
  99. /**
  100. * Return the Google font stylesheet URL, if available.
  101. *
  102. * The use of Source Sans Pro and Bitter by default is localized. For languages
  103. * that use characters not supported by the font, the font can be disabled.
  104. *
  105. * @since Twenty Thirteen 1.0
  106. *
  107. * @return string Font stylesheet or empty string if disabled.
  108. */
  109. function twentythirteen_fonts_url() {
  110. $fonts_url = '';
  111. /* Translators: If there are characters in your language that are not
  112. * supported by Source Sans Pro, translate this to 'off'. Do not translate
  113. * into your own language.
  114. */
  115. $source_sans_pro = _x( 'on', 'Source Sans Pro font: on or off', 'twentythirteen' );
  116. /* Translators: If there are characters in your language that are not
  117. * supported by Bitter, translate this to 'off'. Do not translate into your
  118. * own language.
  119. */
  120. $bitter = _x( 'on', 'Bitter font: on or off', 'twentythirteen' );
  121. if ( 'off' !== $source_sans_pro || 'off' !== $bitter ) {
  122. $font_families = array();
  123. if ( 'off' !== $source_sans_pro )
  124. $font_families[] = 'Source Sans Pro:300,400,700,300italic,400italic,700italic';
  125. if ( 'off' !== $bitter )
  126. $font_families[] = 'Bitter:400,700';
  127. $query_args = array(
  128. 'family' => urlencode( implode( '|', $font_families ) ),
  129. 'subset' => urlencode( 'latin,latin-ext' ),
  130. );
  131. $fonts_url = add_query_arg( $query_args, "//fonts.googleapis.com/css" );
  132. }
  133. return $fonts_url;
  134. }
  135. /**
  136. * Enqueue scripts and styles for the front end.
  137. *
  138. * @since Twenty Thirteen 1.0
  139. */
  140. function twentythirteen_scripts_styles() {
  141. /*
  142. * Adds JavaScript to pages with the comment form to support
  143. * sites with threaded comments (when in use).
  144. */
  145. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
  146. wp_enqueue_script( 'comment-reply' );
  147. // Adds Masonry to handle vertical alignment of footer widgets.
  148. if ( is_active_sidebar( 'sidebar-1' ) )
  149. wp_enqueue_script( 'jquery-masonry' );
  150. // Loads JavaScript file with functionality specific to Twenty Thirteen.
  151. wp_enqueue_script( 'twentythirteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '2014-03-18', true );
  152. // Add Source Sans Pro and Bitter fonts, used in the main stylesheet.
  153. wp_enqueue_style( 'twentythirteen-fonts', twentythirteen_fonts_url(), array(), null );
  154. // Add Genericons font, used in the main stylesheet.
  155. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.03' );
  156. // Loads our main stylesheet.
  157. wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' );
  158. // Loads the Internet Explorer specific stylesheet.
  159. wp_enqueue_style( 'twentythirteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentythirteen-style' ), '2013-07-18' );
  160. wp_style_add_data( 'twentythirteen-ie', 'conditional', 'lt IE 9' );
  161. }
  162. add_action( 'wp_enqueue_scripts', 'twentythirteen_scripts_styles' );
  163. /**
  164. * Filter the page title.
  165. *
  166. * Creates a nicely formatted and more specific title element text for output
  167. * in head of document, based on current view.
  168. *
  169. * @since Twenty Thirteen 1.0
  170. *
  171. * @param string $title Default title text for current view.
  172. * @param string $sep Optional separator.
  173. * @return string The filtered title.
  174. */
  175. function twentythirteen_wp_title( $title, $sep ) {
  176. global $paged, $page;
  177. if ( is_feed() )
  178. return $title;
  179. // Add the site name.
  180. $title .= get_bloginfo( 'name', 'display' );
  181. // Add the site description for the home/front page.
  182. $site_description = get_bloginfo( 'description', 'display' );
  183. if ( $site_description && ( is_home() || is_front_page() ) )
  184. $title = "$title $sep $site_description";
  185. // Add a page number if necessary.
  186. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() )
  187. $title = "$title $sep " . sprintf( __( 'Page %s', 'twentythirteen' ), max( $paged, $page ) );
  188. return $title;
  189. }
  190. add_filter( 'wp_title', 'twentythirteen_wp_title', 10, 2 );
  191. /**
  192. * Register two widget areas.
  193. *
  194. * @since Twenty Thirteen 1.0
  195. */
  196. function twentythirteen_widgets_init() {
  197. register_sidebar( array(
  198. 'name' => __( 'Main Widget Area', 'twentythirteen' ),
  199. 'id' => 'sidebar-1',
  200. 'description' => __( 'Appears in the footer section of the site.', 'twentythirteen' ),
  201. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  202. 'after_widget' => '</aside>',
  203. 'before_title' => '<h3 class="widget-title">',
  204. 'after_title' => '</h3>',
  205. ) );
  206. register_sidebar( array(
  207. 'name' => __( 'Secondary Widget Area', 'twentythirteen' ),
  208. 'id' => 'sidebar-2',
  209. 'description' => __( 'Appears on posts and pages in the sidebar.', 'twentythirteen' ),
  210. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  211. 'after_widget' => '</aside>',
  212. 'before_title' => '<h3 class="widget-title">',
  213. 'after_title' => '</h3>',
  214. ) );
  215. }
  216. add_action( 'widgets_init', 'twentythirteen_widgets_init' );
  217. if ( ! function_exists( 'twentythirteen_paging_nav' ) ) :
  218. /**
  219. * Display navigation to next/previous set of posts when applicable.
  220. *
  221. * @since Twenty Thirteen 1.0
  222. */
  223. function twentythirteen_paging_nav() {
  224. global $wp_query;
  225. // Don't print empty markup if there's only one page.
  226. if ( $wp_query->max_num_pages < 2 )
  227. return;
  228. ?>
  229. <nav class="navigation paging-navigation" role="navigation">
  230. <h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentythirteen' ); ?></h1>
  231. <div class="nav-links">
  232. <?php if ( get_next_posts_link() ) : ?>
  233. <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentythirteen' ) ); ?></div>
  234. <?php endif; ?>
  235. <?php if ( get_previous_posts_link() ) : ?>
  236. <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentythirteen' ) ); ?></div>
  237. <?php endif; ?>
  238. </div><!-- .nav-links -->
  239. </nav><!-- .navigation -->
  240. <?php
  241. }
  242. endif;
  243. if ( ! function_exists( 'twentythirteen_post_nav' ) ) :
  244. /**
  245. * Display navigation to next/previous post when applicable.
  246. *
  247. * @since Twenty Thirteen 1.0
  248. */
  249. function twentythirteen_post_nav() {
  250. global $post;
  251. // Don't print empty markup if there's nowhere to navigate.
  252. $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
  253. $next = get_adjacent_post( false, '', false );
  254. if ( ! $next && ! $previous )
  255. return;
  256. ?>
  257. <nav class="navigation post-navigation" role="navigation">
  258. <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentythirteen' ); ?></h1>
  259. <div class="nav-links">
  260. <?php previous_post_link( '%link', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', 'twentythirteen' ) ); ?>
  261. <?php next_post_link( '%link', _x( '%title <span class="meta-nav">&rarr;</span>', 'Next post link', 'twentythirteen' ) ); ?>
  262. </div><!-- .nav-links -->
  263. </nav><!-- .navigation -->
  264. <?php
  265. }
  266. endif;
  267. if ( ! function_exists( 'twentythirteen_entry_meta' ) ) :
  268. /**
  269. * Print HTML with meta information for current post: categories, tags, permalink, author, and date.
  270. *
  271. * Create your own twentythirteen_entry_meta() to override in a child theme.
  272. *
  273. * @since Twenty Thirteen 1.0
  274. */
  275. function twentythirteen_entry_meta() {
  276. if ( is_sticky() && is_home() && ! is_paged() )
  277. echo '<span class="featured-post">' . __( 'Sticky', 'twentythirteen' ) . '</span>';
  278. if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
  279. twentythirteen_entry_date();
  280. // Translators: used between list items, there is a space after the comma.
  281. $categories_list = get_the_category_list( __( ', ', 'twentythirteen' ) );
  282. if ( $categories_list ) {
  283. echo '<span class="categories-links">' . $categories_list . '</span>';
  284. }
  285. // Translators: used between list items, there is a space after the comma.
  286. $tag_list = get_the_tag_list( '', __( ', ', 'twentythirteen' ) );
  287. if ( $tag_list ) {
  288. echo '<span class="tags-links">' . $tag_list . '</span>';
  289. }
  290. // Post author
  291. if ( 'post' == get_post_type() ) {
  292. printf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
  293. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  294. esc_attr( sprintf( __( 'View all posts by %s', 'twentythirteen' ), get_the_author() ) ),
  295. get_the_author()
  296. );
  297. }
  298. }
  299. endif;
  300. if ( ! function_exists( 'twentythirteen_entry_date' ) ) :
  301. /**
  302. * Print HTML with date information for current post.
  303. *
  304. * Create your own twentythirteen_entry_date() to override in a child theme.
  305. *
  306. * @since Twenty Thirteen 1.0
  307. *
  308. * @param boolean $echo (optional) Whether to echo the date. Default true.
  309. * @return string The HTML-formatted post date.
  310. */
  311. function twentythirteen_entry_date( $echo = true ) {
  312. if ( has_post_format( array( 'chat', 'status' ) ) )
  313. $format_prefix = _x( '%1$s on %2$s', '1: post format name. 2: date', 'twentythirteen' );
  314. else
  315. $format_prefix = '%2$s';
  316. $date = sprintf( '<span class="date"><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a></span>',
  317. esc_url( get_permalink() ),
  318. esc_attr( sprintf( __( 'Permalink to %s', 'twentythirteen' ), the_title_attribute( 'echo=0' ) ) ),
  319. esc_attr( get_the_date( 'c' ) ),
  320. esc_html( sprintf( $format_prefix, get_post_format_string( get_post_format() ), get_the_date() ) )
  321. );
  322. if ( $echo )
  323. echo $date;
  324. return $date;
  325. }
  326. endif;
  327. if ( ! function_exists( 'twentythirteen_the_attached_image' ) ) :
  328. /**
  329. * Print the attached image with a link to the next attached image.
  330. *
  331. * @since Twenty Thirteen 1.0
  332. */
  333. function twentythirteen_the_attached_image() {
  334. /**
  335. * Filter the image attachment size to use.
  336. *
  337. * @since Twenty thirteen 1.0
  338. *
  339. * @param array $size {
  340. * @type int The attachment height in pixels.
  341. * @type int The attachment width in pixels.
  342. * }
  343. */
  344. $attachment_size = apply_filters( 'twentythirteen_attachment_size', array( 724, 724 ) );
  345. $next_attachment_url = wp_get_attachment_url();
  346. $post = get_post();
  347. /*
  348. * Grab the IDs of all the image attachments in a gallery so we can get the URL
  349. * of the next adjacent image in a gallery, or the first image (if we're
  350. * looking at the last image in a gallery), or, in a gallery of one, just the
  351. * link to that image file.
  352. */
  353. $attachment_ids = get_posts( array(
  354. 'post_parent' => $post->post_parent,
  355. 'fields' => 'ids',
  356. 'numberposts' => -1,
  357. 'post_status' => 'inherit',
  358. 'post_type' => 'attachment',
  359. 'post_mime_type' => 'image',
  360. 'order' => 'ASC',
  361. 'orderby' => 'menu_order ID'
  362. ) );
  363. // If there is more than 1 attachment in a gallery...
  364. if ( count( $attachment_ids ) > 1 ) {
  365. foreach ( $attachment_ids as $attachment_id ) {
  366. if ( $attachment_id == $post->ID ) {
  367. $next_id = current( $attachment_ids );
  368. break;
  369. }
  370. }
  371. // get the URL of the next image attachment...
  372. if ( $next_id )
  373. $next_attachment_url = get_attachment_link( $next_id );
  374. // or get the URL of the first image attachment.
  375. else
  376. $next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
  377. }
  378. printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
  379. esc_url( $next_attachment_url ),
  380. the_title_attribute( array( 'echo' => false ) ),
  381. wp_get_attachment_image( $post->ID, $attachment_size )
  382. );
  383. }
  384. endif;
  385. /**
  386. * Return the post URL.
  387. *
  388. * @uses get_url_in_content() to get the URL in the post meta (if it exists) or
  389. * the first link found in the post content.
  390. *
  391. * Falls back to the post permalink if no URL is found in the post.
  392. *
  393. * @since Twenty Thirteen 1.0
  394. *
  395. * @return string The Link format URL.
  396. */
  397. function twentythirteen_get_link_url() {
  398. $content = get_the_content();
  399. $has_url = get_url_in_content( $content );
  400. return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
  401. }
  402. /**
  403. * Extend the default WordPress body classes.
  404. *
  405. * Adds body classes to denote:
  406. * 1. Single or multiple authors.
  407. * 2. Active widgets in the sidebar to change the layout and spacing.
  408. * 3. When avatars are disabled in discussion settings.
  409. *
  410. * @since Twenty Thirteen 1.0
  411. *
  412. * @param array $classes A list of existing body class values.
  413. * @return array The filtered body class list.
  414. */
  415. function twentythirteen_body_class( $classes ) {
  416. if ( ! is_multi_author() )
  417. $classes[] = 'single-author';
  418. if ( is_active_sidebar( 'sidebar-2' ) && ! is_attachment() && ! is_404() )
  419. $classes[] = 'sidebar';
  420. if ( ! get_option( 'show_avatars' ) )
  421. $classes[] = 'no-avatars';
  422. return $classes;
  423. }
  424. add_filter( 'body_class', 'twentythirteen_body_class' );
  425. /**
  426. * Adjust content_width value for video post formats and attachment templates.
  427. *
  428. * @since Twenty Thirteen 1.0
  429. */
  430. function twentythirteen_content_width() {
  431. global $content_width;
  432. if ( is_attachment() )
  433. $content_width = 724;
  434. elseif ( has_post_format( 'audio' ) )
  435. $content_width = 484;
  436. }
  437. add_action( 'template_redirect', 'twentythirteen_content_width' );
  438. /**
  439. * Add postMessage support for site title and description for the Customizer.
  440. *
  441. * @since Twenty Thirteen 1.0
  442. *
  443. * @param WP_Customize_Manager $wp_customize Customizer object.
  444. */
  445. function twentythirteen_customize_register( $wp_customize ) {
  446. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  447. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  448. $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
  449. }
  450. add_action( 'customize_register', 'twentythirteen_customize_register' );
  451. /**
  452. * Enqueue Javascript postMessage handlers for the Customizer.
  453. *
  454. * Binds JavaScript handlers to make the Customizer preview
  455. * reload changes asynchronously.
  456. *
  457. * @since Twenty Thirteen 1.0
  458. */
  459. function twentythirteen_customize_preview_js() {
  460. wp_enqueue_script( 'twentythirteen-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20130226', true );
  461. }
  462. add_action( 'customize_preview_init', 'twentythirteen_customize_preview_js' );