0

I have a multisite WordPress project with a main site using Theme A and all 'sub'-sites using Theme B.

Both Theme A and Theme B are Child Themes.

I want to import the header file from Theme A into Theme B, so I can show that header before Theme B's header - how would I implement this header properly?

I need a way that ensures the styling and functions (bloginfo() etc) of Theme A are maintained.

1

As you have now stated that both Theme A and Theme B are already Child Themes, perhaps you could try adding this to your funcions.php file in Theme B -

add_action('wp_enqueue_scripts', 'enqueue_front_scripts');
function enqueue_front_scripts(){

    /** Switch to the parent blog */
    switch_to_blog(1);  // You may need to change the ID, I don't know what ID you main site has
    
    /** Grab the path of the parents 'header.php' file */
    $main_header_style_path =  get_stylesheet_directory_uri() . '/header.css';

    /** Restore the current blog */
    restore_current_blog();
    
    /** Enqueue the main header styling */
    wp_enqueue_style('main-header', $main_header_style_path);
    
}

function get_main_header(){

    /** Switch to the parent blog */
    switch_to_blog(1);  // You may need to change the ID, I don't know what ID you main site has

    /** Grab the path of the parents 'header.php' file */
    $main_header_path =  get_stylesheet_directory_uri() . '/header.php';

    /** Output the main header */
    require_once($main_header_path);

    /** Restore the current blog */
    restore_current_blog(); // Don't restore until after you have included the header, otherwise you 'get_blogino()', etc. calls will reference Theme B
    
}

And then to include both headers you'd do this (note that the header file in both themes would simply be called header.php) -

get_main_header();
get_header();

I suggest that you have a read of the Function Reference for switch_to_blog() for more information.

Update

I forgot to mention that you would also still need to seperate your header styling in to it's own header.css file in Theme A, and then enqueue this in both Theme A and Theme B.

I have update my code example about to reflect this.

4
  • Thanks, great idea - so also create a header.css file in Theme A and require that? I will look more into the switch_to_blog function. But it might be a heavy function to execute?
    – Mac Luc
    Feb 16 '15 at 10:32
  • Yes, you would need to do that - I'll update the question shortly.
    – David Gard
    Feb 16 '15 at 10:33
  • Appreciated @DavidGard. Is there need to setup 2 functions - i cant grab both paths within the same switch_to_blog function?
    – Mac Luc
    Feb 16 '15 at 10:45
  • The proper way to enqueue scripts/styles within WP is to use the wp_enqueue_scripts action hook, as above. The reason for this is so that scripts/styles are included in your header, as opposed to the body of the page, and are only included once. Doing it this way also means that you can enqueue header.css properly in Theme A. Yes, it's a couple of extra lines, but it's honestly worth it in the long run. Have a read of the Action Reference for wp_enqueue_scripts for more information.
    – David Gard
    Feb 16 '15 at 11:11
0

You could make Theme B a Child Theme of Theme A, and then create header-b.php in Theme B (as opposed to header.php), then just include that after your call to

get_header();
require_once("header-b.php")

To ensure that all of your styling is carried over from the parent, include this in your functions.php file of Theme B -

add_action('wp_enqueue_scripts', 'my_front_scripts');
function my_front_scripts(){

    wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');

}

If it really is only your header style that you wish to use, it may even be worth creating that in it's own CSS file (header.css) and enqueuing that - obviously you'd need to also enqueue that in Theme A.

5
  • Nice input, but the problem I guess, is that both Theme A and B are child themes of other themes :-)
    – Mac Luc
    Feb 16 '15 at 10:03
  • Oh. It would have been nice if you had added that in your question.
    – David Gard
    Feb 16 '15 at 10:11
  • My bad - I've added that to the question now.
    – Mac Luc
    Feb 16 '15 at 10:16
  • 1
    @MacLuc this is not your first question. Please do post all relevant info that might influence the result and correctness of an answer. I have previously answered two questions just to later find out there is missing info in your question. It is really really frustating wasting one's time answering a question and then getting told that the question lacked information. Sit and reread your question from another guys perspective that does not know your setup. To be honest, users tend to just ignore future questions from the specific user. :-) Feb 16 '15 at 10:29
  • In this case I didnt realize that this had relevans for a solution suggestion. But you are of course right!
    – Mac Luc
    Feb 16 '15 at 10:35

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.