[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-content/themes/twentytwelve/ -> functions.php (source)

   1  <?php
   2  /**
   3   * Twenty Twelve 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, twentytwelve_setup(), sets up the theme by registering support
  10   * for various features in WordPress, such as a custom background and a navigation menu.
  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.
  20   *
  21   * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
  22   *
  23   * @package WordPress
  24   * @subpackage Twenty_Twelve
  25   * @since Twenty Twelve 1.0
  26   */
  27  
  28  /**
  29   * Set the content width based on the theme's design and stylesheet.
  30   */
  31  if ( ! isset( $content_width ) )
  32      $content_width = 584;
  33  
  34  /**
  35   * Tell WordPress to run twentytwelve_setup() when the 'after_setup_theme' hook is run.
  36   */
  37  add_action( 'after_setup_theme', 'twentytwelve_setup' );
  38  
  39  if ( ! function_exists( 'twentytwelve_setup' ) ) :
  40  /**
  41   * Sets up theme defaults and registers support for various WordPress features.
  42   *
  43   * @uses load_theme_textdomain() For translation/localization support.
  44   * @uses add_theme_support() To add support for automatic feed links.
  45   * @uses register_nav_menu() To add support for navigation menus.
  46   * @uses add_custom_background() To add support for a custom background.
  47   *
  48   * @since Twenty Twelve 1.0
  49   */
  50  function twentytwelve_setup() {
  51      /**
  52       * Make Twenty Twelve available for translation.
  53       * Translations can be added to the /languages/ directory.
  54       * If you're building a theme based on Twenty Twelve, use a find and replace
  55       * to change 'twentytwelve' to the name of your theme in all the template files.
  56       */
  57      load_theme_textdomain( 'twentytwelve', get_template_directory() . '/languages' );
  58  
  59      // Add default posts and comments RSS feed links to <head>.
  60      add_theme_support( 'automatic-feed-links' );
  61  
  62      // Add support for a variety of post formats
  63      add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote' ) );
  64  
  65      // This theme uses wp_nav_menu() in one location.
  66      register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ) );
  67  
  68      // Add support for custom background.
  69      add_custom_background();
  70  
  71      // Add support for a custom header image.
  72      $header_args = array(
  73          'random-default' => true,
  74          'flex-height' => true,
  75          'suggested-height' => apply_filters( 'twentytwelve_header_image_height', 250 ),
  76          'flex-width' => true,
  77          'max-width' => apply_filters( 'twentytwelve_header_image_max_width', 2000 ),
  78          'suggested-width' => apply_filters( 'twentytwelve_header_image_width', 960 ),
  79      );
  80      add_theme_support( 'custom-header', $header_args );
  81      add_custom_image_header( 'twentytwelve_header_style', 'twentytwelve_admin_header_style', 'twentytwelve_admin_header_image' );
  82  
  83      // The default header text color
  84      define( 'HEADER_TEXTCOLOR', '444' );
  85  }
  86  endif;
  87  
  88  if ( ! function_exists( 'twentytwelve_header_style' ) ) :
  89  /**
  90   * Styles the header image and text displayed on the blog
  91   *
  92   * get_header_textcolor() options: HEADER_TEXTCOLOR is default, hide text (returns 'blank'), or any hex value
  93   *
  94   * @since Twenty Twelve 1.0
  95   */
  96  function twentytwelve_header_style() {
  97      // If no custom options for text are set, let's bail
  98      if ( HEADER_TEXTCOLOR == get_header_textcolor() )
  99          return;
 100      // If we get this far, we have custom styles.
 101      ?>
 102      <style type="text/css">
 103      <?php
 104          // Has the text been hidden?
 105          if ( 'blank' == get_header_textcolor() ) :
 106      ?>
 107          .site-title,
 108          .site-description {
 109              position: absolute !important;
 110              clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
 111              clip: rect(1px, 1px, 1px, 1px);
 112          }
 113      <?php
 114          // If the user has set a custom color for the text, use that.
 115          else :
 116      ?>
 117          .site-title a,
 118          .site-description {
 119              color: #<?php echo get_header_textcolor(); ?> !important;
 120          }
 121      <?php endif; ?>
 122      </style>
 123      <?php
 124  }
 125  endif;
 126  
 127  if ( ! function_exists( 'twentytwelve_admin_header_style' ) ) :
 128  /**
 129   * Styles the header image displayed on the Appearance > Header admin panel.
 130   *
 131   * Referenced via add_custom_image_header() in twentytwelve_setup().
 132   *
 133   * @since Twenty Twelve 1.0
 134   */
 135  function twentytwelve_admin_header_style() {
 136  ?>
 137      <style type="text/css">
 138      .appearance_page_custom-header #headimg {
 139          border: none;
 140      }
 141      #headimg h1,
 142      #headimg h2 {
 143          line-height: 1.6;
 144          margin: 0;
 145          padding: 0;
 146      }
 147      #headimg h1 {
 148          font-size: 30px;
 149      }
 150      #headimg h1 a {
 151          text-decoration: none;
 152      }
 153      #headimg h2 {
 154          font: normal 13px/1.8 "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif;
 155          margin-bottom: 24px;
 156      }
 157      #headimg img {
 158      }
 159      </style>
 160  <?php
 161  }
 162  endif;
 163  
 164  if ( ! function_exists( 'twentytwelve_admin_header_image' ) ) :
 165  /**
 166   * Custom header image markup displayed on the Appearance > Header admin panel.
 167   *
 168   * Referenced via add_custom_image_header() in twentytwelve_setup().
 169   *
 170   * @since Twenty Twelve 1.0
 171   */
 172  function twentytwelve_admin_header_image() { ?>
 173      <div id="headimg">
 174          <?php
 175          if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || '' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) )
 176              $style = ' style="display:none;"';
 177          else
 178              $style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
 179          ?>
 180          <h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
 181          <h2 id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></h2>
 182          <?php $header_image = get_header_image();
 183          if ( ! empty( $header_image ) ) : ?>
 184              <img src="<?php echo esc_url( $header_image ); ?>" alt="" />
 185          <?php endif; ?>
 186      </div>
 187  <?php }
 188  endif;
 189  
 190  /**
 191   * Enqueue script for handling navigation.
 192   *
 193   * @since Twenty Twelve 1.0
 194   */
 195  function twentytwelve_scripts() {
 196      wp_enqueue_script( 'jquery' );
 197      wp_enqueue_script( 'navigation', get_template_directory_uri() . '/js/navigation.js', 'jquery', '20120227', true );
 198  }
 199  add_action( 'wp_enqueue_scripts', 'twentytwelve_scripts' );
 200  
 201  /**
 202   * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
 203   *
 204   * @since Twenty Twelve 1.0
 205   */
 206  function twentytwelve_page_menu_args( $args ) {
 207      $args['show_home'] = true;
 208      return $args;
 209  }
 210  add_filter( 'wp_page_menu_args', 'twentytwelve_page_menu_args' );
 211  
 212  /**
 213   * Register our single widget area.
 214   *
 215   * @since Twenty Twelve 1.0
 216   */
 217  function twentytwelve_widgets_init() {
 218      register_sidebar( array(
 219          'name' => __( 'Main Sidebar', 'twentytwelve' ),
 220          'id' => 'sidebar-1',
 221          'before_widget' => '<aside id="%1$s" class="widget %2$s">',
 222          'after_widget' => "</aside>",
 223          'before_title' => '<h3 class="widget-title">',
 224          'after_title' => '</h3>',
 225      ) );
 226  }
 227  add_action( 'widgets_init', 'twentytwelve_widgets_init' );
 228  
 229  if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
 230  /**
 231   * Display navigation to next/previous pages when applicable
 232   *
 233   * @since Twenty Twelve 1.0
 234   */
 235  function twentytwelve_content_nav( $nav_id ) {
 236      global $wp_query;
 237  
 238      if ( $wp_query->max_num_pages > 1 ) : ?>
 239          <nav id="<?php echo $nav_id; ?>" role="navigation">
 240              <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
 241              <div class="nav-previous alignleft"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentytwelve' ) ); ?></div>
 242              <div class="nav-next alignright"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?></div>
 243          </nav><!-- #nav-above -->
 244      <?php endif;
 245  }
 246  endif;
 247  
 248  if ( ! function_exists( 'twentytwelve_comment' ) ) :
 249  /**
 250   * Template for comments and pingbacks.
 251   *
 252   * To override this walker in a child theme without modifying the comments template
 253   * simply create your own twentytwelve_comment(), and that function will be used instead.
 254   *
 255   * Used as a callback by wp_list_comments() for displaying the comments.
 256   *
 257   * @since Twenty Twelve 1.0
 258   */
 259  function twentytwelve_comment( $comment, $args, $depth ) {
 260      $GLOBALS['comment'] = $comment;
 261      switch ( $comment->comment_type ) :
 262          case 'pingback' :
 263          case 'trackback' :
 264          // Display trackbacks differently than normal comments.
 265      ?>
 266      <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
 267          <p><?php _e( 'Pingback:', 'twentytwelve' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></p>
 268      <?php
 269              break;
 270          default :
 271          // Proceed with normal comments.
 272      ?>
 273      <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
 274          <article id="comment-<?php comment_ID(); ?>" class="comment">
 275              <header class="comment-meta comment-author vcard">
 276                  <?php
 277                      echo get_avatar( $comment, 44 );
 278  
 279                      printf( '<cite class="fn">%s</cite>', get_comment_author_link() );
 280                      printf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
 281                          esc_url( get_comment_link( $comment->comment_ID ) ),
 282                          get_comment_time( 'c' ),
 283                          /* translators: 1: date, 2: time */
 284                          sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
 285                      );
 286                  ?>
 287                  <?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?>
 288              </header>
 289  
 290              <?php if ( '0' == $comment->comment_approved ) : ?>
 291                  <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwelve' ); ?></p>
 292              <?php endif; ?>
 293  
 294              <section class="comment post-content">
 295                  <?php comment_text(); ?>
 296              </section>
 297  
 298              <div class="reply">
 299                  <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>&darr;</span>', 'twentytwelve' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
 300              </div><!-- .reply -->
 301          </article><!-- #comment-## -->
 302      <?php
 303          break;
 304      endswitch; // end comment_type check
 305  }
 306  endif;
 307  
 308  if ( ! function_exists( 'twentytwelve_posted_on' ) ) :
 309  /**
 310   * Prints HTML with meta information for the current post-date/time and author.
 311   *
 312   * Create your own twentytwelve_posted_on() to override in a child theme.
 313   *
 314   * @since Twenty Twelve 1.0
 315   */
 316  function twentytwelve_posted_on() {
 317      printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentytwelve' ),
 318          esc_url( get_permalink() ),
 319          esc_attr( get_the_time() ),
 320          esc_attr( get_the_date( 'c' ) ),
 321          esc_html( get_the_date() ),
 322          esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 323          esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
 324          get_the_author()
 325      );
 326  }
 327  endif;
 328  
 329  /**
 330   * Extends the default WordPress body class to denote a full-width layout.
 331   *
 332   * Used in two cases: no active widgets in sidebar, and full-width page template.
 333   *
 334   * @since Twenty Twelve 1.0
 335   */
 336  function twentytwelve_body_class( $classes ) {
 337      if ( ! is_active_sidebar( 'sidebar-1' ) || is_page_template( 'full-width' ) )
 338          $classes[] = 'full-width';
 339  
 340      return $classes;
 341  }
 342  add_filter( 'body_class', 'twentytwelve_body_class' );


Generated: Tue Mar 20 03:55:56 2012 Hosted by follow the white rabbit.