You can easily put your Google Adsense code within the <head> element of your WordPress site by using either of these methods in your functions.php file or by creating a plugin for that purpose:
/**
* Load my Google Adsense code.
*
* Here, your Google Adsense Code is stored within in your plugins directory, under the
* the file named: gads.js, which contains your Google Adsense Code.
* The full file path for the purpose below: wp-content/plugins/my-plugin-name/inc/gads.js
*/
function load_my_google_adsense_code() {
wp_enqueue_script( 'gads', plugins_url('/inc/gads.js', __FILE__), null, '', true );
}
add_action( 'wp_enqueue_scripts', 'load_my_google_adsense_code' );
Alternatively, you can include it directly as part of your function as below;
/**
* Load my Google Adsense code.
*
* Here, your Google Adsense Code is contained directly in your function.
*/
function load_my_google_adsense_code() {
?>
<script>
// Your Google Adsense Code here.
</script>
<?php
}
add_action('wp_head', 'load_my_google_adsense_code');
Please note:
The two (2) approaches above will enable you to have your Google Adsense code within the <head> element of your WordPress site, usually right before </head>, but this is really theme dependent as it depends on the placement of the wp_head() hook within the header.php.
Its generally recommended to have the <?php wp_head(); ?> declaration before the </head> which explains the point above, purposely to avoid potential conflicts with plugins among others and implemented as below:
<?php
/**
* The header for our theme
* File name: header.php
*/
?><!DOCTYPE html>
<html>
<head>
<!-- Other contents here -->
<?php wp_head(); ?>
</head>
<!-- Other contents may follow -->
As such, the options provided above will work as Google Adsense requires, per your OP, should your the wp_head() hook of your theme satisfy that condition: occurring right below your <head> element.
Should that not be the case and you still need to achieve this as Google Adsense requires, you will need to directly edit your header.php file for that purpose, either:
1- Putting the wp_head() hook right after your <head> element as in:
<?php
/**
* The header for our theme
* File name: header.php
*/
?<!DOCTYPE html>
<html>
<head>
<?php wp_head(); ?>
<!-- Other contents here -->
In the above example, you will have to use either of the earlier two enumerated approaches above so as to leverage on this edit in order to achieve your goal.
or
2- Pasting your code directly in your header.php file, right after your <head> element as in:
<?php
/**
* The header for our theme
* File name: header.php
*/
?<!DOCTYPE html>
<html>
<head>
<!-- Your Google Adsense code here, right after the <head> element -->
<!-- Other contents here -->
Always remember to back up your files before doing such edits.