How to Remove Parent Template Files in WordPress - dummies

How to Remove Parent Template Files in WordPress

By Lisa Sabin-Wilson

You may be asking why you would want to remove a parent’s template file in WordPress. That’s a good question. Unfortunately, the Twenty Eleven theme doesn’t provide a good example of why you would want to do this. Therefore, you must use your imagination a bit here, just to understand the mechanics of removing a file from the parent theme, for future reference.

Imagine that you’re creating a child theme built off a parent theme called Example Parent. Example Parent is well designed, and a great child theme was quickly built off it. The child theme looks and works exactly the way you want it to, but there’s a problem.

The Example Parent theme has a home.php template file that provides a highly customized nonblog home page. This template works very well but isn’t what you want for the site. You want a standard blog home page. If the home.php file didn’t exist in Example Parent, everything would work perfectly.

There isn’t a way to remove the home.php file from Example Parent without modifying the Parent theme (which you never, ever want to do), so you have to use a trick. Instead of removing the file, override the home.php file and have it emulate index.php.

You may think that simply copying and pasting the Example Parent index.php code into the child theme’s home.php file would be a good approach. Although this would work, there is a better way: You can tell WordPress to run the index.php file so that changes to index.php are respected. This single line of code inside the child theme’s home.php is all you need to replace home.php with index.php:

<?php locate_template( array( 'index.php' ), true ); ?>

The locate_template function does a bit of magic. If the child theme supplies an index.php file, it is used. If not, the parent index.php file is used. This approach produces the same result that removing the parent theme’s home.php file would have. The home.php code is ignored and the changes to index.php are respected.