| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Twenty Eleven 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, twentyeleven_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', 'twentyeleven_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_Eleven 38 * @since Twenty Eleven 1.0 39 */ 40 41 /** 42 * Set the content width based on the theme's design and stylesheet. 43 */ 44 if ( ! isset( $content_width ) ) 45 $content_width = 584; 46 47 /** 48 * Tell WordPress to run twentyeleven_setup() when the 'after_setup_theme' hook is run. 49 */ 50 add_action( 'after_setup_theme', 'twentyeleven_setup' ); 51 52 if ( ! function_exists( 'twentyeleven_setup' ) ): 53 /** 54 * Sets up theme defaults and registers support for various WordPress features. 55 * 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. 59 * 60 * To override twentyeleven_setup() in a child theme, add your own twentyeleven_setup to your child theme's 61 * functions.php file. 62 * 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, custom headers 66 * and backgrounds, and post formats. 67 * @uses register_nav_menus() To add support for navigation menus. 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 Eleven 1.0 72 */ 73 function twentyeleven_setup() { 74 75 /* Make Twenty Eleven available for translation. 76 * Translations can be added to the /languages/ directory. 77 * If you're building a theme based on Twenty Eleven, use a find and replace 78 * to change 'twentyeleven' to the name of your theme in all the template files. 79 */ 80 load_theme_textdomain( 'twentyeleven', get_template_directory() . '/languages' ); 81 82 // This theme styles the visual editor with editor-style.css to match the theme style. 83 add_editor_style(); 84 85 // Load up our theme options page and related code. 86 require( get_template_directory() . '/inc/theme-options.php' ); 87 88 // Grab Twenty Eleven's Ephemera widget. 89 require( get_template_directory() . '/inc/widgets.php' ); 90 91 // Add default posts and comments RSS feed links to <head>. 92 add_theme_support( 'automatic-feed-links' ); 93 94 // This theme uses wp_nav_menu() in one location. 95 register_nav_menu( 'primary', __( 'Primary Menu', 'twentyeleven' ) ); 96 97 // Add support for a variety of post formats 98 add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) ); 99 100 // Add support for custom backgrounds. 101 add_theme_support( 'custom-background' ); 102 103 // This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images 104 add_theme_support( 'post-thumbnails' ); 105 106 // Add support for custom headers. 107 add_theme_support( 'custom-header', array( 108 // The default header text color. 109 'default-text-color' => '000', 110 // The height and width of our custom header. 111 'width' => apply_filters( 'twentyeleven_header_image_width', 1000 ), 112 'height' => apply_filters( 'twentyeleven_header_image_height', 288 ), 113 // Support flexible heights. 114 'flex-height' => true, 115 // Random image rotation by default. 116 'random-default' => true, 117 // Callback for styling the header. 118 'wp-head-callback' => 'twentyeleven_header_style', 119 // Callback for styling the header preview in the admin. 120 'admin-head-callback' => 'twentyeleven_admin_header_style', 121 // Callback used to display the header preview in the admin. 122 'admin-preview-callback' => 'twentyeleven_admin_header_image', 123 ) ); 124 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( get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ), true ); 129 130 // Add Twenty Eleven's custom image sizes. 131 // Used for large feature (header) images. 132 add_image_size( 'large-feature', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ), true ); 133 // Used for featured posts if a large-feature doesn't exist. 134 add_image_size( 'small-feature', 500, 300 ); 135 136 // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI. 137 register_default_headers( array( 138 'wheel' => array( 139 'url' => '%s/images/headers/wheel.jpg', 140 'thumbnail_url' => '%s/images/headers/wheel-thumbnail.jpg', 141 /* translators: header image description */ 142 'description' => __( 'Wheel', 'twentyeleven' ) 143 ), 144 'shore' => array( 145 'url' => '%s/images/headers/shore.jpg', 146 'thumbnail_url' => '%s/images/headers/shore-thumbnail.jpg', 147 /* translators: header image description */ 148 'description' => __( 'Shore', 'twentyeleven' ) 149 ), 150 'trolley' => array( 151 'url' => '%s/images/headers/trolley.jpg', 152 'thumbnail_url' => '%s/images/headers/trolley-thumbnail.jpg', 153 /* translators: header image description */ 154 'description' => __( 'Trolley', 'twentyeleven' ) 155 ), 156 'pine-cone' => array( 157 'url' => '%s/images/headers/pine-cone.jpg', 158 'thumbnail_url' => '%s/images/headers/pine-cone-thumbnail.jpg', 159 /* translators: header image description */ 160 'description' => __( 'Pine Cone', 'twentyeleven' ) 161 ), 162 'chessboard' => array( 163 'url' => '%s/images/headers/chessboard.jpg', 164 'thumbnail_url' => '%s/images/headers/chessboard-thumbnail.jpg', 165 /* translators: header image description */ 166 'description' => __( 'Chessboard', 'twentyeleven' ) 167 ), 168 'lanterns' => array( 169 'url' => '%s/images/headers/lanterns.jpg', 170 'thumbnail_url' => '%s/images/headers/lanterns-thumbnail.jpg', 171 /* translators: header image description */ 172 'description' => __( 'Lanterns', 'twentyeleven' ) 173 ), 174 'willow' => array( 175 'url' => '%s/images/headers/willow.jpg', 176 'thumbnail_url' => '%s/images/headers/willow-thumbnail.jpg', 177 /* translators: header image description */ 178 'description' => __( 'Willow', 'twentyeleven' ) 179 ), 180 'hanoi' => array( 181 'url' => '%s/images/headers/hanoi.jpg', 182 'thumbnail_url' => '%s/images/headers/hanoi-thumbnail.jpg', 183 /* translators: header image description */ 184 'description' => __( 'Hanoi Plant', 'twentyeleven' ) 185 ) 186 ) ); 187 } 188 endif; // twentyeleven_setup 189 190 if ( ! function_exists( 'twentyeleven_header_style' ) ) : 191 /** 192 * Styles the header image and text displayed on the blog 193 * 194 * @since Twenty Eleven 1.0 195 */ 196 function twentyeleven_header_style() { 197 $text_color = get_header_textcolor(); 198 199 // If no custom options for text are set, let's bail. 200 if ( $text_color == get_theme_support( 'custom-header', 'default-text-color' ) ) 201 return; 202 // If we get this far, we have custom styles. Let's do this. 203 ?> 204 <style type="text/css"> 205 <?php 206 // Has the text been hidden? 207 if ( 'blank' == $text_color ) : 208 ?> 209 #site-title, 210 #site-description { 211 position: absolute !important; 212 clip: rect(1px 1px 1px 1px); /* IE6, IE7 */ 213 clip: rect(1px, 1px, 1px, 1px); 214 } 215 <?php 216 // If the user has set a custom color for the text use that 217 else : 218 ?> 219 #site-title a, 220 #site-description { 221 color: #<?php echo $text_color; ?> !important; 222 } 223 <?php endif; ?> 224 </style> 225 <?php 226 } 227 endif; // twentyeleven_header_style 228 229 if ( ! function_exists( 'twentyeleven_admin_header_style' ) ) : 230 /** 231 * Styles the header image displayed on the Appearance > Header admin panel. 232 * 233 * Referenced via add_theme_support('custom-header') in twentyeleven_setup(). 234 * 235 * @since Twenty Eleven 1.0 236 */ 237 function twentyeleven_admin_header_style() { 238 ?> 239 <style type="text/css"> 240 .appearance_page_custom-header #headimg { 241 border: none; 242 } 243 #headimg h1, 244 #desc { 245 font-family: "Helvetica Neue", Arial, Helvetica, "Nimbus Sans L", sans-serif; 246 } 247 #headimg h1 { 248 margin: 0; 249 } 250 #headimg h1 a { 251 font-size: 32px; 252 line-height: 36px; 253 text-decoration: none; 254 } 255 #desc { 256 font-size: 14px; 257 line-height: 23px; 258 padding: 0 0 3em; 259 } 260 <?php 261 // If the user has set a custom color for the text use that 262 if ( get_header_textcolor() != get_theme_support( 'custom-header', 'default-text-color' ) ) : 263 ?> 264 #site-title a, 265 #site-description { 266 color: #<?php echo get_header_textcolor(); ?>; 267 } 268 <?php endif; ?> 269 #headimg img { 270 max-width: 1000px; 271 height: auto; 272 width: 100%; 273 } 274 </style> 275 <?php 276 } 277 endif; // twentyeleven_admin_header_style 278 279 if ( ! function_exists( 'twentyeleven_admin_header_image' ) ) : 280 /** 281 * Custom header image markup displayed on the Appearance > Header admin panel. 282 * 283 * Referenced via add_theme_support('custom-header') in twentyeleven_setup(). 284 * 285 * @since Twenty Eleven 1.0 286 */ 287 function twentyeleven_admin_header_image() { ?> 288 <div id="headimg"> 289 <?php 290 $color = get_header_textcolor(); 291 $image = get_header_image(); 292 if ( $color && $color != 'blank' ) 293 $style = ' style="color:#' . $color . '"'; 294 else 295 $style = ' style="display:none"'; 296 ?> 297 <h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1> 298 <div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div> 299 <?php if ( $image ) : ?> 300 <img src="<?php echo esc_url( $image ); ?>" alt="" /> 301 <?php endif; ?> 302 </div> 303 <?php } 304 endif; // twentyeleven_admin_header_image 305 306 /** 307 * Sets the post excerpt length to 40 words. 308 * 309 * To override this length in a child theme, remove the filter and add your own 310 * function tied to the excerpt_length filter hook. 311 */ 312 function twentyeleven_excerpt_length( $length ) { 313 return 40; 314 } 315 add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' ); 316 317 /** 318 * Returns a "Continue Reading" link for excerpts 319 */ 320 function twentyeleven_continue_reading_link() { 321 return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) . '</a>'; 322 } 323 324 /** 325 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyeleven_continue_reading_link(). 326 * 327 * To override this in a child theme, remove the filter and add your own 328 * function tied to the excerpt_more filter hook. 329 */ 330 function twentyeleven_auto_excerpt_more( $more ) { 331 return ' …' . twentyeleven_continue_reading_link(); 332 } 333 add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' ); 334 335 /** 336 * Adds a pretty "Continue Reading" link to custom post excerpts. 337 * 338 * To override this link in a child theme, remove the filter and add your own 339 * function tied to the get_the_excerpt filter hook. 340 */ 341 function twentyeleven_custom_excerpt_more( $output ) { 342 if ( has_excerpt() && ! is_attachment() ) { 343 $output .= twentyeleven_continue_reading_link(); 344 } 345 return $output; 346 } 347 add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' ); 348 349 /** 350 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link. 351 */ 352 function twentyeleven_page_menu_args( $args ) { 353 $args['show_home'] = true; 354 return $args; 355 } 356 add_filter( 'wp_page_menu_args', 'twentyeleven_page_menu_args' ); 357 358 /** 359 * Register our sidebars and widgetized areas. Also register the default Epherma widget. 360 * 361 * @since Twenty Eleven 1.0 362 */ 363 function twentyeleven_widgets_init() { 364 365 register_widget( 'Twenty_Eleven_Ephemera_Widget' ); 366 367 register_sidebar( array( 368 'name' => __( 'Main Sidebar', 'twentyeleven' ), 369 'id' => 'sidebar-1', 370 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 371 'after_widget' => "</aside>", 372 'before_title' => '<h3 class="widget-title">', 373 'after_title' => '</h3>', 374 ) ); 375 376 register_sidebar( array( 377 'name' => __( 'Showcase Sidebar', 'twentyeleven' ), 378 'id' => 'sidebar-2', 379 'description' => __( 'The sidebar for the optional Showcase Template', 'twentyeleven' ), 380 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 381 'after_widget' => "</aside>", 382 'before_title' => '<h3 class="widget-title">', 383 'after_title' => '</h3>', 384 ) ); 385 386 register_sidebar( array( 387 'name' => __( 'Footer Area One', 'twentyeleven' ), 388 'id' => 'sidebar-3', 389 'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ), 390 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 391 'after_widget' => "</aside>", 392 'before_title' => '<h3 class="widget-title">', 393 'after_title' => '</h3>', 394 ) ); 395 396 register_sidebar( array( 397 'name' => __( 'Footer Area Two', 'twentyeleven' ), 398 'id' => 'sidebar-4', 399 'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ), 400 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 401 'after_widget' => "</aside>", 402 'before_title' => '<h3 class="widget-title">', 403 'after_title' => '</h3>', 404 ) ); 405 406 register_sidebar( array( 407 'name' => __( 'Footer Area Three', 'twentyeleven' ), 408 'id' => 'sidebar-5', 409 'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ), 410 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 411 'after_widget' => "</aside>", 412 'before_title' => '<h3 class="widget-title">', 413 'after_title' => '</h3>', 414 ) ); 415 } 416 add_action( 'widgets_init', 'twentyeleven_widgets_init' ); 417 418 if ( ! function_exists( 'twentyeleven_content_nav' ) ) : 419 /** 420 * Display navigation to next/previous pages when applicable 421 */ 422 function twentyeleven_content_nav( $nav_id ) { 423 global $wp_query; 424 425 if ( $wp_query->max_num_pages > 1 ) : ?> 426 <nav id="<?php echo $nav_id; ?>"> 427 <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3> 428 <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyeleven' ) ); ?></div> 429 <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></div> 430 </nav><!-- #nav-above --> 431 <?php endif; 432 } 433 endif; // twentyeleven_content_nav 434 435 /** 436 * Return the URL for the first link found in the post content. 437 * 438 * @since Twenty Eleven 1.0 439 * @return string|bool URL or false when no link is present. 440 */ 441 function twentyeleven_url_grabber() { 442 if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', get_the_content(), $matches ) ) 443 return false; 444 445 return esc_url_raw( $matches[1] ); 446 } 447 448 /** 449 * Count the number of footer sidebars to enable dynamic classes for the footer 450 */ 451 function twentyeleven_footer_sidebar_class() { 452 $count = 0; 453 454 if ( is_active_sidebar( 'sidebar-3' ) ) 455 $count++; 456 457 if ( is_active_sidebar( 'sidebar-4' ) ) 458 $count++; 459 460 if ( is_active_sidebar( 'sidebar-5' ) ) 461 $count++; 462 463 $class = ''; 464 465 switch ( $count ) { 466 case '1': 467 $class = 'one'; 468 break; 469 case '2': 470 $class = 'two'; 471 break; 472 case '3': 473 $class = 'three'; 474 break; 475 } 476 477 if ( $class ) 478 echo 'class="' . $class . '"'; 479 } 480 481 if ( ! function_exists( 'twentyeleven_comment' ) ) : 482 /** 483 * Template for comments and pingbacks. 484 * 485 * To override this walker in a child theme without modifying the comments template 486 * simply create your own twentyeleven_comment(), and that function will be used instead. 487 * 488 * Used as a callback by wp_list_comments() for displaying the comments. 489 * 490 * @since Twenty Eleven 1.0 491 */ 492 function twentyeleven_comment( $comment, $args, $depth ) { 493 $GLOBALS['comment'] = $comment; 494 switch ( $comment->comment_type ) : 495 case 'pingback' : 496 case 'trackback' : 497 ?> 498 <li class="post pingback"> 499 <p><?php _e( 'Pingback:', 'twentyeleven' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?></p> 500 <?php 501 break; 502 default : 503 ?> 504 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>"> 505 <article id="comment-<?php comment_ID(); ?>" class="comment"> 506 <footer class="comment-meta"> 507 <div class="comment-author vcard"> 508 <?php 509 $avatar_size = 68; 510 if ( '0' != $comment->comment_parent ) 511 $avatar_size = 39; 512 513 echo get_avatar( $comment, $avatar_size ); 514 515 /* translators: 1: comment author, 2: date and time */ 516 printf( __( '%1$s on %2$s <span class="says">said:</span>', 'twentyeleven' ), 517 sprintf( '<span class="fn">%s</span>', get_comment_author_link() ), 518 sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>', 519 esc_url( get_comment_link( $comment->comment_ID ) ), 520 get_comment_time( 'c' ), 521 /* translators: 1: date, 2: time */ 522 sprintf( __( '%1$s at %2$s', 'twentyeleven' ), get_comment_date(), get_comment_time() ) 523 ) 524 ); 525 ?> 526 527 <?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?> 528 </div><!-- .comment-author .vcard --> 529 530 <?php if ( $comment->comment_approved == '0' ) : ?> 531 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentyeleven' ); ?></em> 532 <br /> 533 <?php endif; ?> 534 535 </footer> 536 537 <div class="comment-content"><?php comment_text(); ?></div> 538 539 <div class="reply"> 540 <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>↓</span>', 'twentyeleven' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> 541 </div><!-- .reply --> 542 </article><!-- #comment-## --> 543 544 <?php 545 break; 546 endswitch; 547 } 548 endif; // ends check for twentyeleven_comment() 549 550 if ( ! function_exists( 'twentyeleven_posted_on' ) ) : 551 /** 552 * Prints HTML with meta information for the current post-date/time and author. 553 * Create your own twentyeleven_posted_on to override in a child theme 554 * 555 * @since Twenty Eleven 1.0 556 */ 557 function twentyeleven_posted_on() { 558 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' ), 559 esc_url( get_permalink() ), 560 esc_attr( get_the_time() ), 561 esc_attr( get_the_date( 'c' ) ), 562 esc_html( get_the_date() ), 563 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), 564 esc_attr( sprintf( __( 'View all posts by %s', 'twentyeleven' ), get_the_author() ) ), 565 get_the_author() 566 ); 567 } 568 endif; 569 570 /** 571 * Adds two classes to the array of body classes. 572 * The first is if the site has only had one author with published posts. 573 * The second is if a singular post being displayed 574 * 575 * @since Twenty Eleven 1.0 576 */ 577 function twentyeleven_body_classes( $classes ) { 578 579 if ( function_exists( 'is_multi_author' ) && ! is_multi_author() ) 580 $classes[] = 'single-author'; 581 582 if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) && ! is_page_template( 'sidebar-page.php' ) ) 583 $classes[] = 'singular'; 584 585 return $classes; 586 } 587 add_filter( 'body_class', 'twentyeleven_body_classes' ); 588
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 25 03:56:23 2012 | Hosted by follow the white rabbit. |