Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hooking into get_header to replace header.php in certain files?


  • madyogi
    Participant

    @madyogi

    So, I’ve been digging around in the bp parent theme, and I realize that all I really need to do to fully customize buddypress for my project is replace the standard header.php and footer.php with customized templates, then style what’s in between to my liking.

    What I’m needing to know is what you all suggest as the best way to accomplish this. I figure pulling all the files that call get_header and get_footer from the parent bp theme into my child theme and changing them individually is extremely inefficient, yes?

    With this in mind, I am thinking I ought to be able to write some code in my child theme’s functions.php file that hooks into the standard get_header and get_footer hooks (which are inside all the buddypress parent files I need to amend), check if the file is of a certain type, then replace the header.php and footer.php files with the properly-customized header/footer file for that page.

    My questions:

    1) Is this a good strategy to move forward with, or is there a better one for replacing the header.php file atop every buddypress template file without actually changing my theme’s header.php file itself? For example, say I have a communityHeader.php file that is specific to all the pages involving the BP social networking functionality, including login and registration, plus there’s a communityFooter.php that goes with those pages as well, both of which are different from my main header.php/footer.php files.

    2) What would be the best method of querying each get_header hook to see if the template file asking for header.php is in fact a buddypress file or not? Is there something that easily identifies them as being different from other customized template files in my child theme? Is there perhaps a way of asking what directory the file calling get_header is in, or some other way of differentiating the various index.php files from each other?

    Again, I could obviously identify each file from the parent theme that needs changing, move it and its directory into my child theme, then add the whole <?php include (TEMPLATEPATH . ‘../INSERT_APPROPRIATE_DIRECTORY_STRUCTURE/communityHeader.php’); ?> line in place of every get_header call, but this seems terribly inefficient to me.

    Thanks so much in advance for any advice!

Viewing 7 replies - 1 through 7 (of 7 total)

  • Boris
    Participant

    @travel-junkie

    No, what you need to do is copy header.php and footer.php into your child theme and adjust them. Then, if you need different headers for each section do it with if statements (also in header.php and footer.php). You can find all the conditional tags in the BP codex.

    You could also code a little switcher that works with includes. That way you’d have the various headers in different files. It’d be easier to maintain.


    madyogi
    Participant

    @madyogi

    Okay, just looked at the conditional tags in the BP Codex. I could place a conditional statement in the header asking if various conditions are present, such as bp_is_directory(), or bp_is_user_profile() etc, then relevant code?

    I’m not sure how to,

    code a little switcher that works with includes.

    Is there someplace I can go to get the gist of this? Should I just search the WP codex for “includes”?

    Also, what attribute can I use to differentiate one file from another when using if statements?


    Boris
    Participant

    @travel-junkie

    have a look here: https://codex.buddypress.org/developer-docs/conditional-template-tags/

    there’s some examples as well.

    so you could do something like this:

    if( bp_is_blog_page() ) {
    include( 'blog-header.php' ); // include this file on blog pages
    } elseif( bp_is_directory() ) {
    include( 'dir-header.php' ); // include that file on directory pages
    } else {
    get_header(); // otherwise use our normal header
    }


    madyogi
    Participant

    @madyogi

    Thanks so much … this is making more and more sense. One question, though – what would be the best way to place such a condition into the template (while keeping as much of the parent template intact as possible and avoiding tedious file-by-file manipulation)?

    Could I, for example, place this sort of condition atop the normal header.php file, moving all the normal header HTML markup into the final else condition and removing the get_header(); call? Would this make sense:

    <?php if ( bp_is_user_profile() ) {
    include (TEMPLATEPATH . '/profileHeader.php'); //include this header on profile pages
    } elseif ( other_conditions() ) {
    include (TEMPLATEPATH . '/otherHeader.php'); //include this header on profile pages
    } else {
    include (TEMPLATEPATH . '/homeHeader.php'); //include normal header markup placed into homeHeader.php
    } : ?>

    Would that code work? Does it need an endif? Not sure if the closure of it is correct either, what with the colon and all. Regardless, once the syntax is corrected, it seems like an easy thing to maintain, since it would only involve changing the header.php, then creating whatever other headers I might need. Then every time wordpress looked at header.php it would decide which header markup to include. Is that a correct assessment?

    Is there some more efficient implementation of this type of conditional statement I’m not thinking of?

    Thanks again for all your help, Travel-junkie!


    Boris
    Participant

    @travel-junkie

    Hey,

    yep, you can do it that way.

    Just fyi, get_header() also accepts one variable, $name. So if you write this in a template:

    get_header('blog');

    then WP will look for a file called header-blog.php in your theme files and include that one instead your normal header.php file.

    Have a look at the get_header function. Maybe you’re lucky and there’s a filter applied to $name. Then you could just write a function that does it all for you and you won’t even have to touch the header.php file in your parents theme.


    madyogi
    Participant

    @madyogi

    Thanks once again for your help, Travel-Junkie! I’m looking forward to working on this more tomorrow. I’ll let you guys know how it goes!


    madyogi
    Participant

    @madyogi

    So, what I can’t figure out now is what precise conditions those conditional tags are checking. Specifically, the condition bp_is_user_profile seems to be true all the time. I am wanting to load in specific content only when my profile page is showing, but checking the bp_is_user_profile conditional tag always displays that content, even when the profile page is not being shown.

    Is there any documentation saying specifically what each of the conditional tags checks, ie what would yield a “true” return?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Hooking into get_header to replace header.php in certain files?’ is closed to new replies.
Skip to toolbar