Insert Adsense Effectively to your WordPress site without plugin

Are you planning to monetize your WordPress site? After working so hard on your website, it would be great if the site generates revenue. The right time to apply monetization strategy is right now. So, hurry up.

Google AdSense is one of the most popular way to monetize your website. You can easily add Google Adsense to your WordPress site. However, getting Adsense approval is not easy. There are some requirements for approval.

In this article, I will show you how to effectively insert Adsense to your WordPress site. You will know how to place Adsense code in sidebar widget, within content, homepage and between posts. Furthermore, I will show you how to manage Adsense code for smaller screen like mobile phones.

Before I begin, I assume that you have already got approved for Adsense. You know how to create ads and generate Adsense code from your Google Adsense account.

Insert Adsense in sidebar widget

The easiest way to insert adsense code to your WordPress site is by using WordPress widget. All you have to do is log into your WordPress site.

Go to Appearance >> Widgets

Adding WordPress widget

Next drag and drop the “Text” widget to your desired Widget Area. You will be prompted with a Editor. If not, click the widget you have just created. You will see editor similar to the following screenshot.

WordPress Text Widget Demo
WordPress Text Widget

You can skip the title Field. In the editor, go to Text mode and insert your Adsense code and hit the Save button. Bingo! You have done it.

Now that you have inserted Google code in your Widget, you can control the Visibility of your Widget to appear / not appear in certain posts, homepage, pages, etc by clicking the Visibility button.

Insert Adsense within Post

In this section, I will show you how to add Adsense inside content using shortcode. This is the best slot for revenue.

You can create shortcode in WordPress by simply using this syntax.

<?php add_shortcode( $tag , $func ); ?>

All you have to do is create a function that returns Adsense code and then use above syntax to create a shortcode. Then you can use this shortcode wherever required in your  Post.

In order to create shortcode, locate the file functions.php in your theme by going through Appearance >> Editor.

Here’s the snippet to create shortcode.

// functions.php

function getAdsenseCodeForPost() {
  return '
    <center>
    <ins class="adsbygoogle"
       style="display:inline-block;width:336px;height:280px"
       data-ad-client="ca-pub-xxxxxxxxxxx"
       data-ad-slot="xxxxxxx"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    </center>';
}

add_shortcode('insertAdsense', 'getAdsenseCodeForPost');

Replace Adsense code in the above function getAdsenseCodeForPost with your own code.

Now that you have created shortcode, you can use this shortcode within your post by simply using this shortcode surrounded by square brackets.

Here’s the screenshot how it looks like.

insert adsense using short code
Inserting Adsense using shortcode within Post

Insert Adsense in Homepage

In order to add Google Adsense to your home page, go to Appearance >> Editor and select the appropriate home page template. The template file is either front-page.php, home.php or index.php depending upon your theme or setup. Once you locate the template, you can insert your Adsense code to your desired location.

Editing WordPress Home Template

Similarly, you can add Adsense code by editing respective template file for category, tags and so on.

Inserting Adsense between posts

By default, WordPress sets your site’s home page to display your latest blog posts. You can insert Adsense between those posts so that you maximize your chance to increase revenue.

In order to do so, you need to locate the template file which is generally home.php or index.php. The snippet that generates the blog list is as follows:

<?php 

  if (have_posts()) : 
  
    while (have_posts()) : the_post();

    get_template_part( 'content', $theme_options['posts_length'] );

    endwhile;

  endif; 
?>

You can insert Google Ad after certain number of posts. Here’s the step.

  1. Add a variable $post_counter = 1 before this snippet
  2. Increment $post_counter  within the while loop.
  3. Check if ($post_counter % no_of_post == 0) to insert Adsense code.

The code will be similar as follows:

<?php 

  if (have_posts()) : 
  
    $post_counter = 1;
    
    while (have_posts()) : the_post();

      get_template_part( 'content', $theme_options['posts_length'] );
    
      if($post_counter % 3 == 0) {
?>

      Place Your Adsense Code 
<?php		
    
      }
    
      $post_counter++;

    endwhile;

  endif; 
?>

In this way, you can insert Adsense Code between posts.

 

Optimizing Google Adsense for Mobile

If you have lots of visitors coming from small devices like mobile phone, it is good idea to switch Google Adsense Ad unit for Mobile view. Some of the Ad units like Leaderboard (728 x 90) is not suitable  while browsing through mobile. For such case, you can switch with Resposive or Large Mobile Banner Ad unit.

In that case, you can use WordPress function wp_is_mobile(). This function can detect whether visitor is viewing through mobile or large devices. Here’s how you can do.

<?php if( wp_is_mobile() ): ?>

// Adsense code for mobile version

<?php else: ?>

// Adsense code for normal version

<?php endif; ?>

 

In this way, we can integrate Google Adsense to our WordPress site without using any plugin.

 

Leave a Reply