0

Google Adsense requires its activation code to be pasted right after the <head> tag of a site.

However, I'm using a WordPress child theme.

How can I paste my Google Adsense code in a child theme?

2

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.

0
-1

Use wp_head hook in functions.php

add_action('wp_head','add_adsense');
1
  • 1
    this might be the correct answer, but it is very hard to understand how exactly to use it (for example what is add_adsense). Please edit it and add all the parts required to actually make it work or at least to understand how to make it work. – Mark Kaplun Mar 4 '17 at 5:52
-1
add_action( 'wp_head', 'add_google_adsense_code' );
function add_google_adsense_code() {
?><script>
// Your Google Adsense Code here.
</script><?php        
}

See action hooks

Added to your child themes functions file, this hook fires after wp_head therefore printing your script after the closing </head> tag.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.