WordPress Child Theme

Just under a year ago, I wrote about the changes I made to the WordPress TwentyTwelve theme to remove the comment RSS feeds from the site headers. As I was adding the new Microblog RSS feed to the header, I decided to see if I could migrate my modifications into a child theme.

The big advantage of creating a child theme, instead of modifying the parent (original) theme, is that when there is an update available to the parent theme your modifications won’t be lost.

My original modifications required changes to two files:

  1. functions.php:

    • Comment out the line that adds in all feeds:

      // add_theme_support( 'automatic-feed-links' );
      
    • Add in a filter to remove the post comment feeds:

      function disablePostCommentsFeedLink($for_comments) {
          return;
      }
      add_filter('post_comments_feed_link','disablePostCommentsFeedLink');
      
  2. header.php:

    • Added in links to the main RSS feed (since they were removed from functions.php).

To migrate the changes to a child theme, I reverted my version of the TwentyTwelve theme to its original state. You can find my child theme on Github, and you can see the torturous path I took to develop it in the commit history.

My first attempt involved duplicating the entire header.php file, with my changes, into the child theme, and I gave up on figuring out how to comment out add_theme_support( 'automatic-feed-links' ); from the child.

For the second attempt, I tried to achieve the commenting out of add_theme_support( 'automatic-feed-links' ); by removing the function calling it from the after_theme_setup hook. I struggled with figuring out what hook to attach the removal to, and consulted the Actions Run During a Typical Request list in the Codex. Ultimately I attached the removal function to the same after_theme_setup hook, but with a priority of 0:

add_action( 'after_setup_theme', 'child_remove_parent_function_twentytwelve_setup', 0 );

Once I had that working, I stumbled onto the feed_links_show_comments_feed filter, which greatly simplified the whole endeavor. The filter removes both the master comment feed and post comment feeds, which was my entire original objective. I was able to strike both the header.php file and the effort to comment out add_theme_support( 'automatic-feed-links' ); in functions.php.

I was able to accomplish the removal of the comment RSS feeds in my child theme with just this in my functions.php file1:

// disable comment feeds (this seems to disable main and post comment feeds)
add_filter( 'feed_links_show_comments_feed', '__return_false' );

The only thing left to do was add back in the Microblog RSS feed, which I injected into the wp_head hook2 (this, too, is in functions.php):

// Add in microblog feed to header
function microblog_feed() {
    echo '
<link rel="alternate" type="application/rss+xml" title="Jeff Vautin &raquo; Microblog Feed" href="/micro.xml" />
';
}
add_action('wp_head', 'microblog_feed');

I ended up with the same functionality I had before, but in just a handle of lines of code, nicely contained in a child theme. Feel free to fork it for your own use!


  1. Beyond the boilerplate needed to create a child theme

  2. I took inspiration from this Stack Overflow answer