3 * TwentyTen functions and definitions
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.
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.
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.
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.
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:
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' );
34 * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
37 * @subpackage Twenty_Ten
38 * @since Twenty Ten 1.0
42 * Set the content width based on the theme's design and stylesheet.
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.
47 if ( ! isset( $content_width ) )
50 /** Tell WordPress to run twentyten_setup() when the 'after_setup_theme' hook is run. */
51 add_action( 'after_setup_theme', 'twentyten_setup' );
53 if ( ! function_exists( 'twentyten_setup' ) ):
55 * Sets up theme defaults and registers support for various WordPress features.
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.
61 * To override twentyten_setup() in a child theme, add your own twentyten_setup to your child theme's
64 * @uses add_theme_support() To add support for post thumbnails and automatic feed links.
65 * @uses register_nav_menus() To add support for navigation menus.
66 * @uses add_custom_background() To add support for a custom background.
67 * @uses add_editor_style() To style the visual editor.
68 * @uses load_theme_textdomain() For translation/localization support.
69 * @uses add_custom_image_header() To add support for a custom header.
70 * @uses register_default_headers() To register the default custom header images provided with the theme.
71 * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
73 * @since Twenty Ten 1.0
75 function twentyten_setup() {
77 // This theme styles the visual editor with editor-style.css to match the theme style.
80 // This theme uses post thumbnails
81 add_theme_support( 'post-thumbnails' );
83 // Add default posts and comments RSS feed links to head
84 add_theme_support( 'automatic-feed-links' );
86 // Make theme available for translation
87 // Translations can be filed in the /languages/ directory
88 load_theme_textdomain( 'twentyten', TEMPLATEPATH . '/languages' );
90 $locale = get_locale();
91 $locale_file = TEMPLATEPATH . "/languages/$locale.php";
92 if ( is_readable( $locale_file ) )
93 require_once( $locale_file );
95 // This theme uses wp_nav_menu() in one location.
96 register_nav_menus( array(
97 'primary' => __( 'Primary Navigation', 'twentyten' ),
100 // This theme allows users to set a custom background
101 add_custom_background();
103 // Your changeable header business starts here
104 define( 'HEADER_TEXTCOLOR', '' );
105 // No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
106 define( 'HEADER_IMAGE', '%s/images/headers/path.jpg' );
108 // The height and width of your custom header. You can hook into the theme's own filters to change these values.
109 // Add a filter to twentyten_header_image_width and twentyten_header_image_height to change these values.
110 define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyten_header_image_width', 940 ) );
111 define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyten_header_image_height', 198 ) );
113 // We'll be using post thumbnails for custom header images on posts and pages.
114 // We want them to be 940 pixels wide by 198 pixels tall.
115 // Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
116 set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
118 // Don't support text inside the header image.
119 define( 'NO_HEADER_TEXT', true );
121 // Add a way for the custom header to be styled in the admin panel that controls
122 // custom headers. See twentyten_admin_header_style(), below.
123 add_custom_image_header( '', 'twentyten_admin_header_style' );
125 // ... and thus ends the changeable header business.
127 // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
128 register_default_headers( array(
130 'url' => '%s/images/headers/berries.jpg',
131 'thumbnail_url' => '%s/images/headers/berries-thumbnail.jpg',
132 /* translators: header image description */
133 'description' => __( 'Berries', 'twentyten' )
135 'cherryblossom' => array(
136 'url' => '%s/images/headers/cherryblossoms.jpg',
137 'thumbnail_url' => '%s/images/headers/cherryblossoms-thumbnail.jpg',
138 /* translators: header image description */
139 'description' => __( 'Cherry Blossoms', 'twentyten' )
142 'url' => '%s/images/headers/concave.jpg',
143 'thumbnail_url' => '%s/images/headers/concave-thumbnail.jpg',
144 /* translators: header image description */
145 'description' => __( 'Concave', 'twentyten' )
148 'url' => '%s/images/headers/fern.jpg',
149 'thumbnail_url' => '%s/images/headers/fern-thumbnail.jpg',
150 /* translators: header image description */
151 'description' => __( 'Fern', 'twentyten' )
153 'forestfloor' => array(
154 'url' => '%s/images/headers/forestfloor.jpg',
155 'thumbnail_url' => '%s/images/headers/forestfloor-thumbnail.jpg',
156 /* translators: header image description */
157 'description' => __( 'Forest Floor', 'twentyten' )
160 'url' => '%s/images/headers/inkwell.jpg',
161 'thumbnail_url' => '%s/images/headers/inkwell-thumbnail.jpg',
162 /* translators: header image description */
163 'description' => __( 'Inkwell', 'twentyten' )
166 'url' => '%s/images/headers/path.jpg',
167 'thumbnail_url' => '%s/images/headers/path-thumbnail.jpg',
168 /* translators: header image description */
169 'description' => __( 'Path', 'twentyten' )
172 'url' => '%s/images/headers/sunset.jpg',
173 'thumbnail_url' => '%s/images/headers/sunset-thumbnail.jpg',
174 /* translators: header image description */
175 'description' => __( 'Sunset', 'twentyten' )
181 if ( ! function_exists( 'twentyten_admin_header_style' ) ) :
183 * Styles the header image displayed on the Appearance > Header admin panel.
185 * Referenced via add_custom_image_header() in twentyten_setup().
187 * @since Twenty Ten 1.0
189 function twentyten_admin_header_style() {
191 <style type="text/css">
192 /* Shows the same border as on front end */
194 border-bottom: 1px solid #000;
195 border-top: 4px solid #000;
197 /* If NO_HEADER_TEXT is false, you would style the text with these selectors:
207 * Makes some changes to the <title> tag, by filtering the output of wp_title().
209 * If we have a site description and we're viewing the home page or a blog posts
210 * page (when using a static front page), then we will add the site description.
212 * If we're viewing a search result, then we're going to recreate the title entirely.
213 * We're going to add page numbers to all titles as well, to the middle of a search
214 * result title and the end of all other titles.
216 * The site title also gets added to all titles.
218 * @since Twenty Ten 1.0
220 * @param string $title Title generated by wp_title()
221 * @param string $separator The separator passed to wp_title(). Twenty Ten uses a
222 * vertical bar, "|", as a separator in header.php.
223 * @return string The new title, ready for the <title> tag.
225 function twentyten_filter_wp_title( $title, $separator ) {
226 // Don't affect wp_title() calls in feeds.
230 // The $paged global variable contains the page number of a listing of posts.
231 // The $page global variable contains the page number of a single post that is paged.
232 // We'll display whichever one applies, if we're not looking at the first page.
233 global $paged, $page;
236 // If we're a search, let's start over:
237 $title = sprintf( __( 'Search results for %s', 'twentyten' ), '"' . get_search_query() . '"' );
238 // Add a page number if we're on page 2 or more:
240 $title .= " $separator " . sprintf( __( 'Page %s', 'twentyten' ), $paged );
241 // Add the site name to the end:
242 $title .= " $separator " . get_bloginfo( 'name', 'display' );
243 // We're done. Let's send the new title back to wp_title():
247 // Otherwise, let's start by adding the site name to the end:
248 $title .= get_bloginfo( 'name', 'display' );
250 // If we have a site description and we're on the home/front page, add the description:
251 $site_description = get_bloginfo( 'description', 'display' );
252 if ( $site_description && ( is_home() || is_front_page() ) )
253 $title .= " $separator " . $site_description;
255 // Add a page number if necessary:
256 if ( $paged >= 2 || $page >= 2 )
257 $title .= " $separator " . sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );
259 // Return the new title to wp_title():
262 add_filter( 'wp_title', 'twentyten_filter_wp_title', 10, 2 );
265 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
267 * To override this in a child theme, remove the filter and optionally add
268 * your own function tied to the wp_page_menu_args filter hook.
270 * @since Twenty Ten 1.0
272 function twentyten_page_menu_args( $args ) {
273 $args['show_home'] = true;
276 add_filter( 'wp_page_menu_args', 'twentyten_page_menu_args' );
279 * Sets the post excerpt length to 40 characters.
281 * To override this length in a child theme, remove the filter and add your own
282 * function tied to the excerpt_length filter hook.
284 * @since Twenty Ten 1.0
287 function twentyten_excerpt_length( $length ) {
290 add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
293 * Returns a "Continue Reading" link for excerpts
295 * @since Twenty Ten 1.0
296 * @return string "Continue Reading" link
298 function twentyten_continue_reading_link() {
299 return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) . '</a>';
303 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyten_continue_reading_link().
305 * To override this in a child theme, remove the filter and add your own
306 * function tied to the excerpt_more filter hook.
308 * @since Twenty Ten 1.0
309 * @return string An ellipsis
311 function twentyten_auto_excerpt_more( $more ) {
312 return ' …' . twentyten_continue_reading_link();
314 add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
317 * Adds a pretty "Continue Reading" link to custom post excerpts.
319 * To override this link in a child theme, remove the filter and add your own
320 * function tied to the get_the_excerpt filter hook.
322 * @since Twenty Ten 1.0
323 * @return string Excerpt with a pretty "Continue Reading" link
325 function twentyten_custom_excerpt_more( $output ) {
326 if ( has_excerpt() && ! is_attachment() ) {
327 $output .= twentyten_continue_reading_link();
331 add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
334 * Remove inline styles printed when the gallery shortcode is used.
336 * Galleries are styled by the theme in Twenty Ten's style.css.
338 * @since Twenty Ten 1.0
339 * @return string The gallery style filter, with the styles themselves removed.
341 function twentyten_remove_gallery_css( $css ) {
342 return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
344 add_filter( 'gallery_style', 'twentyten_remove_gallery_css' );
346 if ( ! function_exists( 'twentyten_comment' ) ) :
348 * Template for comments and pingbacks.
350 * To override this walker in a child theme without modifying the comments template
351 * simply create your own twentyten_comment(), and that function will be used instead.
353 * Used as a callback by wp_list_comments() for displaying the comments.
355 * @since Twenty Ten 1.0
357 function twentyten_comment( $comment, $args, $depth ) {
358 $GLOBALS['comment'] = $comment;
359 switch ( $comment->comment_type ) :
362 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
363 <div id="comment-<?php comment_ID(); ?>">
364 <div class="comment-author vcard">
365 <?php echo get_avatar( $comment, 40 ); ?>
366 <?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
367 </div><!-- .comment-author .vcard -->
368 <?php if ( $comment->comment_approved == '0' ) : ?>
369 <em><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
373 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
375 /* translators: 1: date, 2: time */
376 printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
378 </div><!-- .comment-meta .commentmetadata -->
380 <div class="comment-body"><?php comment_text(); ?></div>
383 <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
384 </div><!-- .reply -->
385 </div><!-- #comment-## -->
392 <li class="post pingback">
393 <p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p>
401 * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
403 * To override twentyten_widgets_init() in a child theme, remove the action hook and add your own
404 * function tied to the init hook.
406 * @since Twenty Ten 1.0
407 * @uses register_sidebar
409 function twentyten_widgets_init() {
410 // Area 1, located at the top of the sidebar.
411 register_sidebar( array(
412 'name' => __( 'Primary Widget Area', 'twentyten' ),
413 'id' => 'primary-widget-area',
414 'description' => __( 'The primary widget area', 'twentyten' ),
415 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
416 'after_widget' => '</li>',
417 'before_title' => '<h3 class="widget-title">',
418 'after_title' => '</h3>',
421 // Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
422 register_sidebar( array(
423 'name' => __( 'Secondary Widget Area', 'twentyten' ),
424 'id' => 'secondary-widget-area',
425 'description' => __( 'The secondary widget area', 'twentyten' ),
426 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
427 'after_widget' => '</li>',
428 'before_title' => '<h3 class="widget-title">',
429 'after_title' => '</h3>',
432 // Area 3, located in the footer. Empty by default.
433 register_sidebar( array(
434 'name' => __( 'First Footer Widget Area', 'twentyten' ),
435 'id' => 'first-footer-widget-area',
436 'description' => __( 'The first footer widget area', 'twentyten' ),
437 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
438 'after_widget' => '</li>',
439 'before_title' => '<h3 class="widget-title">',
440 'after_title' => '</h3>',
443 // Area 4, located in the footer. Empty by default.
444 register_sidebar( array(
445 'name' => __( 'Second Footer Widget Area', 'twentyten' ),
446 'id' => 'second-footer-widget-area',
447 'description' => __( 'The second footer widget area', 'twentyten' ),
448 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
449 'after_widget' => '</li>',
450 'before_title' => '<h3 class="widget-title">',
451 'after_title' => '</h3>',
454 // Area 5, located in the footer. Empty by default.
455 register_sidebar( array(
456 'name' => __( 'Third Footer Widget Area', 'twentyten' ),
457 'id' => 'third-footer-widget-area',
458 'description' => __( 'The third footer widget area', 'twentyten' ),
459 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
460 'after_widget' => '</li>',
461 'before_title' => '<h3 class="widget-title">',
462 'after_title' => '</h3>',
465 // Area 6, located in the footer. Empty by default.
466 register_sidebar( array(
467 'name' => __( 'Fourth Footer Widget Area', 'twentyten' ),
468 'id' => 'fourth-footer-widget-area',
469 'description' => __( 'The fourth footer widget area', 'twentyten' ),
470 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
471 'after_widget' => '</li>',
472 'before_title' => '<h3 class="widget-title">',
473 'after_title' => '</h3>',
476 /** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
477 add_action( 'widgets_init', 'twentyten_widgets_init' );
480 * Removes the default styles that are packaged with the Recent Comments widget.
482 * To override this in a child theme, remove the filter and optionally add your own
483 * function tied to the widgets_init action hook.
485 * @since Twenty Ten 1.0
487 function twentyten_remove_recent_comments_style() {
488 global $wp_widget_factory;
489 remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
491 add_action( 'widgets_init', 'twentyten_remove_recent_comments_style' );
493 if ( ! function_exists( 'twentyten_posted_on' ) ) :
495 * Prints HTML with meta information for the current post—date/time and author.
497 * @since Twenty Ten 1.0
499 function twentyten_posted_on() {
500 printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
501 'meta-prep meta-prep-author',
502 sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
504 esc_attr( get_the_time() ),
507 sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
508 get_author_posts_url( get_the_author_meta( 'ID' ) ),
509 sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ),
516 if ( ! function_exists( 'twentyten_posted_in' ) ) :
518 * Prints HTML with meta information for the current post (category, tags and permalink).
520 * @since Twenty Ten 1.0
522 function twentyten_posted_in() {
523 // Retrieves tag list of current post, separated by commas.
524 $tag_list = get_the_tag_list( '', ', ' );
526 $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' );
527 } elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
528 $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' );
530 $posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
532 // Prints the string, replacing the placeholders.
535 get_the_category_list( ', ' ),
538 the_title_attribute( 'echo=0' )