• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Content Modeling for WordPress
    • WordPress Hidden Gems
    • Web Design
  • Work
    • MLIS Class Projects (2019-2022)
    • Portfolio (2002-2019)
    • Services
    • WordPress Plugins
    • WordPress Themes
    • Presentations and Interviews
    • on GitHub →

Stephanie Leary

Writer, WordPress consultant, recent MLIS grad

  • About
    • Press Kit
    • Presentations and Interviews
  • Contact Me

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

?>
You are here: Home / Books / Beginning WordPress 3 / Beginning WordPress 3, Appendix 2: Theme Functions

Footer

My Books

I’m a front end developer at Equinox OLI, working on open source library software. I was previously a freelance WordPress developer in higher education. You can get in touch here or on LinkedIn.

Copyright © 2023 Stephanie Leary · Contact