Display Adsense Ads anywhere within WordPress post

Displaying Adsense Ads at the start or end of the post content is most preferred setup among majority of publishers. If you have a long article content, then you can experiment by embedding Google Adsense Ad (or other Advertisements) bang in the middle of the content. You can easily implement this by creating quick shortcode and display ads anywhere within post content area.

Create shortcode to display Adsense Ads within post

1. Paste the following code in functions.php file of your WordPress setup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function myads1() {
return '
<!-- Start Here -->

<script type="text/javascript">// <![CDATA[
google_ad_client = "pub-123abc"; /* 250x250, created 7/17/08 */ google_ad_slot = "8453380705"; google_ad_width = 250; google_ad_height = 250;
// ]]></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[


// ]]></script>

<!-- End Here -->
';
}

add_shortcode('ad1', 'myads1');

Once you paste the above code, make sure you replace the ad code with your own Google Adsense ad code as seen between “Start here” and “End here” tags.

2. Now to insert and display Ads within post, open specific post in the WordPress dashboard. Click on HTML mode button in the editor and then paste [ad1] within post body where you want to show the advertisement.

You can also display multiple adsense ad code blocks using above method by changing function name. For example: for second ad code, just add another set of code with different functions name like use 2 in place of 1 for myads1 and ad1. Besides Adsense, you can also use above method for inserting any other type of ad code.

How to style & position displayed advertisement

1. While adding shortcode [ad1] in the post editor, wrap it with specific class for styling as seen in code below:

1
<div class="adstyle">[ad1]</div>

2. Now add adstyle class code to your style.css file. You can begin by adding following sample code. You can modify it further as per your styling requirements.

1
2
3
4
5
6
7
8
.adstyle {
background:#f4f4f4;
width:400px;
height:500px;
text-align:right;
float:right;
border:solid 1px #c7c7c7;
}

Above is handy shortcode method to insert any type of advertisment code or banner ad within your post content. Depending on the structure of your post content, you can fit different ad banners at various places within post content for better user response and engagement with ads.

SHARE

Comments

  1. Man, this is AWESOME! Working, simple, elegant! Thanks a lot for this, cheers!

Speak Your Mind

*

Subscribe to our mailing list

* indicates required
Close