WordPress Twenty Eleven Child Theme Shiv

twenty eleven child theme

WordPress’s Twenty Eleven child themes make great use of HTML5, but suffer from one particular flaw. They require you to hack apart functions hijacking your code in unexpected situations. Because of this, I’ve created a Twenty Eleven child theme shiv that removes all the extra junk, so you don’t have to worry about manually cleaning everything.

How it Works

To use the following code shiv, store it in a file called twentyeleven-shiv.php inside your child theme. Most people would tell you to place it in functions.php, but its a bad idea. Shoving too much code in functions.php turns it into an unobtainable mess over time.

<?php

/*
Title: Twenty Eleven Child Theme Shiv
Version: .02
Author: Ash Blue
Author URL: http://www.blueashes.com
Repository URL: https://gist.github.com/gists/1154369/
*/

/************
Calls / Hooks
************/
add_filter('the_content_more_link', 'remove_more_jump_link');
add_action('after_setup_theme', 'my_child_theme_setup', 11);

/************
WordPress Core Hacks
************/
// Stop read more from following a bookmark and jumping down the page
function remove_more_jump_link($link) {
        $offset = strpos($link, '#more-');
        if ($offset) {	$end = strpos($link, '"',$offset); }
        if ($end) { $link = substr_replace($link, '', $offset, $end-$offset); }
        return $link;
}

/************
TwentyEleven Reset
************/
function my_child_theme_setup() {
        // Set default editor width
        $content_width = 620;

        // Set new thumbnail size
        set_post_thumbnail_size(460, 220, true);

        // Remove sidebar
        remove_filter( 'widgets_init', 'twentyeleven_widgets_init' );

        // Remove menu registration
        unregister_nav_menu('primary');

        // Setup post formats
        remove_theme_support('post-formats');
        add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image', 'video' ) );

        // Remove features and extra settings
        remove_action('admin_menu', array(&$GLOBALS['custom_background'], 'init'));
        remove_custom_image_header();
        remove_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );
}

// Remove default header images
function wp_remove_header_images() {
        unregister_default_headers( array('wheel','shore','trolley','pine-cone','chessboard','lanterns','willow','hanoi'));
}
add_action( 'after_setup_theme', 'wp_remove_header_images', 11 );

?>

Although I told you not to place the Twenty Eleven Child Theme shiv in functions.php, that isn’t quite true. It wont activate until we reference it there (origin point for most custom code in themes). Include the following code snippet at the top of functions.php and you’ll be good to go.


include STYLESHEETPATH . '/include/twentyeleven-shiv.php';

I normally like to credit authors for their code snippets in files I assemble, but there are too many to list. A lot of these code chunks literally came from 2 – 3 sources and were combined for the desired functionality.

What Twenty Eleven Child Theme Shiv Does

Although some of the code here may look unfamiliar, my notes should fill you in on the functionality. If you don’t like anything I’m doing, simply comment it out and the shiv will still work perfectly fine. Since you’re here though, I’ll walk through step by step on how the shiv works.

Remove Read More Jumping

Since Twenty Eleven doesn’t stop your content’s ‘Read More’ links from following a bookmark and jumping down the page, I’ve included a fix. A majority of developers remove this anyway, so it prevents re-writing code.

function remove_more_jump_link($link) {
        $offset = strpos($link, '#more-');
        if ($offset) {	$end = strpos($link, '"',$offset); }
        if ($end) { $link = substr_replace($link, '', $offset, $end-$offset); }
        return $link;
}
add_filter('the_content_more_link', 'remove_more_jump_link');

Resets editor and thumbnail sizes

You can set the content width for your admin editor in-case your page’s size is different. Post thumbnail sizes can be set back to whatever you want.

// Set default editor width
$content_width = 620;

// Set new thumbnail size
set_post_thumbnail_size(460, 220, true);

Removes Sidebar, Menus, and Post Formats

I personally don’t care for the sidebar and menu naming schemes carried into a Twenty Eleven Child Theme. I always end up renaming them, so they’ve been removed. Post formats have also been removed. Since you probably only need a few, you can tell it what particular formats you want right after removing them.

// Remove sidebar
remove_filter( 'widgets_init', 'twentyeleven_widgets_init' );

// Remove menu registration
unregister_nav_menu('primary');

// Setup post formats
remove_theme_support('post-formats');
add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image', 'video' ));

Removes Theme Options, Background, and Header Settings from admin

Since you probably don’t want your child themes to inherit 3 different setting menus, I’ve disabled them. If you want to add settings to your theme, you’re better off writing pages from scratch with the Settings API (trust me).

// Remove features and extra settings
remove_action('admin_menu', array(&$GLOBALS['custom_background'], 'init'));
remove_custom_image_header();
remove_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );

Default Header Theme Removal

At the bottom you’ll see a chunk of code that removes all default header images. You’ll find it very useful when working on a commercial project that only needs a few header images to choose from.

// Remove default header images
function wp_remove_header_images() {
        unregister_default_headers( array('wheel','shore','trolley','pine-cone','chessboard','lanterns','willow','hanoi'));
}
add_action( 'after_setup_theme', 'wp_remove_header_images', 11 );

2 thoughts on “WordPress Twenty Eleven Child Theme Shiv”

Leave a Reply

Your email address will not be published. Required fields are marked *