Make WordPress Core


Ignore:
Timestamp:
04/26/2011 10:52:18 PM (13 years ago)
Author:
iandstewart
Message:

Twenty Eleven: functions.php cleanup and introduction of theme options; see #17198

  • Cleanup functions.php, adding comments and function_exists() checks following Twenty Ten's example
  • Theme option for choosing an alternate (dark) color scheme. It currently only loads a placeholder CSS file with dark styles to follow.
  • Theme option for selecting a link color that loads an internal style block for resetting link colors. An updated style.css will follow to take advantage of this.
  • Theme options for selecting an alternate layout. Adds a class to the body element. An updated style.css will follow to take advantage of this.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-content/themes/twentyeleven/functions.php

    r17720 r17721  
    11<?php
    22/**
     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 *
    336 * @package WordPress
    437 * @subpackage Twenty Eleven
    5  */
    6 
    7 /**
    8  * Make theme available for translation
    9  * Translations can be filed in the /languages/ directory
    10  * If you're building a theme based on Twenty Eleven, use a find and replace
    11  * to change 'twentyeleven' to the name of your theme in all the template files
    12  */
    13 load_theme_textdomain( 'twentyeleven', TEMPLATEPATH . '/languages' );
    14 
    15 $locale = get_locale();
    16 $locale_file = TEMPLATEPATH . "/languages/$locale.php";
    17 if ( is_readable( $locale_file ) )
    18     require_once( $locale_file );
     38 * @since Twenty Eleven 1.0
     39 */
    1940
    2041/**
     
    2546
    2647/**
    27  * This theme uses wp_nav_menu() in one location.
    28  */
    29 register_nav_menus( array(
    30     'primary' => __( 'Primary Menu', 'twentyeleven' ),
    31 ) );
    32 
    33 /**
    34  * Add default posts and comments RSS feed links to head
    35  */
    36 add_theme_support( 'automatic-feed-links' );
    37 
    38 /**
    39  * Add support for an Aside Post Format
    40  */
    41 add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote' ) );
    42 
    43 /**
    44  * Add support for custom backgrounds
    45  */
    46 add_custom_background();
    47 
    48 // This theme uses Feature Images for per-post/per-page Custom Header images
    49 add_theme_support( 'post-thumbnails' );
    50 
    51 /**
    52  * Add support for Custom Headers
    53  */
    54 define( 'HEADER_TEXTCOLOR', '000' );
    55 
    56 // No CSS, just an IMG call. The %s is a placeholder for the theme template directory URI.
    57 define( 'HEADER_IMAGE', '%s/images/headers/default.jpg' );
    58 
    59 // The height and width of your custom header. You can hook into the theme's own filters to change these values.
    60 // Add a filter to twentyeleven_header_image_width and twentyeleven_header_image_height to change these values.
    61 define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyeleven_header_image_width', 1000 ) );
    62 define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyeleven_header_image_height', 300 ) );
    63 
    64 // We'll be using post thumbnails for custom header images on posts and pages.
    65 // We want them to be 940 pixels wide by 198 pixels tall.
    66 // Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
    67 set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
    68 
    69 // Add a way for the custom header to be styled in the admin panel that controls
    70 // custom headers. See twentyeleven_admin_header_style(), below.
    71 add_custom_image_header( 'twentyeleven_header_style', 'twentyeleven_admin_header_style', 'twentyeleven_admin_header_image' );
    72 
    73 // ... and thus ends the changeable header business.
     48 * Tell WordPress to run twentyeleven_setup() when the 'after_setup_theme' hook is run.
     49 */
     50add_action( 'after_setup_theme', 'twentyeleven_setup' );
     51
     52if ( ! 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 add_theme_support() To add support for post thumbnails and automatic feed links.
     64 * @uses register_nav_menus() To add support for navigation menus.
     65 * @uses add_custom_background() To add support for a custom background.
     66 * @uses load_theme_textdomain() For translation/localization support.
     67 * @uses add_custom_image_header() To add support for a custom header.
     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 */
     73function twentyeleven_setup() {
     74
     75    /**
     76     * Make theme available for translation
     77     * Translations can be filed in 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
     80     */
     81    load_theme_textdomain( 'twentyeleven', TEMPLATEPATH . '/languages' );
     82
     83    $locale = get_locale();
     84    $locale_file = TEMPLATEPATH . "/languages/$locale.php";
     85    if ( is_readable( $locale_file ) )
     86        require_once( $locale_file );
     87
     88    /**
     89     * Load up our theme options page
     90     */
     91    require( dirname( __FILE__ ) . '/inc/theme-options/theme-options.php' );
     92
     93    /**
     94     * Grab Twenty Eleven's Custom Widgets
     95     */
     96    require( dirname( __FILE__ ) . '/inc/widgets.php' );
     97
     98    /**
     99     * Add default posts and comments RSS feed links to head
     100     */
     101    add_theme_support( 'automatic-feed-links' );
     102
     103    /**
     104     * This theme uses wp_nav_menu() in one location.
     105     */
     106    register_nav_menus( array(
     107        'primary' => __( 'Primary Menu', 'twentyeleven' ),
     108    ) );
     109
     110    /**
     111     * Add support for an Aside Post Format
     112     */
     113    add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote' ) );
     114
     115    /**
     116     * Add support for custom backgrounds
     117     */
     118    add_custom_background();
     119
     120    // This theme uses Feature Images for per-post/per-page Custom Header images
     121    add_theme_support( 'post-thumbnails' );
     122
     123    /**
     124     * Add support for Custom Headers
     125     */
     126    define( 'HEADER_TEXTCOLOR', '000' );
     127
     128    // No CSS, just an IMG call. The %s is a placeholder for the theme template directory URI.
     129    define( 'HEADER_IMAGE', '%s/images/headers/default.jpg' );
     130
     131    // The height and width of your custom header. You can hook into the theme's own filters to change these values.
     132    // Add a filter to twentyeleven_header_image_width and twentyeleven_header_image_height to change these values.
     133    define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyeleven_header_image_width', 1000 ) );
     134    define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyeleven_header_image_height', 300 ) );
     135
     136    // We'll be using post thumbnails for custom header images on posts and pages.
     137    // We want them to be 940 pixels wide by 198 pixels tall.
     138    // Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
     139    set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
     140
     141    // Add Twenty Eleven's custom image sizes
     142    add_image_size( 'large-feature', HEADER_IMAGE_WIDTH, 500, true ); // Used for large feature images
     143    add_image_size( 'small-feature', 500, 500 ); // Used for featured posts if a large-feature doesn't exist
     144
     145    // Add a way for the custom header to be styled in the admin panel that controls
     146    // custom headers. See twentyeleven_admin_header_style(), below.
     147    add_custom_image_header( 'twentyeleven_header_style', 'twentyeleven_admin_header_style', 'twentyeleven_admin_header_image' );
     148
     149    // ... and thus ends the changeable header business.
     150}
     151endif; // twentyeleven_setup
    74152
    75153if ( ! function_exists( 'twentyeleven_header_style' ) ) :
     
    80158 */
    81159function twentyeleven_header_style() {
     160
    82161    // If no custom options for text are set, let's bail
    83162    // get_header_textcolor() options: HEADER_TEXTCOLOR is default, hide text (returns 'blank') or any hex value
     
    109188    <?php
    110189}
    111 endif;
     190endif; // twentyeleven_header_style
    112191
    113192if ( ! function_exists( 'twentyeleven_admin_header_style' ) ) :
     
    159238<?php
    160239}
    161 endif;
     240endif; // twentyeleven_admin_header_style
    162241
    163242if ( ! function_exists( 'twentyeleven_admin_header_image' ) ) :
     
    182261    </div>
    183262<?php }
    184 endif;
     263endif; // twentyeleven_admin_header_image
    185264
    186265/**
     
    227306add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
    228307
    229 
    230 /**
    231  * Add Twenty Eleven's custom image sizes
    232  */
    233 add_image_size( 'large-feature', HEADER_IMAGE_WIDTH, 500, true ); // Used for large feature images
    234 add_image_size( 'small-feature', 500, 500 ); // Used for featured posts if a large-feature doesn't exist
    235 
    236308/**
    237309 * Add custom body classes
    238310 */
    239 function twentyeleven_body_class($classes) {
     311function twentyeleven_singular_class( $classes ) {
    240312    if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) )
    241313        $classes[] = 'singular';
     
    243315    return $classes;
    244316}
    245 add_filter( 'body_class', 'twentyeleven_body_class' );
     317add_filter( 'body_class', 'twentyeleven_singular_class' );
    246318
    247319/**
     
    310382
    311383/**
    312  * Grab Twenty Eleven's Custom Widgets
    313  */
    314 require( dirname( __FILE__ ) . '/inc/widgets.php' );
    315 
    316 /**
    317384 * Display navigation to next/previous pages when applicable
    318385 */
    319 function twentyeleven_content_nav($nav_id) {
     386function twentyeleven_content_nav( $nav_id ) {
    320387    global $wp_query;
    321388
Note: See TracChangeset for help on using the changeset viewer.