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.

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',

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',

'before_widget' => '<li id="%l$s" class="widget-container %2$s">',

'before_title' => '<h3 class="widget-title">',

register_sidebar( array(

'name' => 'Second Widget Area', 'id' => 'second-widget',

'before_widget' => '<li id="%l$s" class="widget-container %2$s">',

'before_title' => '<h3 class="widget-title">',

// 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 '';

// Allow subscribers to view private posts and pages // (Chapter l0, 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); ?>

List Empire

List Empire

Discover the insider secrets to building profitable mailing lists quickly and easily. You can literally explode your income with a SINGLE list! Find out how.

Get My Free Ebook


Post a comment