Advertisement
Guest User

function.php

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