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.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

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

Leave a Comment!

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