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]

52 Responses

Dec 19 2008 09:14

Could you also post a recipe for not showing ads on specific wordpress pages…

Cheers
Vaibhav

Dec 19 2008 09:19

@Vaibhav: This solution can work with pages as well :)
But if you’d like to know how to prevent displaying ads on specific pages, I’ll write a recipe for sure!

Dec 19 2008 18:23

Great tip! I’ve just been learning the shortcode API for use in my plugins, but I never thought to use them anywhere else… thanks!

Dec 19 2008 19:04

This is so AWESOME! What a handy dandy shortcut! It makes your post SO much cleaner as well!!! THANK YOU!!!

Dec 19 2008 21:46

Thanks for the recipe! I had some problems incorporating it into my theme. I edited my single.php to directly call the function. For some reason the function would not return any output so I had to change the “return” part of your code to “echo”. Once I did this, it worked a treat. Thanks again!

Paul.

Dec 22 2008 13:23

Would you care to explain how to make the adsense float left or right so that the text would wrap around the adsense? Maybe also adding how to increase the margin between the adsense and the text (top, bottom, left and right margin)? Thanks

Dec 22 2008 13:36

@Agolf Cartson: Yes, I’ll write a recipe about enhancing this one soon.

Dec 22 2008 13:39

Thank you very much. That would really be great. Being able to add adsense using [adsense] would make things so much easier :)

Dec 22 2008 13:50

How about the styling?

Dec 22 2008 13:54

@levin: The same question has been already asked by Agolf, I’m going to write a recipe about it soon :)

Dec 23 2008 11:04

That’s pretty smart, but I don’t like to add ads manually and to lazy to make a script:P

Dec 26 2008 01:49

Just Perfect solutions ;)

Dec 26 2008 03:35

Thanks for the recipe! This tool is very nice and of course articles on this page are nice, too.

Dec 27 2008 08:51

What would be the case for existing posts that won’t have the [adsense] tag? Also, if you are to place multiple ads (channels) on a post do we need to add multiple add_shortcode()?

Dec 30 2008 18:46

Thanks for this recipe!

It’s a lot easier to do than I thought, and without using a plugin!

Jan 09 2009 16:12

Thanks good solutions

Feb 05 2009 23:11

really nice solution , thank you

Feb 06 2009 03:19

Does this work for anyone else? When I try it, it keeps giving me a fatal error. I can’t even get to a page in order to put the [adsense] piece into the post. It’s like the moment I put the functions.php file into the theme’s folder, it caused the issue.

Is the functions.php file supposed to look like:

Do I need to call it from anywhere?

Feb 18 2009 03:17

If I put, in single.php, the code below, will it work?

<?php adsense(); ?>

[]‘s!

Feb 18 2009 18:55

What can I do if i put that in all posts but i want that the block only shows in a single post?

(my english is sooo bad, i hope you understand! :P )

Feb 19 2009 17:07

Excellent tip. Just exactly what I need right now. Couldn’t be much easier, could it?

Apr 16 2009 17:44

This doesn’t seem to be working for me.
The result is the ads showing up on all pages (including admin pages) just prior to the header.php

I’m sure it has to do with the original theme the blog was built on, but any clues on what is going on so I can fix it?

Apr 28 2009 08:17

Oooh, awesome, works great.

How do I align the adsense code though? Right now, it’s pushing the text onto the next line instead of wrapping it neatly.

May 28 2009 22:54

Thx, this is a very smale way, thx for this solution.

Jun 07 2009 20:55

Very useful. Earlier I used to edit each template. Now I just need to edit the functions.php

Aug 06 2009 09:50

How to rotate the Ads inside the post, one like Adsense injection plugin???

Sep 04 2009 04:24

Thank you for your tip.I will be thank ful to you for it every time since it increased my earnings.
Thanking you agian.

Oct 05 2009 11:43

Great and very simple piece of code. I’ve just added the code to my website.

Feb 03 2010 01:16

Great Idea! I just can’t seem to get it to work.
I have run out of ideas as to what to change.
Perhaps a kind soul could give me a real detailed blow by blow description of what to do. Please consider I do not know php at all. I am somewhat familiar with HTML from a past life.

May 19 2010 04:49

I want to have the adsense code appear in the middle of the post paragraph , so is it possible?

Jul 07 2010 04:08

I want ask: How to insert Ads in middle of articles?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required