Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a child theme for the first time and I had a few questions regarding code added to header.

In a non child theme there is certain code I add to my header.php file such as google analytics, google webmaster tools, buy sell ads, Facebook open graph, etc....

How do you do this in a child theme? Do you create a header.php file in your child theme? If so how is this done? Is it the same as the @import as I used on the css?

Thanks.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

I would hook into the wp_head action. I would place this in a plugin so as to abstract it from your presentation layer. This allows for scalability and changing of themes. This also prevents any analytics collateral damage if a step is missed in migration from one theme to the next.

add_action('wp_head', 'wpse_43672_wp_head');
function wpse_43672_wp_head(){
    //Close PHP tags 
    ?>
    ADD YOUR PLAIN HTML CODE HERE
    <?php //Open PHP tags
}

Grab the plugin format here.

share|improve this answer
    
Thanks. I don't have any experience in making a plug in. I have about 5 pieces of code I'd like to add into the header. Would I need to make a plug in for each of them? –  Rick Smith Feb 27 '12 at 16:26
    
@RickSmith I added the plugin format to the post above –  Brian Fegter Feb 27 '12 at 16:35
2  
Whether using a Parent Theme, Child Theme, or any Theme whatsoever, the correct approach is to put such code in a callback, hooked into the appropriate action hook. You could put this code in the Theme's functions.php file, or, even better and as Brian has suggested, put it in a site-specific Plugin. –  Chip Bennett Feb 27 '12 at 17:33
1  
@BrianFegter Thanks for taking the time to help me with this. Here is a paste bin, I think I'm still doing something wrong. :) pastebin.com/iT0bJjGE –  Rick Smith Feb 27 '12 at 20:34
1  
let us continue this discussion in chat –  Brian Fegter Feb 27 '12 at 20:40

To modify the header in a child theme, copy the header.php from the parent theme into the child theme and then modify it. WordPress will see that you have a header.php in your child theme and use that instead of the parent theme header.php

Any template files you put in your child theme will take priority over the same file in the parent theme when called by WordPress.

Anything that goes in the tag should be done using something such as the function in Brians answer. If it's theme specific, you can put it in a file called functions.php in your theme folder without any extra steps.

share|improve this answer
    
Tom, thanks. So if I need to install 5 different snips of code in the header.php, could I just make one plugin and install that? Would I still need to copy the header.php file into my child theme? –  Rick Smith Feb 27 '12 at 17:58
    
Yes there is no reason to make 5 plugins –  Tom J Nowell Feb 27 '12 at 17:59
    
@RickSmith If you are abstracting to a plugin, there's no reason to copy over header.php. :) –  Brian Fegter Feb 27 '12 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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