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 need to add some html into the header.php but instead of directly changing the header.php of my theme or creating a child theme, how can I do it with just actions and/or hooks?

share|improve this question
    
Do you want to change the part inside of <head></head> or something below? –  toscho Nov 25 '12 at 21:36
    
@toscho no, the <header> inside the <body> tag. –  laggingreflex Nov 25 '12 at 21:45
add comment

3 Answers

It all depends upon the theme, what you want to change, and if WordPress and/or the theme provide filters and hooks to accomplish it. A child theme would most likely be your best (and safe) bet.

A simple child theme is very easy to setup and code. Take a look at he CODEX docs here.

share|improve this answer
    
So as I understand it, a child theme's header.php would need to override parent's with whatever I want it to, right? For that I'll need to copy parent's header.php and add to that. But then any updates to the parent theme wouldn't apply, right? So isn't there anything more .. permanent? –  laggingreflex Nov 25 '12 at 21:51
    
A child theme is the safe way to modify/customize a theme. The updates to the parent would still inherit the changes your child theme applies. So it's about as permanent as you can get. –  Stephen Nov 25 '12 at 21:53
    
I just tested and it appears that child theme's header.php completely overrides the parent's. As in it doesn't care what's in its parent's header.php. Doesn't that mean that any updates to parent's header.php wouldn't matter unless they are manually copied to the child? –  laggingreflex Nov 25 '12 at 22:17
    
Correct your child's header.php will take priority, so updates that are specific the parent's header.php will not be picked up by your child theme. However you will pick up all of the other updates to the theme. –  Stephen Nov 25 '12 at 22:19
add comment

If you are looking to insert code into the <head></head> block, then this hook does it:

add_action( 'wp_head', 'wpse_73900_wp_head' );

function wpse_73900_wp_head()
{
    ?>
    <!-- Custom Html --->
    <?php
}

If that's not the case, then it's more difficult. I think it would depend on the theme providing a hook at your desired point of insertion.

Another option is to make DOM manipulations with jQuery.

Other quite crazy possibilities can be extracted from this Q&A: Adding onload to body

share|improve this answer
add comment

You have two options:

  • If you're wanting to put things in the head tags, use the wp_head action.
  • If you're wanting to add markup either override header.php or add actions/filters of your own creation.

e.g.

say in header.php I have a large blue square containing a message, and I want to eb able to insert stuff afterwards via a hook, I can do:

<div id="bigbluesquare">
    <div id="bigsquaremessage">
        <p>message</p>
    </div>
    <?php do_action('after_big_blue_box_message'); ?>
</div>

I can now put in functions.php something like this:

add_action('after_big_blue_box_message','hello_world');

function hello_world(){
    echo '<p>hello world</p>';
}

And hello world will be displayed after the big square message

Of course you will need to add the do_action calls in the appropriate places

share|improve this answer
add comment

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.