I'm writing a plugin and I need to replace some files of the used theme. For example I'd like to add the header.php file because this file is in grandparent theme and not in child theme.

I replaced some template files with this code:

function grandchild_include( $template ) {
        if ( file_exists( untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' . basename( $template ) ) )
        $template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' . basename( $template );

    return $template;
add_filter( 'template_include', 'grandchild_include', 11 );

It works when the template file is in the child theme, but when the file is not in the child theme (but only in the parent theme), it not works.

I need to replace header.php . It's in the parent child and not in child theme, so the filter not inlcude header.php

And I need to replace a file (not template file) ex: myname.php (included in header.php with get_template_part() ).

  • That sounds very weird. What are you trying to do? I think you are in the wrong way; a plugin like that will break almost every site where it is installed.
    – cybmeta
    Nov 10, 2015 at 20:10
  • I'm making a grandchildtheme with a plugin. I used include_template filter to add/replace templates and now I need to replace the header.php file.
    – Erfo
    Nov 10, 2015 at 22:09
  • That doesn't explain why you need to replace header.php file. I think you are asking on what you think is the solution instead of asking about your real problem, I may be wrong. I also think that forcing a specific header.php file is a wrong approach. Anyway, looking at the source code of get_header() function I think you can not do it. Please, explain why you need to do it.
    – cybmeta
    Nov 11, 2015 at 9:48
  • The used theme has a framework and the child theme. So it's impossible create a child-child theme, so I created a plugin as grandchildtheme. Now, I'd change some thing. For example, I changed some template pages with the include_template filter. I'd like to replace header.php because I need to change the structure of bootstrap. header.php include a file with get_template_part and I want change this.
    – Erfo
    Nov 11, 2015 at 13:56
  • I edited the main post and add the full information.
    – Erfo
    Nov 11, 2015 at 14:41

1 Answer 1


template_include filter is for template files, header.php is a template part. That is why template_include doesn't work for it. As I said, looking at the source code of get_header() function, I can not see any way you can override the loaded file by that function (or any other template part). But, because it is a template part, you are not requiered to use it. You can define the full header section into a template file or include any file you may want as header.

For example, a template file loaded from the plugin folder could be like this:

<!doctype html>
<html <?php language_attributes();?>>
    <?php wp_head(); ?>
  <body id="body" <?php body_class(); ?>>
    <?php if ( have_posts() ) { ?>
      <?php while ( have_posts() ) { the_post(); ?>

      <?php } ?>       
    <?php } ?>

As you can see, I've also not used get_footer(); like get_header() and header.php, footer.php is a template part and you are not required to use it.

Also, to not repeat same code in all your template files, you could include the header and footer parts using PHP include() function:

<?php include('myheader.php'); ?>
  <?php if ( have_posts() ) { ?>
    <?php while ( have_posts() ) { the_post(); ?>

    <?php } ?>       
  <?php } ?>
<?php include('myfooter.php'); ?>
  • It's a good idea, but in this way I must create (or replace) all the current templates. In the child theme there are many templates and I can't replace all the templates and include the new header. So I wrote this: function newheader($template) { $file = untrailingslashit( DIR_PATH ) . '/templates/header.php' ; if ( locate_template( 'header.php' ) != '' ) { return untrailingslashit( DIR_PATH ) . '/templates/header.php'; } return $template; } add_filter( 'template_include', 'newheader', 10 ); This code replace the header.php file, but not return $template.
    – Erfo
    Nov 16, 2015 at 14:09
  • 1
    The code in your comment does not replace the header.php from theme, it loads only header.php from your plugin as the full template. Do you understand what I mean when I say that header.php is not a full template but a templat part? Template parts can not ben overriden using template_include filter adn as far I know there is no hook to override them. So, the only way a plugin has to replace header.php from the theme is making its own set of full templates it may needs.
    – cybmeta
    Nov 16, 2015 at 15:06
  • Yes, it's all clear.
    – Erfo
    Nov 16, 2015 at 22:11
  • Please, if this answer your question, don't forget to mark the answer as accepted.
    – cybmeta
    Nov 17, 2015 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.