]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/themes/twentyten/functions.php
Wordpress 3.0.4-scripts
[autoinstalls/wordpress.git] / wp-content / themes / twentyten / functions.php
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 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.
72  *
73  * @since Twenty Ten 1.0
74  */
75 function twentyten_setup() {
76
77         // This theme styles the visual editor with editor-style.css to match the theme style.
78         add_editor_style();
79
80         // This theme uses post thumbnails
81         add_theme_support( 'post-thumbnails' );
82
83         // Add default posts and comments RSS feed links to head
84         add_theme_support( 'automatic-feed-links' );
85
86         // Make theme available for translation
87         // Translations can be filed in the /languages/ directory
88         load_theme_textdomain( 'twentyten', TEMPLATEPATH . '/languages' );
89
90         $locale = get_locale();
91         $locale_file = TEMPLATEPATH . "/languages/$locale.php";
92         if ( is_readable( $locale_file ) )
93                 require_once( $locale_file );
94
95         // This theme uses wp_nav_menu() in one location.
96         register_nav_menus( array(
97                 'primary' => __( 'Primary Navigation', 'twentyten' ),
98         ) );
99
100         // This theme allows users to set a custom background
101         add_custom_background();
102
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' );
107
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 ) );
112
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 );
117
118         // Don't support text inside the header image.
119         define( 'NO_HEADER_TEXT', true );
120
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' );
124
125         // ... and thus ends the changeable header business.
126
127         // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
128         register_default_headers( array(
129                 'berries' => 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' )
134                 ),
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' )
140                 ),
141                 'concave' => array(
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' )
146                 ),
147                 'fern' => array(
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' )
152                 ),
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' )
158                 ),
159                 'inkwell' => array(
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' )
164                 ),
165                 'path' => array(
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' )
170                 ),
171                 'sunset' => array(
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' )
176                 )
177         ) );
178 }
179 endif;
180
181 if ( ! function_exists( 'twentyten_admin_header_style' ) ) :
182 /**
183  * Styles the header image displayed on the Appearance > Header admin panel.
184  *
185  * Referenced via add_custom_image_header() in twentyten_setup().
186  *
187  * @since Twenty Ten 1.0
188  */
189 function twentyten_admin_header_style() {
190 ?>
191 <style type="text/css">
192 /* Shows the same border as on front end */
193 #headimg {
194         border-bottom: 1px solid #000;
195         border-top: 4px solid #000;
196 }
197 /* If NO_HEADER_TEXT is false, you would style the text with these selectors:
198         #headimg #name { }
199         #headimg #desc { }
200 */
201 </style>
202 <?php
203 }
204 endif;
205
206 /**
207  * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
208  *
209  * To override this in a child theme, remove the filter and optionally add
210  * your own function tied to the wp_page_menu_args filter hook.
211  *
212  * @since Twenty Ten 1.0
213  */
214 function twentyten_page_menu_args( $args ) {
215         $args['show_home'] = true;
216         return $args;
217 }
218 add_filter( 'wp_page_menu_args', 'twentyten_page_menu_args' );
219
220 /**
221  * Sets the post excerpt length to 40 characters.
222  *
223  * To override this length in a child theme, remove the filter and add your own
224  * function tied to the excerpt_length filter hook.
225  *
226  * @since Twenty Ten 1.0
227  * @return int
228  */
229 function twentyten_excerpt_length( $length ) {
230         return 40;
231 }
232 add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
233
234 /**
235  * Returns a "Continue Reading" link for excerpts
236  *
237  * @since Twenty Ten 1.0
238  * @return string "Continue Reading" link
239  */
240 function twentyten_continue_reading_link() {
241         return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
242 }
243
244 /**
245  * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyten_continue_reading_link().
246  *
247  * To override this in a child theme, remove the filter and add your own
248  * function tied to the excerpt_more filter hook.
249  *
250  * @since Twenty Ten 1.0
251  * @return string An ellipsis
252  */
253 function twentyten_auto_excerpt_more( $more ) {
254         return ' &hellip;' . twentyten_continue_reading_link();
255 }
256 add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
257
258 /**
259  * Adds a pretty "Continue Reading" link to custom post excerpts.
260  *
261  * To override this link in a child theme, remove the filter and add your own
262  * function tied to the get_the_excerpt filter hook.
263  *
264  * @since Twenty Ten 1.0
265  * @return string Excerpt with a pretty "Continue Reading" link
266  */
267 function twentyten_custom_excerpt_more( $output ) {
268         if ( has_excerpt() && ! is_attachment() ) {
269                 $output .= twentyten_continue_reading_link();
270         }
271         return $output;
272 }
273 add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
274
275 /**
276  * Remove inline styles printed when the gallery shortcode is used.
277  *
278  * Galleries are styled by the theme in Twenty Ten's style.css.
279  *
280  * @since Twenty Ten 1.0
281  * @return string The gallery style filter, with the styles themselves removed.
282  */
283 function twentyten_remove_gallery_css( $css ) {
284         return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
285 }
286 add_filter( 'gallery_style', 'twentyten_remove_gallery_css' );
287
288 if ( ! function_exists( 'twentyten_comment' ) ) :
289 /**
290  * Template for comments and pingbacks.
291  *
292  * To override this walker in a child theme without modifying the comments template
293  * simply create your own twentyten_comment(), and that function will be used instead.
294  *
295  * Used as a callback by wp_list_comments() for displaying the comments.
296  *
297  * @since Twenty Ten 1.0
298  */
299 function twentyten_comment( $comment, $args, $depth ) {
300         $GLOBALS['comment'] = $comment;
301         switch ( $comment->comment_type ) :
302                 case '' :
303         ?>
304         <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
305                 <div id="comment-<?php comment_ID(); ?>">
306                 <div class="comment-author vcard">
307                         <?php echo get_avatar( $comment, 40 ); ?>
308                         <?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
309                 </div><!-- .comment-author .vcard -->
310                 <?php if ( $comment->comment_approved == '0' ) : ?>
311                         <em><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
312                         <br />
313                 <?php endif; ?>
314
315                 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
316                         <?php
317                                 /* translators: 1: date, 2: time */
318                                 printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
319                         ?>
320                 </div><!-- .comment-meta .commentmetadata -->
321
322                 <div class="comment-body"><?php comment_text(); ?></div>
323
324                 <div class="reply">
325                         <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
326                 </div><!-- .reply -->
327         </div><!-- #comment-##  -->
328
329         <?php
330                         break;
331                 case 'pingback'  :
332                 case 'trackback' :
333         ?>
334         <li class="post pingback">
335                 <p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p>
336         <?php
337                         break;
338         endswitch;
339 }
340 endif;
341
342 /**
343  * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
344  *
345  * To override twentyten_widgets_init() in a child theme, remove the action hook and add your own
346  * function tied to the init hook.
347  *
348  * @since Twenty Ten 1.0
349  * @uses register_sidebar
350  */
351 function twentyten_widgets_init() {
352         // Area 1, located at the top of the sidebar.
353         register_sidebar( array(
354                 'name' => __( 'Primary Widget Area', 'twentyten' ),
355                 'id' => 'primary-widget-area',
356                 'description' => __( 'The primary widget area', 'twentyten' ),
357                 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
358                 'after_widget' => '</li>',
359                 'before_title' => '<h3 class="widget-title">',
360                 'after_title' => '</h3>',
361         ) );
362
363         // Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
364         register_sidebar( array(
365                 'name' => __( 'Secondary Widget Area', 'twentyten' ),
366                 'id' => 'secondary-widget-area',
367                 'description' => __( 'The secondary 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 3, located in the footer. Empty by default.
375         register_sidebar( array(
376                 'name' => __( 'First Footer Widget Area', 'twentyten' ),
377                 'id' => 'first-footer-widget-area',
378                 'description' => __( 'The first footer 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 4, located in the footer. Empty by default.
386         register_sidebar( array(
387                 'name' => __( 'Second Footer Widget Area', 'twentyten' ),
388                 'id' => 'second-footer-widget-area',
389                 'description' => __( 'The second 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 5, located in the footer. Empty by default.
397         register_sidebar( array(
398                 'name' => __( 'Third Footer Widget Area', 'twentyten' ),
399                 'id' => 'third-footer-widget-area',
400                 'description' => __( 'The third 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 6, located in the footer. Empty by default.
408         register_sidebar( array(
409                 'name' => __( 'Fourth Footer Widget Area', 'twentyten' ),
410                 'id' => 'fourth-footer-widget-area',
411                 'description' => __( 'The fourth 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 /** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
419 add_action( 'widgets_init', 'twentyten_widgets_init' );
420
421 /**
422  * Removes the default styles that are packaged with the Recent Comments widget.
423  *
424  * To override this in a child theme, remove the filter and optionally add your own
425  * function tied to the widgets_init action hook.
426  *
427  * @since Twenty Ten 1.0
428  */
429 function twentyten_remove_recent_comments_style() {
430         global $wp_widget_factory;
431         remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
432 }
433 add_action( 'widgets_init', 'twentyten_remove_recent_comments_style' );
434
435 if ( ! function_exists( 'twentyten_posted_on' ) ) :
436 /**
437  * Prints HTML with meta information for the current post—date/time and author.
438  *
439  * @since Twenty Ten 1.0
440  */
441 function twentyten_posted_on() {
442         printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
443                 'meta-prep meta-prep-author',
444                 sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
445                         get_permalink(),
446                         esc_attr( get_the_time() ),
447                         get_the_date()
448                 ),
449                 sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
450                         get_author_posts_url( get_the_author_meta( 'ID' ) ),
451                         sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ),
452                         get_the_author()
453                 )
454         );
455 }
456 endif;
457
458 if ( ! function_exists( 'twentyten_posted_in' ) ) :
459 /**
460  * Prints HTML with meta information for the current post (category, tags and permalink).
461  *
462  * @since Twenty Ten 1.0
463  */
464 function twentyten_posted_in() {
465         // Retrieves tag list of current post, separated by commas.
466         $tag_list = get_the_tag_list( '', ', ' );
467         if ( $tag_list ) {
468                 $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' );
469         } elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
470                 $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' );
471         } else {
472                 $posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
473         }
474         // Prints the string, replacing the placeholders.
475         printf(
476                 $posted_in,
477                 get_the_category_list( ', ' ),
478                 $tag_list,
479                 get_permalink(),
480                 the_title_attribute( 'echo=0' )
481         );
482 }
483 endif;