PGO
← News

PGO / ARTICLE

How to Add AdSense Code to WordPress (Three Safe Methods)

How to add AdSense code to WordPress with Site Kit, your theme header or a plugin, plus how to confirm the code works and fix ads that will not show.

Navy and gold graphic reading Add AdSense Code to WordPress, with the subtitle Site Kit, header code or a plugin

To add AdSense code to WordPress, you either let Google’s Site Kit plugin place it for you, paste the code between the <head> and </head> tags of your theme, or use a header and footer plugin that does the pasting for you. All three work. The right one depends on how comfortable you are touching code and how many plugins you already run.

This is one of the first technical jobs after you sign up for AdSense, and it is where a lot of new bloggers get stuck. They paste the code in the wrong place, a caching plugin hides it, or they put it on one page and wonder why Google says it cannot find the site. Below I walk through each method step by step, then show you how to confirm the code is really live.

What the AdSense code actually is

When you add a site in your AdSense account, Google gives you a short script that carries your publisher ID (it starts with ca-pub- followed by numbers). That script does two jobs. First, it lets AdSense connect to your site during the review. Second, once you are approved, it is the same code that loads Auto ads on your pages.

Google’s own instruction is plain: paste the code between the <head> and </head> tags of your site, and place it on every page where you want ads to appear. It looks like this, with your own ID in place of the sample one:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX" crossorigin="anonymous"></script>

Always copy the code from your own AdSense account. Never copy it from a tutorial, because the publisher ID in a sample belongs to nobody, or worse, to someone else.

There is also a second kind of code: ad unit code. That is the snippet you get when you create a specific display, in-article, in-feed or multiplex unit under Ads, then By ad unit. Ad unit code goes inside the <body> where you want that particular ad to show. If you are still deciding between the two approaches, read my breakdown of AdSense Auto ads vs ad units first.

Method 1: Add AdSense code to WordPress with Site Kit

This is the method Google itself recommends for WordPress users. Site Kit by Google is a free, open source plugin that connects your site to AdSense, Analytics, Search Console and PageSpeed Insights, and it can place the AdSense code on all your pages for you.

Steps

  1. In your WordPress dashboard, go to Plugins, then Add New.
  2. Search for Site Kit by Google, click Install Now, then Activate.
  3. Click Start Setup and sign in with the Google account you use for AdSense. Follow the prompts.
  4. When setup is done, find Connect Service under AdSense on the Site Kit dashboard or in its Settings tab.
  5. Follow the steps and allow Site Kit to place the AdSense code. Google says this places the code on both the normal and the AMP versions of your site.
  6. In your AdSense account, make sure Auto ads is switched on for the site. Site Kit cannot change your AdSense settings for you, so this part is on you.

When it is working, the AdSense section in Site Kit settings should say the code has been placed. If you run a lot of plugins already, this method still makes sense, because Site Kit replaces several separate tools at once.

Method 2: Paste the code into your theme header

If you are comfortable with a little code and you do not want another plugin, you can paste the script directly into your theme’s header file. This is the old school way, and it works.

Steps

  1. Copy your AdSense code from your account (Sites, then your site, then the code snippet).
  2. Use a child theme. If you edit the parent theme directly, the next theme update will wipe your code and your ads will quietly disappear.
  3. Open header.php in your child theme through your hosting file manager or SFTP. I prefer this over the built in theme editor, because one wrong save in the editor can lock you out of the dashboard.
  4. Paste the code just before the closing </head> tag. Save.
  5. Clear your caching plugin and any server or CDN cache, then load your site.

Note that newer block themes do not use a classic header.php file the same way. On a block theme, Method 1 or Method 3 is the easier route.

Take a backup before you edit any theme file. At Trispark Hosting we run daily backups and 24/7 support, so a bad edit is not the end of the world, but a copy of your own is still the safest habit. If you have never opened a hosting file manager, my guide on what cPanel is shows you around.

Method 3: Use a header and footer plugin

This is the middle ground. Google’s help page lists a few partner plugins people already use for this job, including Insert Headers and Footers, Advanced Ads and Ad Inserter.

Steps with a header and footer plugin

  1. Install and activate the plugin from Plugins, then Add New.
  2. Open its settings page. Most of these plugins have a box labelled Header or Scripts in Header.
  3. Paste your AdSense code into the header box and save.
  4. Clear your cache and check the site.

The advantage over Method 2 is that the code survives theme changes. The advantage of ad plugins like Advanced Ads or Ad Inserter is that they also let you place individual ad units in specific spots, such as after the second paragraph, without editing templates. Google’s own note on plugins is worth repeating: test a few, make sure the plugin works with your theme, and contact the plugin developer if something breaks, because Google is not responsible for non Google plugins.

How to check that your AdSense code is live

Do not trust a plugin message alone. Check the page the way Google will see it.

  1. Open your homepage in a private browser window so you are not logged in.
  2. Right click and choose View page source (in Chrome, Ctrl + U also works).
  3. Press Ctrl + F and search for your publisher ID, the part that starts with ca-pub-.
  4. Confirm it sits inside the head section and appears exactly as it does in your AdSense account.
  5. Repeat on one post page and one category page.

Google lists the common problems you should look for: code that is not complete (a missing <script> or <ins> tag), code squashed onto one line, or extra HTML tags inside it. If you see any of those, replace it with fresh code copied straight from your account.

Why AdSense says it cannot find your code

Based on what I have seen with new bloggers, the code itself is rarely broken. Something around it is. Google’s connection checklist covers the usual suspects:

  • Code on the wrong site. You pasted it on a different domain from the one you added in AdSense, or you added www when the site runs without it.
  • Code on a page nobody visits. Google notes that long waits for activation usually happen when the code is not on a page that gets regular views. Put it on every page, homepage included.
  • The site is behind a login or a coming soon page. The AdSense crawler needs to reach your site without a password.
  • robots.txt blocks the crawler. If you have been experimenting with robots.txt, check you are not blocking Google’s AdSense crawler. My robots.txt guide shows you how to read the file.
  • Cache. A caching plugin can keep serving an old copy of your pages without the code. Clear every layer of cache after you add it.

If you would rather not touch the head at all, AdSense also lets you verify ownership with an ads.txt snippet or a meta tag. You still need the main code for Auto ads later, but ads.txt is something you should set up anyway. Here is how to fix the ads.txt not found warning.

Common mistakes to avoid

  • Pasting the code twice. If you use Site Kit and a header plugin at the same time, the script can load twice. Pick one method.
  • Editing the parent theme. Updates erase it. Use a child theme or a plugin.
  • Clicking your own ads to test them. Never do this. Use the page source check above. Invalid clicks are one of the fastest ways to get into trouble, as I explain in mistakes that get AdSense accounts banned.
  • Adding code before the site is ready. The code connects you, but approval depends on your content and pages. Make sure you have done the work in my guide on how to get AdSense approval on a new blog, including a proper privacy policy.

After the code is in: request the review

Once the code is live, go back to AdSense, tick the box to confirm you have placed it, click Verify, then Request review. Google says the review usually takes a few days, but it can take two to four weeks. Add your payment details while you wait so nothing holds you up later. If it comes back rejected, my guide to AdSense rejection emails explains what each message means and what to fix.

There is also an option to show ads as soon as your account is activated. If you get visitors from the European Economic Area, the UK or Switzerland, Google suggests you think about its EU user consent policy before you tick that box, and set up a consent message (Google offers its own CMP in AdSense under Privacy and messaging).

FAQ

Where exactly do I paste the AdSense code in WordPress?

Between the <head> and </head> tags, on every page. Site Kit or a header plugin does this for you. If you edit by hand, put it just before </head> in your child theme’s header.php.

Do I need a plugin to add AdSense to WordPress?

No. You can paste the code into your theme by hand. A plugin simply makes it safer, because the code survives theme updates.

How long after adding the code do ads show?

Your site has to be reviewed and approved first. For ad units on an approved site, Google says ads usually appear within minutes, and occasionally take up to an hour.

Can I add AdSense code to a site on free hosting?

Many free website builders do not let you edit the head or install plugins, which makes this hard. That is one of the reasons I compare free hosting vs paid hosting before anyone starts a monetised blog.

Is ad unit code the same as the AdSense code?

No. The AdSense code goes in the head and powers Auto ads and verification. Ad unit code goes in the body where you want one specific ad.

If you are setting up a new WordPress blog and want hosting where you can do all of this yourself, see Trispark Hosting. And if you want someone to walk you through AdSense properly, that is what we teach at Trispark Academy.

Share

Share this article.