Google AdSense code in php file

Sometimes it is confusing to add google adsense code in website based on php. This article explains the steps to add Google AdSense code in php file.

AdSense code in php file

AdSense code in php file

Get and copy the ad code

To generate your AdSense ad code you first need to create an ad unit. You do this on the My ads page in your AdSense account. There are a few different types of ad unit that you can choose from. Once you’ve decided which type of ad unit you want to create, follow the steps below to get and copy your ad code.

Ad unit:

A set of Google ads displayed as a result of one piece of AdSense ad code.
Types of ad unit
AdSense offers several different types of ad unit. If a standard ad unit doesn’t meet the requirements of your site, consider using one of our more specialized ad units:

TypeDescription
Ad unit- Standard ad unit
- Supports all of our standard ad sizes.
Custom-sized ad unit- Non-standard size ad unit
- Allows you to specify the width and height of the ad that you want to appear on your page. Learn more about custom-sized ad units.
Responsive ad unit- Adaptable ad unit
- Allows you to support a wide range of devices (i.e., computers, phones, tablets, etc.) by automatically adapting its size to fit your page layout. Learn more about responsive ad units.
Matched content unit- Block of content recommendations
- Generates contextually relevant and personalized article recommendations from the pages on your website. Learn more about the Matched content feature.
How to get and copy your ad code
Select an option below and follow the instructions to get and copy your ad code:
  • Sign in to your AdSense account.
  • Visit the My ads tab.
  • In the sidebar, make sure Content is selected.
  • Find your ad unit in the list of ad units, and click Get code.
  • In the “Ad code” box that opens:
    1. Leave the code type set to “Asynchronous” (the default), unless you want to use synchronous ad code. Learn more about asynchronous ad code.
      If you’ve chosen to create a responsive ad unit, you won’t see the Code type drop-down in the “Ad code” box. Responsive ad units only use asynchronous ad code.
    2. Select the ad code by clicking it. You’ll see the ad code highlighted in blue.
      Example of the "Ad code" box
    3. Copy all of the ad code, e.g., press Ctrl+C (Windows) or ⌘+C (Mac).

Adding AdSense Code in header.php file

  1. Locate the header.php file in your website hosting
  2. Edit the header.php file
  3. Paste the ad code between<?php wp_head(); ?> ………..……..</head>
  4. Save the header.php file and DONE!!