Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Using wp_head to hook into a specific part of the header?

This is my first plugin. I'm trying to add a tagline underneath the logo.

I'm using this code in the plugin:

add_action('wp_head', 'tagline');

function tagline() {
    //Close PHP Tags
    ?>
    <h3>Outdoor Living and General Construction</h3>
    <?php
}

It works but it places it in a random place and I would like it to be outputted here, underneath the logo:

<header id="main-header" class="et_nav_text_color_dark">
        <div class="container clearfix">
                        <a href="#">
                <img src="http://#.com/wp-content/uploads/2014/12/Logo2.png" alt="Logo" id="logo" />
            </a><h3>Outdoor Living and General Construction</h3>

When I add in the div or header where I would like it to go though, it breaks the site.

Any suggestions on the correct way to go about this?

1 Answer

Yojance Rabelo
Yojance Rabelo
1,156 Points

You can't do it that way since wp_head runs in the <head> of the HTML document, not within the <body>

What you can do is add an action or filter in your theme where you want to output the HTML and use it, for example"

do_action( 'header_custom_code' );

Then in your functions file you would do something like:

add_action( 'header_custom_code', 'custom_html_code' );
function custom_html_code() {
    echo '<h3>Outdoor Living and General Construction</h3>';
}

Ok that's cool, I had a suspicion that was the case but I was wondering if there was a similar method like "innerHTML" in JavaScript.

Now, if you have time, that begs another question. Because the whole point of doing this was to not modify theme files. I want to do this is in a way that it is "update-proof". Any ideas on how you might go about that?