1

I would like to use a custom header, like get_header('new')

But I cannot modify the templates easily where the calls are being made due to several reasons. (many files and same code base deployed on several different domains where some domains must not load the new header and use the same templates, so i want to load the new header on one place with a condition based on domain)

Is there any way I could make a hook and call the header function with the parameter myself, but then NOT calling the usual default call?

function new_get_header(){
    get_header('new');
    //die or something
}
add_action('get_header', 'new_get_header');

Any tips appreciated.

3

No, you cannot do that, unfortunately, because get_header( $name ) doesn't have a filter for the $name (it only passes the name with the action call).

However, if you are willing to modify the header.php file for each site with something like this right at the beginning of the file:

<?php
if ( apply_filters( 'load_custom_header', false ) ) {   
    $custom_header = apply_filters( 'get_custom_header', '' );

    if ( '' != $custom_header ) {       
        // Get the header that we just received
        // and call the native 'get_header' function
        // as usual
        get_header( $custom_header );

        // By calling 'return' we are skipping
        // parsing this template any further        
        return;
    }
}

You can then control what header to load like this (you can either place this code in a plugin or in the functions.php file):

// We are hooking to the 'load_custom_header' filter 
// and return a value of 'true' meaning that we want to 
// load a custom header
add_filter( 'load_custom_header', '__return_true', 99 );

add_filter( 'get_custom_header', function( $header ) {
    // Figure out what header you want
    $some_condition = true;

    if ( $some_condition ) {
        $header = 'new';            
    } 

    return $header;
}, 99 );

The obvious advantage is that you don't have to modify all of the get_header calls from all of the templates: single.php, page.php etc. you control all of them in a single place.

  • Excellent, it works partly. I cannot add that code to all the header files that are actually being loaded otherwise It will always get to the return statement and the header wont load. Is that correct? – Sanne Aug 11 '15 at 13:22
  • @Sanne You should add the top code only to the header.php file. I don't understand why would you include that code in your header-new.php file for example since it doesn't make sense to go further. The filters would be called again and you would try to re-include the same file. It won't work because you would end up with an infinite recursion if WordPress wouldn't have a default "true" for require_once when loading a template. This means you can't include the same header again (through the get_header function). – webtoure Aug 11 '15 at 13:37
  • Yes exactly, thats how I though it works. But can you explain what you are doing exactly with if ( apply_filters( 'load_custom_header', false ) ) ? – Sanne Aug 11 '15 at 13:48
  • You need to understand how actions and filters work. Filters manipulate and then return a value. In this case I am returning the value true through the __return_true WordPress defined function. That value is then evaluated by the if statement. Notice that I use false as the default filter value meaning that the if statement would evaluate to false and execution of the corresponding block would be stopped. – webtoure Aug 11 '15 at 14:42

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.