Appendix B. Theme Functions

This sample theme functions file includes many of the tricks shown throughout this book. Save it as functions.php in your theme directory, or copy the features you need into your existing theme functions file.

<?php
/*
Theme Functions
*/

// Removing the meta generator tag
// (Chapter 11, Performance and Security)
remove_action('wp_head', 'wp_generator'),

// Changing excerpt length
// (Chapter 6, Basic Themes)
function change_excerpt_length($length) {
        return 100;
}
add_filter('excerpt_length', 'change_excerpt_length'),

// Changing excerpt more
// (Chapter 6, Basic Themes)
function change_excerpt_more($more) {
        return '...';
}
add_filter('excerpt_more', 'change_excerpt_more'),

// Add excerpts to pages
// (Chapter 12, Custom Content Types, Taxonomies, and Fields)
function add_page_excerpt_meta_box() {
        add_meta_box( 'postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal',
'core' );
}
add_action( 'admin_menu', 'add_page_excerpt_meta_box' );

// Add support for menus
// (Chapter 6, Basic Themes)
add_theme_support( 'nav-menus' );

// Add support for thumbnails
// (Chapter 6, Basic Themes)
add_theme_support( 'post-thumbnails' );

// Add support for backgrounds
// (Chapter 6, Basic Themes)
add_custom_background();

// Defining two widgets
// (Chapter 6, Basic Themes)
function my_widgets_init() {
        register_sidebar( array(
                'name' => 'First Widget Area',
                'id' => 'first-widget-area',
                'description' => __( 'The first widget area'),
                'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
                'after_widget' => "</li>",
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
        ) );
        register_sidebar( array(
                'name' => 'Second Widget Area',
                'id' => 'second-widget',
                'description' => __( 'The second widget area'),
                'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
                'after_widget' => "</li>",
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
        ) );
}

// Add the widget areas
add_action( 'init', 'my_widgets_init' );

// Enable shortcodes in widgets
// (Chapter 6, Basic Themes)
add_filter('the_excerpt', 'shortcode_unautop'),
add_filter('the_excerpt', 'do_shortcode'),

// Add an editorial comment shortcode
// (Chapter 10, Users and Roles)
// Usage: [ed]this is a note only editors can read.[/ed]
function editorial_note($content = null) {
    if (current_user_can('edit_pages') && is_single())
        return '<span class="private">'.$content.'</span>';
    else return '';
}
add_shortcode( 'ed', 'editorial_note' );

// Allow subscribers to view private posts and pages
// (Chapter 10, Users and Roles)
$PrivateRole = get_role('subscriber'),
$PrivateRole -> add_cap('read_private_pages'),
$PrivateRole -> add_cap('read_private_posts'),

// Change user contact fields
// (Chapter 10, Users and Roles)
function change_contactmethod( $contactmethods ) {
        // Add some fields
        $contactmethods['twitter'] = 'Twitter Name (no @)';
        $contactmethods['phone'] = 'Phone Number';
        $contactmethods['title'] = 'Title';
        // Remove AIM, Yahoo IM, Google Talk/Jabber
        unset($contactmethods['aim']);
        unset($contactmethods['yim']);
        unset($contactmethods['jabber']);
        // Make it go!
        return $contactmethods;
}
add_filter('user_contactmethods','change_contactmethod',10,1);

?>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset