3 * Twenty Eleven 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, twentyeleven_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', 'twentyeleven_excerpt_length' );
34 * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
37 * @subpackage Twenty_Eleven
38 * @since Twenty Eleven 1.0
42 * Set the content width based on the theme's design and stylesheet.
44 if ( ! isset( $content_width ) )
48 * Tell WordPress to run twentyeleven_setup() when the 'after_setup_theme' hook is run.
50 add_action( 'after_setup_theme', 'twentyeleven_setup' );
52 if ( ! function_exists( 'twentyeleven_setup' ) ):
54 * Sets up theme defaults and registers support for various WordPress features.
56 * Note that this function is hooked into the after_setup_theme hook, which runs
57 * before the init hook. The init hook is too late for some features, such as indicating
58 * support post thumbnails.
60 * To override twentyeleven_setup() in a child theme, add your own twentyeleven_setup to your child theme's
63 * @uses load_theme_textdomain() For translation/localization support.
64 * @uses add_editor_style() To style the visual editor.
65 * @uses add_theme_support() To add support for post thumbnails, automatic feed links, and Post Formats.
66 * @uses register_nav_menus() To add support for navigation menus.
67 * @uses add_custom_background() To add support for a custom background.
68 * @uses add_custom_image_header() To add support for a custom header.
69 * @uses register_default_headers() To register the default custom header images provided with the theme.
70 * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
72 * @since Twenty Eleven 1.0
74 function twentyeleven_setup() {
76 /* Make Twenty Eleven available for translation.
77 * Translations can be added to the /languages/ directory.
78 * If you're building a theme based on Twenty Eleven, use a find and replace
79 * to change 'twentyeleven' to the name of your theme in all the template files.
81 load_theme_textdomain( 'twentyeleven', get_template_directory() . '/languages' );
83 $locale = get_locale();
84 $locale_file = get_template_directory() . "/languages/$locale.php";
85 if ( is_readable( $locale_file ) )
86 require_once( $locale_file );
88 // This theme styles the visual editor with editor-style.css to match the theme style.
91 // Load up our theme options page and related code.
92 require( get_template_directory() . '/inc/theme-options.php' );
94 // Grab Twenty Eleven's Ephemera widget.
95 require( get_template_directory() . '/inc/widgets.php' );
97 // Add default posts and comments RSS feed links to <head>.
98 add_theme_support( 'automatic-feed-links' );
100 // This theme uses wp_nav_menu() in one location.
101 register_nav_menu( 'primary', __( 'Primary Menu', 'twentyeleven' ) );
103 // Add support for a variety of post formats
104 add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) );
106 // Add support for custom backgrounds
107 add_custom_background();
109 // This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
110 add_theme_support( 'post-thumbnails' );
112 // The next four constants set how Twenty Eleven supports custom headers.
114 // The default header text color
115 define( 'HEADER_TEXTCOLOR', '000' );
117 // By leaving empty, we allow for random image rotation.
118 define( 'HEADER_IMAGE', '' );
120 // The height and width of your custom header.
121 // Add a filter to twentyeleven_header_image_width and twentyeleven_header_image_height to change these values.
122 define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyeleven_header_image_width', 1000 ) );
123 define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyeleven_header_image_height', 288 ) );
125 // We'll be using post thumbnails for custom header images on posts and pages.
126 // We want them to be the size of the header image that we just defined
127 // Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
128 set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
130 // Add Twenty Eleven's custom image sizes
131 add_image_size( 'large-feature', HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true ); // Used for large feature (header) images
132 add_image_size( 'small-feature', 500, 300 ); // Used for featured posts if a large-feature doesn't exist
134 // Turn on random header image rotation by default.
135 add_theme_support( 'custom-header', array( 'random-default' => true ) );
137 // Add a way for the custom header to be styled in the admin panel that controls
138 // custom headers. See twentyeleven_admin_header_style(), below.
139 add_custom_image_header( 'twentyeleven_header_style', 'twentyeleven_admin_header_style', 'twentyeleven_admin_header_image' );
141 // ... and thus ends the changeable header business.
143 // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
144 register_default_headers( array(
146 'url' => '%s/images/headers/wheel.jpg',
147 'thumbnail_url' => '%s/images/headers/wheel-thumbnail.jpg',
148 /* translators: header image description */
149 'description' => __( 'Wheel', 'twentyeleven' )
152 'url' => '%s/images/headers/shore.jpg',
153 'thumbnail_url' => '%s/images/headers/shore-thumbnail.jpg',
154 /* translators: header image description */
155 'description' => __( 'Shore', 'twentyeleven' )
158 'url' => '%s/images/headers/trolley.jpg',
159 'thumbnail_url' => '%s/images/headers/trolley-thumbnail.jpg',
160 /* translators: header image description */
161 'description' => __( 'Trolley', 'twentyeleven' )
163 'pine-cone' => array(
164 'url' => '%s/images/headers/pine-cone.jpg',
165 'thumbnail_url' => '%s/images/headers/pine-cone-thumbnail.jpg',
166 /* translators: header image description */
167 'description' => __( 'Pine Cone', 'twentyeleven' )
169 'chessboard' => array(
170 'url' => '%s/images/headers/chessboard.jpg',
171 'thumbnail_url' => '%s/images/headers/chessboard-thumbnail.jpg',
172 /* translators: header image description */
173 'description' => __( 'Chessboard', 'twentyeleven' )
176 'url' => '%s/images/headers/lanterns.jpg',
177 'thumbnail_url' => '%s/images/headers/lanterns-thumbnail.jpg',
178 /* translators: header image description */
179 'description' => __( 'Lanterns', 'twentyeleven' )
182 'url' => '%s/images/headers/willow.jpg',
183 'thumbnail_url' => '%s/images/headers/willow-thumbnail.jpg',
184 /* translators: header image description */
185 'description' => __( 'Willow', 'twentyeleven' )
188 'url' => '%s/images/headers/hanoi.jpg',
189 'thumbnail_url' => '%s/images/headers/hanoi-thumbnail.jpg',
190 /* translators: header image description */
191 'description' => __( 'Hanoi Plant', 'twentyeleven' )
195 endif; // twentyeleven_setup
197 if ( ! function_exists( 'twentyeleven_header_style' ) ) :
199 * Styles the header image and text displayed on the blog
201 * @since Twenty Eleven 1.0
203 function twentyeleven_header_style() {
205 // If no custom options for text are set, let's bail
206 // get_header_textcolor() options: HEADER_TEXTCOLOR is default, hide text (returns 'blank') or any hex value
207 if ( HEADER_TEXTCOLOR == get_header_textcolor() )
209 // If we get this far, we have custom styles. Let's do this.
211 <style type="text/css">
213 // Has the text been hidden?
214 if ( 'blank' == get_header_textcolor() ) :
218 position: absolute !important;
219 clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
220 clip: rect(1px, 1px, 1px, 1px);
223 // If the user has set a custom color for the text use that
228 color: #<?php echo get_header_textcolor(); ?> !important;
234 endif; // twentyeleven_header_style
236 if ( ! function_exists( 'twentyeleven_admin_header_style' ) ) :
238 * Styles the header image displayed on the Appearance > Header admin panel.
240 * Referenced via add_custom_image_header() in twentyeleven_setup().
242 * @since Twenty Eleven 1.0
244 function twentyeleven_admin_header_style() {
246 <style type="text/css">
247 .appearance_page_custom-header #headimg {
252 font-family: "Helvetica Neue", Arial, Helvetica, "Nimbus Sans L", sans-serif;
260 text-decoration: none;
268 // If the user has set a custom color for the text use that
269 if ( get_header_textcolor() != HEADER_TEXTCOLOR ) :
273 color: #<?php echo get_header_textcolor(); ?>;
284 endif; // twentyeleven_admin_header_style
286 if ( ! function_exists( 'twentyeleven_admin_header_image' ) ) :
288 * Custom header image markup displayed on the Appearance > Header admin panel.
290 * Referenced via add_custom_image_header() in twentyeleven_setup().
292 * @since Twenty Eleven 1.0
294 function twentyeleven_admin_header_image() { ?>
297 if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || '' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) )
298 $style = ' style="display:none;"';
300 $style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
302 <h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
303 <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
304 <?php $header_image = get_header_image();
305 if ( ! empty( $header_image ) ) : ?>
306 <img src="<?php echo esc_url( $header_image ); ?>" alt="" />
310 endif; // twentyeleven_admin_header_image
313 * Sets the post excerpt length to 40 words.
315 * To override this length in a child theme, remove the filter and add your own
316 * function tied to the excerpt_length filter hook.
318 function twentyeleven_excerpt_length( $length ) {
321 add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
324 * Returns a "Continue Reading" link for excerpts
326 function twentyeleven_continue_reading_link() {
327 return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) . '</a>';
331 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyeleven_continue_reading_link().
333 * To override this in a child theme, remove the filter and add your own
334 * function tied to the excerpt_more filter hook.
336 function twentyeleven_auto_excerpt_more( $more ) {
337 return ' …' . twentyeleven_continue_reading_link();
339 add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
342 * Adds a pretty "Continue Reading" link to custom post excerpts.
344 * To override this link in a child theme, remove the filter and add your own
345 * function tied to the get_the_excerpt filter hook.
347 function twentyeleven_custom_excerpt_more( $output ) {
348 if ( has_excerpt() && ! is_attachment() ) {
349 $output .= twentyeleven_continue_reading_link();
353 add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
356 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
358 function twentyeleven_page_menu_args( $args ) {
359 $args['show_home'] = true;
362 add_filter( 'wp_page_menu_args', 'twentyeleven_page_menu_args' );
365 * Register our sidebars and widgetized areas. Also register the default Epherma widget.
367 * @since Twenty Eleven 1.0
369 function twentyeleven_widgets_init() {
371 register_widget( 'Twenty_Eleven_Ephemera_Widget' );
373 register_sidebar( array(
374 'name' => __( 'Main Sidebar', 'twentyeleven' ),
376 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
377 'after_widget' => "</aside>",
378 'before_title' => '<h3 class="widget-title">',
379 'after_title' => '</h3>',
382 register_sidebar( array(
383 'name' => __( 'Showcase Sidebar', 'twentyeleven' ),
385 'description' => __( 'The sidebar for the optional Showcase Template', 'twentyeleven' ),
386 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
387 'after_widget' => "</aside>",
388 'before_title' => '<h3 class="widget-title">',
389 'after_title' => '</h3>',
392 register_sidebar( array(
393 'name' => __( 'Footer Area One', 'twentyeleven' ),
395 'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
396 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
397 'after_widget' => "</aside>",
398 'before_title' => '<h3 class="widget-title">',
399 'after_title' => '</h3>',
402 register_sidebar( array(
403 'name' => __( 'Footer Area Two', 'twentyeleven' ),
405 'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
406 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
407 'after_widget' => "</aside>",
408 'before_title' => '<h3 class="widget-title">',
409 'after_title' => '</h3>',
412 register_sidebar( array(
413 'name' => __( 'Footer Area Three', 'twentyeleven' ),
415 'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
416 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
417 'after_widget' => "</aside>",
418 'before_title' => '<h3 class="widget-title">',
419 'after_title' => '</h3>',
422 add_action( 'widgets_init', 'twentyeleven_widgets_init' );
424 if ( ! function_exists( 'twentyeleven_content_nav' ) ) :
426 * Display navigation to next/previous pages when applicable
428 function twentyeleven_content_nav( $nav_id ) {
431 if ( $wp_query->max_num_pages > 1 ) : ?>
432 <nav id="<?php echo $nav_id; ?>">
433 <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
434 <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyeleven' ) ); ?></div>
435 <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></div>
436 </nav><!-- #nav-above -->
439 endif; // twentyeleven_content_nav
442 * Return the URL for the first link found in the post content.
444 * @since Twenty Eleven 1.0
445 * @return string|bool URL or false when no link is present.
447 function twentyeleven_url_grabber() {
448 if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', get_the_content(), $matches ) )
451 return esc_url_raw( $matches[1] );
455 * Count the number of footer sidebars to enable dynamic classes for the footer
457 function twentyeleven_footer_sidebar_class() {
460 if ( is_active_sidebar( 'sidebar-3' ) )
463 if ( is_active_sidebar( 'sidebar-4' ) )
466 if ( is_active_sidebar( 'sidebar-5' ) )
484 echo 'class="' . $class . '"';
487 if ( ! function_exists( 'twentyeleven_comment' ) ) :
489 * Template for comments and pingbacks.
491 * To override this walker in a child theme without modifying the comments template
492 * simply create your own twentyeleven_comment(), and that function will be used instead.
494 * Used as a callback by wp_list_comments() for displaying the comments.
496 * @since Twenty Eleven 1.0
498 function twentyeleven_comment( $comment, $args, $depth ) {
499 $GLOBALS['comment'] = $comment;
500 switch ( $comment->comment_type ) :
504 <li class="post pingback">
505 <p><?php _e( 'Pingback:', 'twentyeleven' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?></p>
510 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
511 <article id="comment-<?php comment_ID(); ?>" class="comment">
512 <footer class="comment-meta">
513 <div class="comment-author vcard">
516 if ( '0' != $comment->comment_parent )
519 echo get_avatar( $comment, $avatar_size );
521 /* translators: 1: comment author, 2: date and time */
522 printf( __( '%1$s on %2$s <span class="says">said:</span>', 'twentyeleven' ),
523 sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
524 sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
525 esc_url( get_comment_link( $comment->comment_ID ) ),
526 get_comment_time( 'c' ),
527 /* translators: 1: date, 2: time */
528 sprintf( __( '%1$s at %2$s', 'twentyeleven' ), get_comment_date(), get_comment_time() )
533 <?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
534 </div><!-- .comment-author .vcard -->
536 <?php if ( $comment->comment_approved == '0' ) : ?>
537 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentyeleven' ); ?></em>
543 <div class="comment-content"><?php comment_text(); ?></div>
546 <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>↓</span>', 'twentyeleven' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
547 </div><!-- .reply -->
548 </article><!-- #comment-## -->
554 endif; // ends check for twentyeleven_comment()
556 if ( ! function_exists( 'twentyeleven_posted_on' ) ) :
558 * Prints HTML with meta information for the current post-date/time and author.
559 * Create your own twentyeleven_posted_on to override in a child theme
561 * @since Twenty Eleven 1.0
563 function twentyeleven_posted_on() {
564 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>', 'twentyeleven' ),
565 esc_url( get_permalink() ),
566 esc_attr( get_the_time() ),
567 esc_attr( get_the_date( 'c' ) ),
568 esc_html( get_the_date() ),
569 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
570 esc_attr( sprintf( __( 'View all posts by %s', 'twentyeleven' ), get_the_author() ) ),
577 * Adds two classes to the array of body classes.
578 * The first is if the site has only had one author with published posts.
579 * The second is if a singular post being displayed
581 * @since Twenty Eleven 1.0
583 function twentyeleven_body_classes( $classes ) {
585 if ( function_exists( 'is_multi_author' ) && ! is_multi_author() )
586 $classes[] = 'single-author';
588 if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) && ! is_page_template( 'sidebar-page.php' ) )
589 $classes[] = 'singular';
593 add_filter( 'body_class', 'twentyeleven_body_classes' );