Post Pic

How to: Embed Adsense anywhere on your posts

Do you ever wanted to get a total control over the way your adsenses ads are displayed? In the following recipe, I’m going to teach you how to be able to use adsense only in the post you want and display the ads where you want.

First, you have to add the following code to your function.php file. Don't forget to change the adsense code, unless you'd like to display my ads on your own site ;)

function showads() {
    return '<script type="text/javascript"><!--
google_ad_client = "pub-3637220125174754";
google_ad_slot = "4668915978";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';
}

add_shortcode('adsense', 'showads');

Once you saved the functions.php file, you're now able to embed your adsense code on your posts and display it exactly where you want. To do so, simply paste the following code on the editor, in html mode:

[adsense]

8 Responses

Apr 17 2012 16:30

Great share !

Jun 18 2012 16:05

please i don’t know how to locate the end of the php in my function.php file.

Jun 20 2012 20:23

How do i put it side by side with content?

Jul 16 2012 15:42

Nice post dude. Till now am using plugin for this but this tiny code is working great. Keep up

Aug 17 2012 14:04

nice work, but i have some questions :-
1) How to align the ads to center ?
2) How to use multiple ad sizes ?

Hope you ll answer these ques soon

Nov 25 2012 01:58

@Ramandeep Singh

1) To align the ads to center use this code:

function showads() {
return ‘ADSENS CODE

‘;
}

add_shortcode(‘adsense’, ‘showads’);

2.) multiple ad sizes exemple 1

function showads() {
return ‘YOUR ADSENSE CODE

‘;
}

add_shortcode(‘adsense728x90′, ‘showads’);

And paste the following code on the editor, in html mode:

[adsense728x90]

multiple ad sizes exemple 2

function showads() {
return ‘YOUR ADSENSE CODE

‘;
}

add_shortcode(‘adsense468x60′, ‘showads’);

And paste the following code on the editor, in html mode:

[adsense468x60]

Jan 24 2013 19:13

Make sure your functions.php file isn’t capitalized! (Functions.php) Otherwise you’ll drive yourself crazy and your shortcode won’t work!

May 11 2013 04:10

This works great. Now how to text wrap it?

thanks

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox