[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * TwentyTen 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, twentyten_setup(), sets up the theme by registering support
  10   * for various features in WordPress, such as post thumbnails, navigation menus, and the like.
  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. The hook can be removed by using remove_action() or
  20   * remove_filter() and you can attach your own function to the hook.
  21   *
  22   * We can remove the parent theme's hook only after it is attached, which means we need to
  23   * wait until setting up the child theme:
  24   *
  25   * <code>
  26   * add_action( 'after_setup_theme', 'my_child_theme_setup' );
  27   * function my_child_theme_setup() {
  28   *     // We are providing our own filter for excerpt_length (or using the unfiltered value)
  29   *     remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
  30   *     ...
  31   * }
  32   * </code>
  33   *
  34   * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
  35   *
  36   * @package WordPress
  37   * @subpackage Twenty_Ten
  38   * @since Twenty Ten 1.0
  39   */
  40  
  41  /**
  42   * Set the content width based on the theme's design and stylesheet.
  43   *
  44   * Used to set the width of images and content. Should be equal to the width the theme
  45   * is designed for, generally via the style.css stylesheet.
  46   */
  47  if ( ! isset( $content_width ) )
  48      $content_width = 640;
  49  
  50  /** Tell WordPress to run twentyten_setup() when the 'after_setup_theme' hook is run. */
  51  add_action( 'after_setup_theme', 'twentyten_setup' );
  52  
  53  if ( ! function_exists( 'twentyten_setup' ) ):
  54  /**
  55   * Sets up theme defaults and registers support for various WordPress features.
  56   *
  57   * Note that this function is hooked into the after_setup_theme hook, which runs
  58   * before the init hook. The init hook is too late for some features, such as indicating
  59   * support post thumbnails.
  60   *
  61   * To override twentyten_setup() in a child theme, add your own twentyten_setup to your child theme's
  62   * functions.php file.
  63   *
  64   * @uses add_theme_support() To add support for post thumbnails, custom headers and backgrounds, and automatic feed links.
  65   * @uses register_nav_menus() To add support for navigation menus.
  66   * @uses add_editor_style() To style the visual editor.
  67   * @uses load_theme_textdomain() For translation/localization support.
  68   * @uses register_default_headers() To register the default custom header images provided with the theme.
  69   * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
  70   *
  71   * @since Twenty Ten 1.0
  72   */
  73  function twentyten_setup() {
  74  
  75      // This theme styles the visual editor with editor-style.css to match the theme style.
  76      add_editor_style();
  77  
  78      // Post Format support. You can also use the legacy "gallery" or "asides" (note the plural) categories.
  79      add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
  80  
  81      // This theme uses post thumbnails
  82      add_theme_support( 'post-thumbnails' );
  83  
  84      // Add default posts and comments RSS feed links to head
  85      add_theme_support( 'automatic-feed-links' );
  86  
  87      // Make theme available for translation
  88      // Translations can be filed in the /languages/ directory
  89      load_theme_textdomain( 'twentyten', get_template_directory() . '/languages' );
  90  
  91      // This theme uses wp_nav_menu() in one location.
  92      register_nav_menus( array(
  93          'primary' => __( 'Primary Navigation', 'twentyten' ),
  94      ) );
  95  
  96      // This theme allows users to set a custom background.
  97      add_theme_support( 'custom-background' );
  98  
  99      // The custom header business starts here.
 100  
 101      add_theme_support( 'custom-header', array(
 102          // The default image to use.
 103          // The %s is a placeholder for the theme template directory URI.
 104          'default-image' => '%s/images/headers/path.jpg',
 105          // The height and width of our custom header.
 106          'width' => apply_filters( 'twentyten_header_image_width', 940 ),
 107          'height' => apply_filters( 'twentyten_header_image_height', 198 ),
 108          // Support flexible heights.
 109          'flex-height' => true,
 110          // Don't support text inside the header image.
 111          'header-text' => false,
 112          // Callback for styling the header preview in the admin.
 113          'admin-head-callback' => 'twentyten_admin_header_style',
 114      ) );
 115  
 116      // We'll be using post thumbnails for custom header images on posts and pages.
 117      // We want them to be 940 pixels wide by 198 pixels tall.
 118      // Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
 119      set_post_thumbnail_size( get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ), true );
 120  
 121      // ... and thus ends the custom header business.
 122  
 123      // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
 124      register_default_headers( array(
 125          'berries' => array(
 126              'url' => '%s/images/headers/berries.jpg',
 127              'thumbnail_url' => '%s/images/headers/berries-thumbnail.jpg',
 128              /* translators: header image description */
 129              'description' => __( 'Berries', 'twentyten' )
 130          ),
 131          'cherryblossom' => array(
 132              'url' => '%s/images/headers/cherryblossoms.jpg',
 133              'thumbnail_url' => '%s/images/headers/cherryblossoms-thumbnail.jpg',
 134              /* translators: header image description */
 135              'description' => __( 'Cherry Blossoms', 'twentyten' )
 136          ),
 137          'concave' => array(
 138              'url' => '%s/images/headers/concave.jpg',
 139              'thumbnail_url' => '%s/images/headers/concave-thumbnail.jpg',
 140              /* translators: header image description */
 141              'description' => __( 'Concave', 'twentyten' )
 142          ),
 143          'fern' => array(
 144              'url' => '%s/images/headers/fern.jpg',
 145              'thumbnail_url' => '%s/images/headers/fern-thumbnail.jpg',
 146              /* translators: header image description */
 147              'description' => __( 'Fern', 'twentyten' )
 148          ),
 149          'forestfloor' => array(
 150              'url' => '%s/images/headers/forestfloor.jpg',
 151              'thumbnail_url' => '%s/images/headers/forestfloor-thumbnail.jpg',
 152              /* translators: header image description */
 153              'description' => __( 'Forest Floor', 'twentyten' )
 154          ),
 155          'inkwell' => array(
 156              'url' => '%s/images/headers/inkwell.jpg',
 157              'thumbnail_url' => '%s/images/headers/inkwell-thumbnail.jpg',
 158              /* translators: header image description */
 159              'description' => __( 'Inkwell', 'twentyten' )
 160          ),
 161          'path' => array(
 162              'url' => '%s/images/headers/path.jpg',
 163              'thumbnail_url' => '%s/images/headers/path-thumbnail.jpg',
 164              /* translators: header image description */
 165              'description' => __( 'Path', 'twentyten' )
 166          ),
 167          'sunset' => array(
 168              'url' => '%s/images/headers/sunset.jpg',
 169              'thumbnail_url' => '%s/images/headers/sunset-thumbnail.jpg',
 170              /* translators: header image description */
 171              'description' => __( 'Sunset', 'twentyten' )
 172          )
 173      ) );
 174  }
 175  endif;
 176  
 177  if ( ! function_exists( 'twentyten_admin_header_style' ) ) :
 178  /**
 179   * Styles the header image displayed on the Appearance > Header admin panel.
 180   *
 181   * Referenced via add_custom_image_header() in twentyten_setup().
 182   *
 183   * @since Twenty Ten 1.0
 184   */
 185  function twentyten_admin_header_style() {
 186  ?>
 187  <style type="text/css">
 188  /* Shows the same border as on front end */
 189  #headimg {
 190      border-bottom: 1px solid #000;
 191      border-top: 4px solid #000;
 192  }
 193  /* If header-text was supported, you would style the text with these selectors:
 194      #headimg #name { }
 195      #headimg #desc { }
 196  */
 197  </style>
 198  <?php
 199  }
 200  endif;
 201  
 202  /**
 203   * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
 204   *
 205   * To override this in a child theme, remove the filter and optionally add
 206   * your own function tied to the wp_page_menu_args filter hook.
 207   *
 208   * @since Twenty Ten 1.0
 209   */
 210  function twentyten_page_menu_args( $args ) {
 211      $args['show_home'] = true;
 212      return $args;
 213  }
 214  add_filter( 'wp_page_menu_args', 'twentyten_page_menu_args' );
 215  
 216  /**
 217   * Sets the post excerpt length to 40 characters.
 218   *
 219   * To override this length in a child theme, remove the filter and add your own
 220   * function tied to the excerpt_length filter hook.
 221   *
 222   * @since Twenty Ten 1.0
 223   * @return int
 224   */
 225  function twentyten_excerpt_length( $length ) {
 226      return 40;
 227  }
 228  add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
 229  
 230  /**
 231   * Returns a "Continue Reading" link for excerpts
 232   *
 233   * @since Twenty Ten 1.0
 234   * @return string "Continue Reading" link
 235   */
 236  function twentyten_continue_reading_link() {
 237      return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
 238  }
 239  
 240  /**
 241   * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyten_continue_reading_link().
 242   *
 243   * To override this in a child theme, remove the filter and add your own
 244   * function tied to the excerpt_more filter hook.
 245   *
 246   * @since Twenty Ten 1.0
 247   * @return string An ellipsis
 248   */
 249  function twentyten_auto_excerpt_more( $more ) {
 250      return ' &hellip;' . twentyten_continue_reading_link();
 251  }
 252  add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
 253  
 254  /**
 255   * Adds a pretty "Continue Reading" link to custom post excerpts.
 256   *
 257   * To override this link in a child theme, remove the filter and add your own
 258   * function tied to the get_the_excerpt filter hook.
 259   *
 260   * @since Twenty Ten 1.0
 261   * @return string Excerpt with a pretty "Continue Reading" link
 262   */
 263  function twentyten_custom_excerpt_more( $output ) {
 264      if ( has_excerpt() && ! is_attachment() ) {
 265          $output .= twentyten_continue_reading_link();
 266      }
 267      return $output;
 268  }
 269  add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
 270  
 271  /**
 272   * Remove inline styles printed when the gallery shortcode is used.
 273   *
 274   * Galleries are styled by the theme in Twenty Ten's style.css. This is just
 275   * a simple filter call that tells WordPress to not use the default styles.
 276   *
 277   * @since Twenty Ten 1.2
 278   */
 279  add_filter( 'use_default_gallery_style', '__return_false' );
 280  
 281  /**
 282   * Deprecated way to remove inline styles printed when the gallery shortcode is used.
 283   *
 284   * This function is no longer needed or used. Use the use_default_gallery_style
 285   * filter instead, as seen above.
 286   *
 287   * @since Twenty Ten 1.0
 288   * @deprecated Deprecated in Twenty Ten 1.2 for WordPress 3.1
 289   *
 290   * @return string The gallery style filter, with the styles themselves removed.
 291   */
 292  function twentyten_remove_gallery_css( $css ) {
 293      return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
 294  }
 295  // Backwards compatibility with WordPress 3.0.
 296  if ( version_compare( $GLOBALS['wp_version'], '3.1', '<' ) )
 297      add_filter( 'gallery_style', 'twentyten_remove_gallery_css' );
 298  
 299  if ( ! function_exists( 'twentyten_comment' ) ) :
 300  /**
 301   * Template for comments and pingbacks.
 302   *
 303   * To override this walker in a child theme without modifying the comments template
 304   * simply create your own twentyten_comment(), and that function will be used instead.
 305   *
 306   * Used as a callback by wp_list_comments() for displaying the comments.
 307   *
 308   * @since Twenty Ten 1.0
 309   */
 310  function twentyten_comment( $comment, $args, $depth ) {
 311      $GLOBALS['comment'] = $comment;
 312      switch ( $comment->comment_type ) :
 313          case '' :
 314      ?>
 315      <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
 316          <div id="comment-<?php comment_ID(); ?>">
 317          <div class="comment-author vcard">
 318              <?php echo get_avatar( $comment, 40 ); ?>
 319              <?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
 320          </div><!-- .comment-author .vcard -->
 321          <?php if ( $comment->comment_approved == '0' ) : ?>
 322              <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
 323              <br />
 324          <?php endif; ?>
 325  
 326          <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
 327              <?php
 328                  /* translators: 1: date, 2: time */
 329                  printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
 330              ?>
 331          </div><!-- .comment-meta .commentmetadata -->
 332  
 333          <div class="comment-body"><?php comment_text(); ?></div>
 334  
 335          <div class="reply">
 336              <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
 337          </div><!-- .reply -->
 338      </div><!-- #comment-##  -->
 339  
 340      <?php
 341              break;
 342          case 'pingback'  :
 343          case 'trackback' :
 344      ?>
 345      <li class="post pingback">
 346          <p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' ); ?></p>
 347      <?php
 348              break;
 349      endswitch;
 350  }
 351  endif;
 352  
 353  /**
 354   * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
 355   *
 356   * To override twentyten_widgets_init() in a child theme, remove the action hook and add your own
 357   * function tied to the init hook.
 358   *
 359   * @since Twenty Ten 1.0
 360   * @uses register_sidebar
 361   */
 362  function twentyten_widgets_init() {
 363      // Area 1, located at the top of the sidebar.
 364      register_sidebar( array(
 365          'name' => __( 'Primary Widget Area', 'twentyten' ),
 366          'id' => 'primary-widget-area',
 367          'description' => __( 'The primary widget area', 'twentyten' ),
 368          'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
 369          'after_widget' => '</li>',
 370          'before_title' => '<h3 class="widget-title">',
 371          'after_title' => '</h3>',
 372      ) );
 373  
 374      // Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
 375      register_sidebar( array(
 376          'name' => __( 'Secondary Widget Area', 'twentyten' ),
 377          'id' => 'secondary-widget-area',
 378          'description' => __( 'The secondary widget area', 'twentyten' ),
 379          'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
 380          'after_widget' => '</li>',
 381          'before_title' => '<h3 class="widget-title">',
 382          'after_title' => '</h3>',
 383      ) );
 384  
 385      // Area 3, located in the footer. Empty by default.
 386      register_sidebar( array(
 387          'name' => __( 'First Footer Widget Area', 'twentyten' ),
 388          'id' => 'first-footer-widget-area',
 389          'description' => __( 'The first footer widget area', 'twentyten' ),
 390          'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
 391          'after_widget' => '</li>',
 392          'before_title' => '<h3 class="widget-title">',
 393          'after_title' => '</h3>',
 394      ) );
 395  
 396      // Area 4, located in the footer. Empty by default.
 397      register_sidebar( array(
 398          'name' => __( 'Second Footer Widget Area', 'twentyten' ),
 399          'id' => 'second-footer-widget-area',
 400          'description' => __( 'The second footer widget area', 'twentyten' ),
 401          'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
 402          'after_widget' => '</li>',
 403          'before_title' => '<h3 class="widget-title">',
 404          'after_title' => '</h3>',
 405      ) );
 406  
 407      // Area 5, located in the footer. Empty by default.
 408      register_sidebar( array(
 409          'name' => __( 'Third Footer Widget Area', 'twentyten' ),
 410          'id' => 'third-footer-widget-area',
 411          'description' => __( 'The third footer widget area', 'twentyten' ),
 412          'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
 413          'after_widget' => '</li>',
 414          'before_title' => '<h3 class="widget-title">',
 415          'after_title' => '</h3>',
 416      ) );
 417  
 418      // Area 6, located in the footer. Empty by default.
 419      register_sidebar( array(
 420          'name' => __( 'Fourth Footer Widget Area', 'twentyten' ),
 421          'id' => 'fourth-footer-widget-area',
 422          'description' => __( 'The fourth footer widget area', 'twentyten' ),
 423          'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
 424          'after_widget' => '</li>',
 425          'before_title' => '<h3 class="widget-title">',
 426          'after_title' => '</h3>',
 427      ) );
 428  }
 429  /** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
 430  add_action( 'widgets_init', 'twentyten_widgets_init' );
 431  
 432  /**
 433   * Removes the default styles that are packaged with the Recent Comments widget.
 434   *
 435   * To override this in a child theme, remove the filter and optionally add your own
 436   * function tied to the widgets_init action hook.
 437   *
 438   * This function uses a filter (show_recent_comments_widget_style) new in WordPress 3.1
 439   * to remove the default style. Using Twenty Ten 1.2 in WordPress 3.0 will show the styles,
 440   * but they won't have any effect on the widget in default Twenty Ten styling.
 441   *
 442   * @since Twenty Ten 1.0
 443   */
 444  function twentyten_remove_recent_comments_style() {
 445      add_filter( 'show_recent_comments_widget_style', '__return_false' );
 446  }
 447  add_action( 'widgets_init', 'twentyten_remove_recent_comments_style' );
 448  
 449  if ( ! function_exists( 'twentyten_posted_on' ) ) :
 450  /**
 451   * Prints HTML with meta information for the current post-date/time and author.
 452   *
 453   * @since Twenty Ten 1.0
 454   */
 455  function twentyten_posted_on() {
 456      printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
 457          'meta-prep meta-prep-author',
 458          sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
 459              get_permalink(),
 460              esc_attr( get_the_time() ),
 461              get_the_date()
 462          ),
 463          sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
 464              get_author_posts_url( get_the_author_meta( 'ID' ) ),
 465              esc_attr( sprintf( __( 'View all posts by %s', 'twentyten' ), get_the_author() ) ),
 466              get_the_author()
 467          )
 468      );
 469  }
 470  endif;
 471  
 472  if ( ! function_exists( 'twentyten_posted_in' ) ) :
 473  /**
 474   * Prints HTML with meta information for the current post (category, tags and permalink).
 475   *
 476   * @since Twenty Ten 1.0
 477   */
 478  function twentyten_posted_in() {
 479      // Retrieves tag list of current post, separated by commas.
 480      $tag_list = get_the_tag_list( '', ', ' );
 481      if ( $tag_list ) {
 482          $posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
 483      } elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
 484          $posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
 485      } else {
 486          $posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
 487      }
 488      // Prints the string, replacing the placeholders.
 489      printf(
 490          $posted_in,
 491          get_the_category_list( ', ' ),
 492          $tag_list,
 493          get_permalink(),
 494          the_title_attribute( 'echo=0' )
 495      );
 496  }
 497  endif;


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.