Adding code to your theme header with functions.php

Sometimes you simply want to add a line or two in your themes header.php without actually modifying it. Modifying it directly is seldom advisable. Even in a child theme you need to keep track of changes you made in case the parents header.php changes too much – after an update for example.

The solution is simple enough as you can inject code into the <head> area via the functions.php file of your (child) theme by simply using the wp_head hook.

<?php
add_action('wp_head', 'inject_wp_head');
function inject_wp_head(){
 ?> 
 <meta name="theme-color" content="#db5945">
 <?php 
}
?>

I am using it here to inject a meta tag with a theme color which some browsers support and tint the tabs / browser windows in this color.

Yes, there are plugins that can add code to your footer / header etc. but I like to keep things as simple as possible as more plugins require more maintenance in the long run and every extra plugin is usually detrimental for performance and security.