PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 450 lines | 220 code | 52 blank | 178 comment | 37 complexity | 675bcc20a3c59ae134b47d25310ee277 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Twenty Twelve functions and definitions.
  4. *
  5. * Sets up the theme and provides some helper functions, which are used
  6. * in the theme as custom template tags. Others are attached to action and
  7. * filter hooks in WordPress to change core functionality.
  8. *
  9. * When using a child theme (see http://codex.wordpress.org/Theme_Development and
  10. * http://codex.wordpress.org/Child_Themes), you can override certain functions
  11. * (those wrapped in a function_exists() call) by defining them first in your child theme's
  12. * functions.php file. The child theme's functions.php file is included before the parent
  13. * theme's file, so the child theme functions would be used.
  14. *
  15. * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
  16. * to a filter or action hook.
  17. *
  18. * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
  19. *
  20. * @package WordPress
  21. * @subpackage Twenty_Twelve
  22. * @since Twenty Twelve 1.0
  23. */
  24. /**
  25. * Sets up the content width value based on the theme's design and stylesheet.
  26. */
  27. if ( ! isset( $content_width ) )
  28. $content_width = 625;
  29. /**
  30. * Sets up theme defaults and registers the various WordPress features that
  31. * Twenty Twelve supports.
  32. *
  33. * @uses load_theme_textdomain() For translation/localization support.
  34. * @uses add_editor_style() To add a Visual Editor stylesheet.
  35. * @uses add_theme_support() To add support for post thumbnails, automatic feed links,
  36. * custom background, and post formats.
  37. * @uses register_nav_menu() To add support for navigation menus.
  38. * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
  39. *
  40. * @since Twenty Twelve 1.0
  41. */
  42. function twentytwelve_setup() {
  43. /*
  44. * Makes Twenty Twelve available for translation.
  45. *
  46. * Translations can be added to the /languages/ directory.
  47. * If you're building a theme based on Twenty Twelve, use a find and replace
  48. * to change 'twentytwelve' to the name of your theme in all the template files.
  49. */
  50. load_theme_textdomain( 'twentytwelve', get_template_directory() . '/languages' );
  51. // This theme styles the visual editor with editor-style.css to match the theme style.
  52. add_editor_style();
  53. // Adds RSS feed links to <head> for posts and comments.
  54. add_theme_support( 'automatic-feed-links' );
  55. // This theme supports a variety of post formats.
  56. add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote', 'status' ) );
  57. // This theme uses wp_nav_menu() in one location.
  58. register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ) );
  59. /*
  60. * This theme supports custom background color and image, and here
  61. * we also set up the default background color.
  62. */
  63. add_theme_support( 'custom-background', array(
  64. 'default-color' => 'e6e6e6',
  65. ) );
  66. // This theme uses a custom image size for featured images, displayed on "standard" posts.
  67. add_theme_support( 'post-thumbnails' );
  68. set_post_thumbnail_size( 624, 9999 ); // Unlimited height, soft crop
  69. }
  70. add_action( 'after_setup_theme', 'twentytwelve_setup' );
  71. /**
  72. * Adds support for a custom header image.
  73. */
  74. require( get_template_directory() . '/inc/custom-header.php' );
  75. /**
  76. * Enqueues scripts and styles for front-end.
  77. *
  78. * @since Twenty Twelve 1.0
  79. */
  80. function twentytwelve_scripts_styles() {
  81. global $wp_styles;
  82. /*
  83. * Adds JavaScript to pages with the comment form to support
  84. * sites with threaded comments (when in use).
  85. */
  86. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
  87. wp_enqueue_script( 'comment-reply' );
  88. /*
  89. * Adds JavaScript for handling the navigation menu hide-and-show behavior.
  90. */
  91. wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '1.0', true );
  92. /*
  93. * Loads our special font CSS file.
  94. *
  95. * The use of Open Sans by default is localized. For languages that use
  96. * characters not supported by the font, the font can be disabled.
  97. *
  98. * To disable in a child theme, use wp_dequeue_style()
  99. * function mytheme_dequeue_fonts() {
  100. * wp_dequeue_style( 'twentytwelve-fonts' );
  101. * }
  102. * add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );
  103. */
  104. /* translators: If there are characters in your language that are not supported
  105. by Open Sans, translate this to 'off'. Do not translate into your own language. */
  106. if ( 'off' !== _x( 'on', 'Open Sans font: on or off', 'twentytwelve' ) ) {
  107. $subsets = 'latin,latin-ext';
  108. /* translators: To add an additional Open Sans character subset specific to your language, translate
  109. this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language. */
  110. $subset = _x( 'no-subset', 'Open Sans font: add new subset (greek, cyrillic, vietnamese)', 'twentytwelve' );
  111. if ( 'cyrillic' == $subset )
  112. $subsets .= ',cyrillic,cyrillic-ext';
  113. elseif ( 'greek' == $subset )
  114. $subsets .= ',greek,greek-ext';
  115. elseif ( 'vietnamese' == $subset )
  116. $subsets .= ',vietnamese';
  117. $protocol = is_ssl() ? 'https' : 'http';
  118. $query_args = array(
  119. 'family' => 'Open+Sans:400italic,700italic,400,700',
  120. 'subset' => $subsets,
  121. );
  122. wp_enqueue_style( 'twentytwelve-fonts', add_query_arg( $query_args, "$protocol://fonts.googleapis.com/css" ), array(), null );
  123. }
  124. /*
  125. * Loads our main stylesheet.
  126. */
  127. wp_enqueue_style( 'twentytwelve-style', get_stylesheet_uri() );
  128. /*
  129. * Loads the Internet Explorer specific stylesheet.
  130. */
  131. wp_enqueue_style( 'twentytwelve-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentytwelve-style' ), '20121010' );
  132. $wp_styles->add_data( 'twentytwelve-ie', 'conditional', 'lt IE 9' );
  133. }
  134. add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts_styles' );
  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 Twenty Twelve 1.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 twentytwelve_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', 'twentytwelve' ), max( $paged, $page ) );
  158. return $title;
  159. }
  160. add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );
  161. /**
  162. * Makes our wp_nav_menu() fallback -- wp_page_menu() -- show a home link.
  163. *
  164. * @since Twenty Twelve 1.0
  165. */
  166. function twentytwelve_page_menu_args( $args ) {
  167. if ( ! isset( $args['show_home'] ) )
  168. $args['show_home'] = true;
  169. return $args;
  170. }
  171. add_filter( 'wp_page_menu_args', 'twentytwelve_page_menu_args' );
  172. /**
  173. * Registers our main widget area and the front page widget areas.
  174. *
  175. * @since Twenty Twelve 1.0
  176. */
  177. function twentytwelve_widgets_init() {
  178. register_sidebar( array(
  179. 'name' => __( 'Main Sidebar', 'twentytwelve' ),
  180. 'id' => 'sidebar-1',
  181. 'description' => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'twentytwelve' ),
  182. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  183. 'after_widget' => '</aside>',
  184. 'before_title' => '<h3 class="widget-title">',
  185. 'after_title' => '</h3>',
  186. ) );
  187. register_sidebar( array(
  188. 'name' => __( 'First Front Page Widget Area', 'twentytwelve' ),
  189. 'id' => 'sidebar-2',
  190. 'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
  191. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  192. 'after_widget' => '</aside>',
  193. 'before_title' => '<h3 class="widget-title">',
  194. 'after_title' => '</h3>',
  195. ) );
  196. register_sidebar( array(
  197. 'name' => __( 'Second Front Page Widget Area', 'twentytwelve' ),
  198. 'id' => 'sidebar-3',
  199. 'description' => __( 'Appears when using the optional Front Page template with a page set as Static Front Page', 'twentytwelve' ),
  200. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  201. 'after_widget' => '</aside>',
  202. 'before_title' => '<h3 class="widget-title">',
  203. 'after_title' => '</h3>',
  204. ) );
  205. }
  206. add_action( 'widgets_init', 'twentytwelve_widgets_init' );
  207. if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
  208. /**
  209. * Displays navigation to next/previous pages when applicable.
  210. *
  211. * @since Twenty Twelve 1.0
  212. */
  213. function twentytwelve_content_nav( $html_id ) {
  214. global $wp_query;
  215. $html_id = esc_attr( $html_id );
  216. if ( $wp_query->max_num_pages > 1 ) : ?>
  217. <nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
  218. <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
  219. <div class="nav-previous alignleft"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' ) ); ?></div>
  220. <div class="nav-next alignright"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?></div>
  221. </nav><!-- #<?php echo $html_id; ?> .navigation -->
  222. <?php endif;
  223. }
  224. endif;
  225. if ( ! function_exists( 'twentytwelve_comment' ) ) :
  226. /**
  227. * Template for comments and pingbacks.
  228. *
  229. * To override this walker in a child theme without modifying the comments template
  230. * simply create your own twentytwelve_comment(), and that function will be used instead.
  231. *
  232. * Used as a callback by wp_list_comments() for displaying the comments.
  233. *
  234. * @since Twenty Twelve 1.0
  235. */
  236. function twentytwelve_comment( $comment, $args, $depth ) {
  237. $GLOBALS['comment'] = $comment;
  238. switch ( $comment->comment_type ) :
  239. case 'pingback' :
  240. case 'trackback' :
  241. // Display trackbacks differently than normal comments.
  242. ?>
  243. <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
  244. <p><?php _e( 'Pingback:', 'twentytwelve' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></p>
  245. <?php
  246. break;
  247. default :
  248. // Proceed with normal comments.
  249. global $post;
  250. ?>
  251. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  252. <article id="comment-<?php comment_ID(); ?>" class="comment">
  253. <header class="comment-meta comment-author vcard">
  254. <?php
  255. echo get_avatar( $comment, 44 );
  256. printf( '<cite class="fn">%1$s %2$s</cite>',
  257. get_comment_author_link(),
  258. // If current post author is also comment author, make it known visually.
  259. ( $comment->user_id === $post->post_author ) ? '<span> ' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
  260. );
  261. printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
  262. esc_url( get_comment_link( $comment->comment_ID ) ),
  263. get_comment_time( 'c' ),
  264. /* translators: 1: date, 2: time */
  265. sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
  266. );
  267. ?>
  268. </header><!-- .comment-meta -->
  269. <?php if ( '0' == $comment->comment_approved ) : ?>
  270. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwelve' ); ?></p>
  271. <?php endif; ?>
  272. <section class="comment-content comment">
  273. <?php comment_text(); ?>
  274. <?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<p class="edit-link">', '</p>' ); ?>
  275. </section><!-- .comment-content -->
  276. <div class="reply">
  277. <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'twentytwelve' ), 'after' => ' <span>&darr;</span>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  278. </div><!-- .reply -->
  279. </article><!-- #comment-## -->
  280. <?php
  281. break;
  282. endswitch; // end comment_type check
  283. }
  284. endif;
  285. if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
  286. /**
  287. * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
  288. *
  289. * Create your own twentytwelve_entry_meta() to override in a child theme.
  290. *
  291. * @since Twenty Twelve 1.0
  292. */
  293. function twentytwelve_entry_meta() {
  294. // Translators: used between list items, there is a space after the comma.
  295. $categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );
  296. // Translators: used between list items, there is a space after the comma.
  297. $tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) );
  298. $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
  299. esc_url( get_permalink() ),
  300. esc_attr( get_the_time() ),
  301. esc_attr( get_the_date( 'c' ) ),
  302. esc_html( get_the_date() )
  303. );
  304. $author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
  305. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  306. esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
  307. get_the_author()
  308. );
  309. // Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
  310. if ( $tag_list ) {
  311. $utility_text = __( 'This entry was posted in %1$s and tagged %2$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
  312. } elseif ( $categories_list ) {
  313. $utility_text = __( 'This entry was posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
  314. } else {
  315. $utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
  316. }
  317. printf(
  318. $utility_text,
  319. $categories_list,
  320. $tag_list,
  321. $date,
  322. $author
  323. );
  324. }
  325. endif;
  326. /**
  327. * Extends the default WordPress body class to denote:
  328. * 1. Using a full-width layout, when no active widgets in the sidebar
  329. * or full-width template.
  330. * 2. Front Page template: thumbnail in use and number of sidebars for
  331. * widget areas.
  332. * 3. White or empty background color to change the layout and spacing.
  333. * 4. Custom fonts enabled.
  334. * 5. Single or multiple authors.
  335. *
  336. * @since Twenty Twelve 1.0
  337. *
  338. * @param array Existing class values.
  339. * @return array Filtered class values.
  340. */
  341. function twentytwelve_body_class( $classes ) {
  342. $background_color = get_background_color();
  343. if ( ! is_active_sidebar( 'sidebar-1' ) || is_page_template( 'page-templates/full-width.php' ) )
  344. $classes[] = 'full-width';
  345. if ( is_page_template( 'page-templates/front-page.php' ) ) {
  346. $classes[] = 'template-front-page';
  347. if ( has_post_thumbnail() )
  348. $classes[] = 'has-post-thumbnail';
  349. if ( is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) )
  350. $classes[] = 'two-sidebars';
  351. }
  352. if ( empty( $background_color ) )
  353. $classes[] = 'custom-background-empty';
  354. elseif ( in_array( $background_color, array( 'fff', 'ffffff' ) ) )
  355. $classes[] = 'custom-background-white';
  356. // Enable custom font class only if the font CSS is queued to load.
  357. if ( wp_style_is( 'twentytwelve-fonts', 'queue' ) )
  358. $classes[] = 'custom-font-enabled';
  359. if ( ! is_multi_author() )
  360. $classes[] = 'single-author';
  361. return $classes;
  362. }
  363. add_filter( 'body_class', 'twentytwelve_body_class' );
  364. /**
  365. * Adjusts content_width value for full-width and single image attachment
  366. * templates, and when there are no active widgets in the sidebar.
  367. *
  368. * @since Twenty Twelve 1.0
  369. */
  370. function twentytwelve_content_width() {
  371. if ( is_page_template( 'page-templates/full-width.php' ) || is_attachment() || ! is_active_sidebar( 'sidebar-1' ) ) {
  372. global $content_width;
  373. $content_width = 960;
  374. }
  375. }
  376. add_action( 'template_redirect', 'twentytwelve_content_width' );
  377. /**
  378. * Add postMessage support for site title and description for the Theme Customizer.
  379. *
  380. * @since Twenty Twelve 1.0
  381. *
  382. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  383. * @return void
  384. */
  385. function twentytwelve_customize_register( $wp_customize ) {
  386. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  387. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  388. }
  389. add_action( 'customize_register', 'twentytwelve_customize_register' );
  390. /**
  391. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  392. *
  393. * @since Twenty Twelve 1.0
  394. */
  395. function twentytwelve_customize_preview_js() {
  396. wp_enqueue_script( 'twentytwelve-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20120827', true );
  397. }
  398. add_action( 'customize_preview_init', 'twentytwelve_customize_preview_js' );