I just learned that it is possible to create differtent header.php files in your theme by creating a new File and calling it something like: header-myname.php.

To call, i would use the header templates tag like this:

get_header( 'myname' );

But that way I would have to put my file in the main directory of my theme, am I right?

So my question is: Is there a way to put the custom-header file into a sub-directory and if so, how to i declare its position with the get_header() tag ?

  • Is there a problem this solves? Or is this a case of the theme folder feels bloated/full? WP expects header files like that in the theme folder, what you ask for isn't possible, see the answers for why – Tom J Nowell Mar 27 '18 at 13:35

No. The function adds a string of "header-{$name}.php" to the search for templates. You can't prepend it.

However, if you're not reyling on the get_header action, you can just as well use get_template_part(), as these functions work very similar.

get_template_part('subdir/header', 'my');

will look for a file in subdir/header-my.php.

  • so there is no need to use get_header, or get_footer in my index.php file for example. I can just use get_template_part instead? Or does it work any different ? – jkresse Mar 27 '18 at 13:26
  • It works very similar. The only difference, as Jacob Peattie pointed out in more detail, is that get_header() calls the action get_header, which get_template_part() does not by default. You can call the action in the PHP file or leave it be, depending on your other plugins/themes – kero Mar 27 '18 at 16:40

Not with get_header(), no. As you can see in the source of get_header() the argument is simply appended to the filename and searched for in the root theme directory:

$name = (string) $name;
if ( '' !== $name ) {
    $templates[] = "header-{$name}.php";

$templates[] = 'header.php';

locate_template( $templates, true );

And as far as I can tell there are no filters that would be useful here. As Justin Tadlock noted, this lack of filters has persisted for 8 years.

The next best thing you could do is to use get_template_part() instead:

get_template_part( 'subdirectory/header', 'myname' );

But you'll need your regular header in the subdirectory too.

The only thing you'll miss out on doing it this way is that the get_header() hook won't fire. But you could remedy that by putting it at the beginning of your custom template:

do_action( 'get_header' );

or for the custom template:

do_action( 'get_header', 'myname' );

But honestly, I'm not sure how widely used that hook is, because it's not very useful. You can probably get away with leaving it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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