Advertisement
gblitzer

function-php

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