11
votes

I start new projects by writing a child theme for the twentyeleven theme. I rarely design the new theme to use any of the options built into the twentyeleven theme (such as background color, etc). Those residual options don't really hurt anything, but I'd like to get rid of them since they don't do anything.

The trouble is that the theme options are declared in the parent theme's functions.php, which is loaded along with (not instead of) the child theme's functions.php file (so I could delete them but they'll come back next upgrade).

Is there a way to remove or disable those theme options from my child theme? Perhaps something along the lines of a "remove_options()" function? Or perhaps something that would achieve that effect? In other words, the question is whether theme_options can be removed WITHOUT deleting/overriding the original function that added them.

I'm sure with enough puttering, I could hide the option with CSS or javascript... but c'mon.

4

4 Answers

14
votes

After a second round of digging...

This is TOTALLY easy!

You can retrace my steps by starting here, but the code is pretty self-explanatory:

add_action( 'init', 'remove_crap' );
    function remove_crap() {

    remove_custom_image_header();
    remove_custom_background();
    remove_theme_support('post-formats');
}

You can look these up in the codex. Remove_theme_support takes one of several strings that identify various options (besides just post-formats). The only issue I encountered is that they need to be called from a hook (you can't just dumpt them into functions.php). I'm using init but there's probably another one that's more appropriated.

The only thing I still haven't figured out is how to remove the "Theme Options" page link that appears under Appearances. I know it's added with add_theme_page() but there doesn't seem to be a handy remove_theme_page().

UPDATE: I found it! This is VERY poorly documented, but in the end it's quite easy to do:

add_action('admin_init', 'remove_twentyeleven_theme_options', 11);
     function remove_twentyeleven_theme_options() {
 remove_submenu_page('themes.php', 'theme_options');
}

In my example, 'themes.php' targets the Appearances menu and 'theme_options' is the menu_slug used in the twentyeleven theme. Obviously these parameters will differ depending on which menu or submenu you're editing. This page will point you in the right direction.

ps: Here's how to get rid of templates from the parent theme that you don't want to use: THIS isn't essential to my exact question, but it's closely related and probably useful to anyone who's trying to do what I'm doing.

10
votes

The correct way to remove theme support added in a parent theme from the child theme is to make the call to remove_theme_support in an after_setup_theme action called with a lower priority than that of the parent.

The functions.php file from a child themes is called immediately before that of the parent theme, so if you use the default priority for after_setup_theme, the child's after_setup_theme ends up getting called before that of the parent, so you end up removing non-existant theme support in your child, only to have it added back in from the parent running after_setup_theme.

So by adding your child action with a lower priority, you can ensure it gets called after the parent's call to the same action.

So:

// added to child's functions.php    

add_action( 'after_setup_theme', 'child_after_setup_theme', 11 ); 
// Parent theme uses the default priority of 10, so
// use a priority of 11 to load after the parent theme.

function child_after_setup_theme()
{
    remove_theme_support('custom-background');
    remove_theme_support('custom-header');
    remove_theme_support('post-formats');
    // ... etc.
}

In the case of the twentyeleven theme, you could also just over-ride the entire twentyeleven_setup function in your child's functions.php, but that is a rather unsubtle method of achieving this.

1
votes

Unfortunately, the way that theme inheritance works in Wordpress' case is that child theme functions are just "added on" to the parent theme functions.

Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)(1)

So, in direct answer to your question above, it looks(2) like this might not possible with the way WordPress handles themes and child themes.

Personally, I wouldn't worry about having those extra functions or variables in the functions.php file.

  1. http://codex.wordpress.org/Child_Themes
  2. http://wordpress.org/support/topic/theme-options-in-child-theme
1
votes

This is an old thread so I just want to add if someone enters this place and want an answer. I solve the child theme by taking that exact file, making a copy and adding it to child theme. I have the plugin "advanced code editor" so I dont need to go in FTP. Copy the specific file you want to edit, make a new sheet in child theme with same name and content, and then do the edits you want there. It will fetch the child theme files first and your site will be updated.