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.


git-svn-id: http://svn.automattic.com/wordpress/trunk@17721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
iandstewart 2011-04-26 22:52:18 +00:00
parent 2bcbcf31ec
commit 90451d9052
10 changed files with 510 additions and 65 deletions

View File

@ -0,0 +1 @@
/* Placeholder for a Twenty Eleven dark color scheme */

View File

@ -1,22 +1,43 @@
<?php
/**
* Twenty Eleven functions and definitions
*
* Sets up the theme and provides some helper functions. Some helper functions
* are used in the theme as custom template tags. Others are attached to action and
* filter hooks in WordPress to change core functionality.
*
* The first function, twentyeleven_setup(), sets up the theme by registering support
* for various features in WordPress, such as post thumbnails, navigation menus, and the like.
*
* When using a child theme (see http://codex.wordpress.org/Theme_Development and
* http://codex.wordpress.org/Child_Themes), you can override certain functions
* (those wrapped in a function_exists() call) by defining them first in your child theme's
* functions.php file. The child theme's functions.php file is included before the parent
* theme's file, so the child theme functions would be used.
*
* Functions that are not pluggable (not wrapped in function_exists()) are instead attached
* to a filter or action hook. The hook can be removed by using remove_action() or
* remove_filter() and you can attach your own function to the hook.
*
* We can remove the parent theme's hook only after it is attached, which means we need to
* wait until setting up the child theme:
*
* <code>
* add_action( 'after_setup_theme', 'my_child_theme_setup' );
* function my_child_theme_setup() {
* // We are providing our own filter for excerpt_length (or using the unfiltered value)
* remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
* ...
* }
* </code>
*
* For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
*
* @package WordPress
* @subpackage Twenty Eleven
* @since Twenty Eleven 1.0
*/
/**
* Make theme available for translation
* Translations can be filed in the /languages/ directory
* If you're building a theme based on Twenty Eleven, use a find and replace
* to change 'twentyeleven' to the name of your theme in all the template files
*/
load_theme_textdomain( 'twentyeleven', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
/**
* Set the content width based on the theme's design and stylesheet.
*/
@ -24,53 +45,110 @@ if ( ! isset( $content_width ) )
$content_width = 584;
/**
* This theme uses wp_nav_menu() in one location.
* Tell WordPress to run twentyeleven_setup() when the 'after_setup_theme' hook is run.
*/
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'twentyeleven' ),
) );
add_action( 'after_setup_theme', 'twentyeleven_setup' );
if ( ! function_exists( 'twentyeleven_setup' ) ):
/**
* Add default posts and comments RSS feed links to head
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* To override twentyeleven_setup() in a child theme, add your own twentyeleven_setup to your child theme's
* functions.php file.
*
* @uses add_theme_support() To add support for post thumbnails and automatic feed links.
* @uses register_nav_menus() To add support for navigation menus.
* @uses add_custom_background() To add support for a custom background.
* @uses load_theme_textdomain() For translation/localization support.
* @uses add_custom_image_header() To add support for a custom header.
* @uses register_default_headers() To register the default custom header images provided with the theme.
* @uses set_post_thumbnail_size() To set a custom post thumbnail size.
*
* @since Twenty Eleven 1.0
*/
add_theme_support( 'automatic-feed-links' );
function twentyeleven_setup() {
/**
* Add support for an Aside Post Format
*/
add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote' ) );
/**
* Make theme available for translation
* Translations can be filed in the /languages/ directory
* If you're building a theme based on Twenty Eleven, use a find and replace
* to change 'twentyeleven' to the name of your theme in all the template files
*/
load_theme_textdomain( 'twentyeleven', TEMPLATEPATH . '/languages' );
/**
* Add support for custom backgrounds
*/
add_custom_background();
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
// This theme uses Feature Images for per-post/per-page Custom Header images
add_theme_support( 'post-thumbnails' );
/**
* Load up our theme options page
*/
require( dirname( __FILE__ ) . '/inc/theme-options/theme-options.php' );
/**
* Add support for Custom Headers
*/
define( 'HEADER_TEXTCOLOR', '000' );
/**
* Grab Twenty Eleven's Custom Widgets
*/
require( dirname( __FILE__ ) . '/inc/widgets.php' );
// No CSS, just an IMG call. The %s is a placeholder for the theme template directory URI.
define( 'HEADER_IMAGE', '%s/images/headers/default.jpg' );
/**
* Add default posts and comments RSS feed links to head
*/
add_theme_support( 'automatic-feed-links' );
// The height and width of your custom header. You can hook into the theme's own filters to change these values.
// Add a filter to twentyeleven_header_image_width and twentyeleven_header_image_height to change these values.
define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyeleven_header_image_width', 1000 ) );
define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyeleven_header_image_height', 300 ) );
/**
* This theme uses wp_nav_menu() in one location.
*/
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'twentyeleven' ),
) );
// We'll be using post thumbnails for custom header images on posts and pages.
// We want them to be 940 pixels wide by 198 pixels tall.
// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
/**
* Add support for an Aside Post Format
*/
add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote' ) );
// Add a way for the custom header to be styled in the admin panel that controls
// custom headers. See twentyeleven_admin_header_style(), below.
add_custom_image_header( 'twentyeleven_header_style', 'twentyeleven_admin_header_style', 'twentyeleven_admin_header_image' );
/**
* Add support for custom backgrounds
*/
add_custom_background();
// ... and thus ends the changeable header business.
// This theme uses Feature Images for per-post/per-page Custom Header images
add_theme_support( 'post-thumbnails' );
/**
* Add support for Custom Headers
*/
define( 'HEADER_TEXTCOLOR', '000' );
// No CSS, just an IMG call. The %s is a placeholder for the theme template directory URI.
define( 'HEADER_IMAGE', '%s/images/headers/default.jpg' );
// The height and width of your custom header. You can hook into the theme's own filters to change these values.
// Add a filter to twentyeleven_header_image_width and twentyeleven_header_image_height to change these values.
define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyeleven_header_image_width', 1000 ) );
define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyeleven_header_image_height', 300 ) );
// We'll be using post thumbnails for custom header images on posts and pages.
// We want them to be 940 pixels wide by 198 pixels tall.
// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
// Add Twenty Eleven's custom image sizes
add_image_size( 'large-feature', HEADER_IMAGE_WIDTH, 500, true ); // Used for large feature images
add_image_size( 'small-feature', 500, 500 ); // Used for featured posts if a large-feature doesn't exist
// Add a way for the custom header to be styled in the admin panel that controls
// custom headers. See twentyeleven_admin_header_style(), below.
add_custom_image_header( 'twentyeleven_header_style', 'twentyeleven_admin_header_style', 'twentyeleven_admin_header_image' );
// ... and thus ends the changeable header business.
}
endif; // twentyeleven_setup
if ( ! function_exists( 'twentyeleven_header_style' ) ) :
/**
@ -79,6 +157,7 @@ if ( ! function_exists( 'twentyeleven_header_style' ) ) :
* @since Twenty Eleven 1.0
*/
function twentyeleven_header_style() {
// If no custom options for text are set, let's bail
// get_header_textcolor() options: HEADER_TEXTCOLOR is default, hide text (returns 'blank') or any hex value
if ( HEADER_TEXTCOLOR == get_header_textcolor() )
@ -108,7 +187,7 @@ function twentyeleven_header_style() {
</style>
<?php
}
endif;
endif; // twentyeleven_header_style
if ( ! function_exists( 'twentyeleven_admin_header_style' ) ) :
/**
@ -158,7 +237,7 @@ function twentyeleven_admin_header_style() {
</style>
<?php
}
endif;
endif; // twentyeleven_admin_header_style
if ( ! function_exists( 'twentyeleven_admin_header_image' ) ) :
/**
@ -181,7 +260,7 @@ function twentyeleven_admin_header_image() { ?>
<img src="<?php esc_url ( header_image() ); ?>" alt="" />
</div>
<?php }
endif;
endif; // twentyeleven_admin_header_image
/**
* Sets the post excerpt length to 40 characters.
@ -226,23 +305,16 @@ function twentyeleven_custom_excerpt_more( $output ) {
}
add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
/**
* Add Twenty Eleven's custom image sizes
*/
add_image_size( 'large-feature', HEADER_IMAGE_WIDTH, 500, true ); // Used for large feature images
add_image_size( 'small-feature', 500, 500 ); // Used for featured posts if a large-feature doesn't exist
/**
* Add custom body classes
*/
function twentyeleven_body_class($classes) {
function twentyeleven_singular_class( $classes ) {
if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) )
$classes[] = 'singular';
return $classes;
}
add_filter( 'body_class', 'twentyeleven_body_class' );
add_filter( 'body_class', 'twentyeleven_singular_class' );
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
@ -308,15 +380,10 @@ function twentyeleven_widgets_init() {
}
add_action( 'init', 'twentyeleven_widgets_init' );
/**
* Grab Twenty Eleven's Custom Widgets
*/
require( dirname( __FILE__ ) . '/inc/widgets.php' );
/**
* Display navigation to next/previous pages when applicable
*/
function twentyeleven_content_nav($nav_id) {
function twentyeleven_content_nav( $nav_id ) {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) : ?>

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 B

View File

@ -0,0 +1,22 @@
#wpcontent select option {
padding-right: 5px;
}
.image-radio-option td {
padding-top: 15px;
}
.image-radio-option label {
display: block;
float: left;
margin: 0 30px 20px 2px;
position: relative;
}
.image-radio-option input {
margin: 0 0 10px;
}
.image-radio-option span {
display: block;
width: 136px;
}
.image-radio-option img {
margin: 0 0 0 -2px;
}

View File

@ -0,0 +1,39 @@
var farbtastic;
function pickColor(a) {
farbtastic.setColor(a);
jQuery("#link-color").val(a);
jQuery("#link-color").css("background-color", a);
}
jQuery(document).ready(function() {
jQuery("#pickcolor").click(function() {
jQuery("#colorPickerDiv").show();
return false;
});
jQuery("#link-color").keyup(function() {
var b = jQuery("#link-color").val(),
a = b;
if (a.charAt(0) != "#") {
a = "#" + a;
}
a = a.replace(/[^#a-fA-F0-9]+/, "");
if (a != b) {
jQuery("#link-color").val(a);
}
if (a.length == 4 || a.length == 7) {
pickColor(a);
}
});
farbtastic = jQuery.farbtastic("#colorPickerDiv",
function(a) {
pickColor(a);
});
pickColor(jQuery("#link-color").val());
jQuery(document).mousedown(function() {
jQuery("#colorPickerDiv").each(function() {
var a = jQuery(this).css("display");
if (a == "block") {
jQuery(this).fadeOut(2);
}
});
});
});

View File

@ -0,0 +1,316 @@
<?php
/**
* Twenty Eleven Theme Options
*
* @package WordPress
* @subpackage Twenty Eleven
*/
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
/**
* Add theme options page styles and scripts
*/
wp_register_style( 'twentyeleven-theme-options', get_template_directory_uri() . '/inc/theme-options/theme-options.css', '', '0.1' );
wp_register_script( 'twentyeleven-theme-options', get_template_directory_uri() . '/inc/theme-options/theme-options.js' );
if ( isset( $_GET['page'] ) && $_GET['page'] == 'theme_options' ) {
wp_enqueue_style( 'twentyeleven-theme-options' );
wp_enqueue_script( 'twentyeleven-theme-options' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_style( 'farbtastic' );
}
/**
* Init plugin options to white list our options
*/
function theme_options_init(){
register_setting( 'twentyeleven_options', 'twentyeleven_theme_options', 'twentyeleven_theme_options_validate' );
}
/**
* Load up the menu page
*/
function theme_options_add_page() {
add_theme_page( __( 'Theme Options', 'twentyeleven' ), __( 'Theme Options', 'twentyeleven' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
/**
* Return array for our color schemes
*/
function twentyeleven_color_schemes() {
$color_scheme_options = array(
'light' => array(
'value' => 'light',
'label' => __( 'Light', 'twentyeleven' )
),
'dark' => array(
'value' => 'dark',
'label' => __( 'Dark', 'twentyeleven' )
),
);
return $color_scheme_options;
}
/**
* Return array for our layout options
*/
function twentyeleven_layouts() {
$layout_options = array(
'content-sidebar' => array(
'value' => 'content-sidebar',
'label' => __( 'Content on left', 'twentyeleven' ),
),
'sidebar-content' => array(
'value' => 'sidebar-content',
'label' => __( 'Content on right', 'twentyeleven' )
),
'content' => array(
'value' => 'content',
'label' => __( 'One-column, no Sidebar', 'twentyeleven' )
),
);
return $layout_options;
}
/**
* Return the current Twenty Eleven theme options, with default values as fallback
*/
function twentyeleven_get_theme_options() {
$defaults = array(
'color_scheme' => 'light',
'link_color' => '1b8be0',
'theme_layout' => 'content-sidebar',
);
$options = get_option( 'twentyeleven_theme_options', $defaults );
return $options;
}
/**
* Create the options page
*/
function theme_options_do_page() {
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'twentyeleven' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved', 'twentyeleven' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'twentyeleven_options' ); ?>
<?php $options = twentyeleven_get_theme_options(); ?>
<table class="form-table">
<?php
/**
* Color Scheme Options
*/
?>
<tr valign="top" class="image-radio-option"><th scope="row"><?php _e( 'Color Scheme', 'twentyeleven' ); ?></th>
<td>
<fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend>
<?php
if ( ! isset( $checked ) )
$checked = '';
foreach ( twentyeleven_color_schemes() as $option ) {
$radio_setting = $options['color_scheme'];
if ( '' != $radio_setting ) {
if ( $options['color_scheme'] == $option['value'] ) {
$checked = "checked=\"checked\"";
} else {
$checked = '';
}
}
?>
<div class="layout">
<label class="description">
<input type="radio" name="twentyeleven_theme_options[color_scheme]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> />
<span>
<img src="<?php echo get_template_directory_uri(); ?>/inc/theme-options/images/<?php echo $option['value']; ?>.png"/>
<?php echo $option['label']; ?>
</span>
</label>
</div>
<?php
}
?>
</fieldset>
</td>
</tr>
<?php
/**
* Link Color Options
*/
?>
<tr valign="top"><th scope="row"><?php _e( 'Link Color', 'twentyeleven' ); ?></th>
<td>
<fieldset><legend class="screen-reader-text"><span><?php _e( 'Link Color', 'twentyeleven' ); ?></span></legend>
<input type="text" name="twentyeleven_theme_options[link_color]" id="link-color" value="<?php esc_attr_e( $options['link_color'] ); ?>" />
<a class="hide-if-no-js" href="#" id="pickcolor"><?php _e( 'Select a Color', 'twentyeleven' ); ?></a>
<div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
<br />
<small class="description"><?php printf( __( 'Default color: %s', 'twentyeleven' ), '#1b8be0' ); ?></small>
</fieldset>
</td>
</tr>
<?php
/**
* Layout Options
*/
?>
<tr valign="top" class="image-radio-option"><th scope="row"><?php _e( 'Layout', 'twentyeleven' ); ?></th>
<td>
<fieldset><legend class="screen-reader-text"><span><?php _e( 'Color Scheme', 'twentyeleven' ); ?></span></legend>
<?php
if ( ! isset( $checked ) )
$checked = '';
foreach ( twentyeleven_layouts() as $option ) {
$radio_setting = $options['theme_layout'];
if ( '' != $radio_setting ) {
if ( $options['theme_layout'] == $option['value'] ) {
$checked = "checked=\"checked\"";
} else {
$checked = '';
}
}
?>
<div class="layout">
<label class="description">
<input type="radio" name="twentyeleven_theme_options[theme_layout]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> />
<span>
<img src="<?php echo get_template_directory_uri(); ?>/inc/theme-options/images/<?php echo $option['value']; ?>.png"/>
<?php echo $option['label']; ?>
</span>
</label>
</div>
<?php
}
?>
</fieldset>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'twentyeleven' ); ?>" />
</p>
</form>
</div>
<?php
}
/**
* Sanitize and validate input. Accepts an array, return a sanitized array.
*/
function twentyeleven_theme_options_validate( $input ) {
// todo get defaults, and use insteadd of null for fallback
// could also be used to trigger a Reset Options action
// Our color scheme option must actually be in our array of color scheme options
if ( ! isset( $input['color_scheme'] ) )
$input['color_scheme'] = null;
if ( ! array_key_exists( $input['color_scheme'], twentyeleven_color_schemes() ) )
$input['color_scheme'] = null;
// Our link color option must be safe text with no HTML tags
$input['link_color'] = wp_filter_nohtml_kses( $input['link_color'] );
// Our theme layout option must actually be in our array of theme layout options
if ( ! isset( $input['theme_layout'] ) )
$input['theme_layout'] = null;
if ( ! array_key_exists( $input['theme_layout'], twentyeleven_layouts() ) )
$input['theme_layout'] = null;
return $input;
}
/**
* Returns the current Twenty Eleven color scheme as selected in the theme options
*
* @since Twenty Eleven 1.0
*/
function twentyeleven_current_color_scheme() {
$options = twentyeleven_get_theme_options();
return $options['color_scheme'];
}
/**
* Register our color schemes and add them to the queue
*/
function twentyeleven_color_registrar() {
$color_scheme = twentyeleven_current_color_scheme();
if ( 'dark' == $color_scheme ) {
wp_register_style( 'dark', get_template_directory_uri() . '/colors/dark.css', null, null );
wp_enqueue_style( 'dark' );
}
}
if ( ! is_admin() )
add_action( 'wp_print_styles', 'twentyeleven_color_registrar' );
/**
* Returns the current Twenty Eleven layout as selected in the theme options
*
* @since Twenty Eleven 1.0
*/
function twentyeleven_current_layout() {
$options = twentyeleven_get_theme_options();
$current_layout = $options['theme_layout'];
$two_columns = array( 'content-sidebar', 'sidebar-content' );
if ( in_array( $current_layout, $two_columns ) )
return 'two-column ' . $current_layout;
else
return 'one-column ' . $current_layout;
}
/**
* Add an internal style block for the link color to wp_head()
*/
function twentyeleven_link_color() {
$options = twentyeleven_get_theme_options();
$current_link_color = $options['link_color'];
// Is the link color just the default color?
if ( '1b8be0' == $current_link_color ) :
return; // we don't need to do anything then
else :
?>
<style>
/* Link color */
a,
.entry-title a:hover {
color: <?php echo $current_link_color ?>;
}
</style>
<?php
endif;
}
add_action( 'wp_head', 'twentyeleven_link_color' );
/**
* Adds twentyeleven_current_layout() to the array of body classes
*
* @since Twenty Eleven 1.0
*/
function twentyeleven_layout_class( $classes ) {
$classes[] = twentyeleven_current_layout();
return $classes;
}
add_filter( 'body_class', 'twentyeleven_layout_class' );