i would like to to add an AdSense activation code to my Wordpress site.

Google says it needs to be inserted right AFTER the tag

I have tried the plugin Snippets, yet it seems not to be working.

The code is:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
(adsbygoogle = window.adsbygoogle || []).push(
{     google_ad_client: "ca-pub-5316020100281676",
enable_page_level_ads: true   }); 

Thanks in advance.

  • When you view the page source, where are you seeing the code getting inserted? On Chrome, right click > View Page Source
    – Jami Gibbs
    Jan 2, 2017 at 15:26
  • It does not actually show anywhere. I tried inserting it as a widget to a siderbar, yet it creates a new paragraph and messes up the graphic. The official Google Adsense plugin does not seem to work. Jan 2, 2017 at 16:15
  • I've suggested an answer on how to add this without using a plugin.
    – Jami Gibbs
    Jan 2, 2017 at 16:48

2 Answers 2


Without using a plugin, the WordPress way would be to use the wp_head action to insert the script and then enqueue the script file:

function mytextdomain_adsense() {
      (adsbygoogle = window.adsbygoogle || []).push({ 
        google_ad_client: 'ca-pub-5316020100281676',
        enable_page_level_ads: true   

    echo $output;

function mytextdomain_adense_script() {
    wp_enqueue_script( 'my-adsense', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', array(), '1.0.0', false );
add_action( 'wp_enqueue_scripts', 'mytextdomain_adense_script' );

And to add the async attribute to the script link:

function mytextdomain_add_async_attribute($tag, $handle) {
    if ( 'my-adsense' !== $handle ) {
      return $tag;
    return str_replace( ' src', ' async src', $tag );
add_filter('script_loader_tag', 'mytextdomain_add_async_attribute', 10, 2);

This should be placed in the theme's functions.php file (preferably a child theme so that it doesn't get overwritten if there's a theme update).

  • This will insert the code before the </head> and not after as required in the question. Not sure if there are any practical differences. The bigger problem is that the script code should depend on the inclusion of the library and come after it, which can be done with developer.wordpress.org/reference/functions/…. But the biggest problem is that with this code the library is not included as async resource, and that is a major problem Jan 2, 2017 at 18:44
  • Fair point about it loading before </head> but I don't see how this will affect the script's functionality. By enqueuing the link and loading it in the header (that's what the false bit is for), it will load in the correct order. As for async, also a good observation. I had actually missed that. I'll update my answer to account for it.
    – Jami Gibbs
    Jan 2, 2017 at 19:23
  • it will most likely end up in the right order due to enque having a high priority (1 IIRC), but probably better to have the dependency explicit (I think the order actually do not matter, which is why it can be async at all, but if there is a need to get support from some pedant guy he might say that the problem is with the order, or not having them one right after the other). Jan 2, 2017 at 19:30
  • Thank you for your answer. I don't know if it matter, but Google says it needs to be inserted right AFTER the head. I am having trouble with authorization, so it might be a problem. It is being inserted into the <head> + the code looks a little bit different than the one Google gave me. For example starting with this line <meta name="google-site-verification" content="CC8Q0egOv5QW8aIj-cSK4QZKuCJYWBu3f8xCu1_Ex9Y" /> Jan 3, 2017 at 12:43
  • I think we're talking about two different things. Are you trying to do a Google site verification or Adsense? There's no mention of adding a meta tag in your original question.
    – Jami Gibbs
    Jan 3, 2017 at 14:21

Maybe they changed their requirements? Google says it needs to be inserted BETWEEN the tags. See here:

Implementing code into WordPress page (Google manual (GER)

There are two ways to realize it:

  • Use a plugin to paste code into section.
  • Or do it manually: create copy of header.php and save it securely. Then open header.php in a text editor and paste AdSense code just BEFORE closing tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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