How to add code to the header in WordPress

- by

WordPress has a hook that lets us add arbitrary HTML code to the <header> tag of a website. Several plugins can be found that accomplish this, but it’s so easy to do that a plugin is often overkill.

Here’s how you can execute an arbitrary PHP function using the wp_head hook:

// write some text as part of the header 
function writeSomeText () { 
    echo 'Hello from my new header function.'; 
add_action('wp_head', 'writeSomeText');

This example inserts some text into the <header> portion of the website. HTML tags can simply be written using echo, including double quotes.

1 thought on “How to add code to the header in WordPress”

Add your voice!

This site uses Akismet to reduce spam. Learn how your comment data is processed.