1

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?

2
  • Do you want to change the part inside of <head></head> or something below?
    – fuxia
    Nov 25, 2012 at 21:36
  • 1
    @toscho no, the <header> inside the <body> tag. Nov 25, 2012 at 21:45

3 Answers 3

1

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

1

You have three 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.
  • Insert it using javascript

Adding a header action

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

Adding wp_head

This will insert code into the <head> tags, and works the same as the above example.

Inserting With Javascript

This might work, but will assume you know the ID and classes

How Do I insert header content That Will Work With Any Theme?

You can't.

There is no standard hook that hooks into the header area. It wouldn't make sense on a lot of themes as header layouts vary wildly. Some themes don't have headers.

0

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.

4
  • 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? Nov 25, 2012 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.
    – Steve
    Nov 25, 2012 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? Nov 25, 2012 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.
    – Steve
    Nov 25, 2012 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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