Stephanie Leary

Writer and WordPress consultant

  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Fascism Watch (2016-17)
    • Content Modeling for WordPress series
    • WordPress Hidden Gems series
  • Work
    • Portfolio
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →
  • About
    • Press Kit
  • Contact
    • Mailing List

Beginning WordPress 3, Appendix 2: Theme Functions

9781430258667-tinyBeginning WordPress 3 is outdated! You should get the new edition instead: WordPress for Web Developers. (It’s the same book, only updated; we changed the title to make its intended audience clearer.)

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 <br ?-->/*
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' => '</pre>
<ul>
	<li class="widget-container %2$s" id="%1$s">',
		'after_widget' => "</li>
</ul>
<pre>

",
		'before_title' => '</pre>
<h3 class="widget-title">',
 'after_title' => '</h3>
<pre>
',
	) );
	register_sidebar( array(
		'name' => 'Second Widget Area',
		'id' => 'second-widget',
		'description' => __( 'The second widget area'),
		'before_widget' => '</pre>
<ul>
	<li class="widget-container %2$s" id="%1$s">',
		'after_widget' => "</li>
</ul>
<pre>

",
		'before_title' => '</pre>
<h3 class="widget-title">',
 'after_title' => '</h3>
<pre>
',
	) );
}

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

?>

Fascism Watch

The Fascism Watch is a daily(ish) news roundup. View all the previous Fascism Watch posts »

Latest WordPress Book

Content Strategy for WordPress

A short book for content strategists and managers on implementing a complete content strategy in WordPress: evaluation, analysis, content modeling, editing and workflows, and long-term planning and maintenance.

Read the sample chapter

Kindle Nook iBooks Kobo Smashwords

WordPress for Web Developers

WordPress for Web Developers (9781430258667)

This is a book for professional web designers and developers who already know HTML and CSS, and want to learn to build sites with WordPress. The book begins with a detailed tour of the administration screens and settings, then digs into server-side topics like performance and security. The second half of the book is devoted to development: learning to build WordPress themes and plugins.

This is the second, much-revised and updated edition of Beginning WordPress 3, with a more accurate title. Everything’s been updated for WordPress 3.6.

WordPress for Web Developers is out now. See what's inside...

The best WordPress features you’ve never noticed

  • WordPress Hidden Gems: Screen Options
  • WordPress Hidden Gems: Bulk Edit
  • WordPress Hidden Gems: Private Status
  • WordPress Hidden Gems: Dashboard Feed Readers
  • WordPress Hidden Gems: Options.php

Content Modeling for WordPress series

  • Content modeling for WordPress, part 1: analyze content
  • Content modeling for WordPress, part 2: functional and organizational requirements
  • Content modeling for WordPress, part 3: a sample content model

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Copyright © 2021 Stephanie Leary