WordPress: Add code to header via functions.php

Do you want to add any code such as Google Analytics within head tag in your WordPress website or blog? You can easily add codes in the header of your WordPress website without editing the header.php file and by adding the same in the functions.php file of the theme.

Paste the code below in the functions.php file and add your HTML or code in the space provided. It will be added to the WordPress footer using the wp_footer function.

<?php
add_action('wp_head', 'header_code');
 
function header_code() { ?>
    // Paste your code here
<?php } ?>