
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.

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
Could you also post a recipe for not showing ads on specific wordpress pages…
Cheers
Vaibhav
@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!
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!
This is so AWESOME! What a handy dandy shortcut! It makes your post SO much cleaner as well!!! THANK YOU!!!
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.
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
@Agolf Cartson: Yes, I’ll write a recipe about enhancing this one soon.
Thank you very much. That would really be great. Being able to add adsense using [adsense] would make things so much easier
How about the styling?
@levin: The same question has been already asked by Agolf, I’m going to write a recipe about it soon
That’s pretty smart, but I don’t like to add ads manually and to lazy to make a script:P
Just Perfect solutions
Thanks for the recipe! This tool is very nice and of course articles on this page are nice, too.
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()?
Thanks for this recipe!
It’s a lot easier to do than I thought, and without using a plugin!
Thanks good solutions
really nice solution , thank you
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?
If I put, in single.php, the code below, will it work?
<?php adsense(); ?>[]‘s!
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!
)
Excellent tip. Just exactly what I need right now. Couldn’t be much easier, could it?
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?
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.
Thx, this is a very smale way, thx for this solution.
Very useful. Earlier I used to edit each template. Now I just need to edit the functions.php
How to rotate the Ads inside the post, one like Adsense injection plugin???
Thank you for your tip.I will be thank ful to you for it every time since it increased my earnings.
Thanking you agian.
Great and very simple piece of code. I’ve just added the code to my website.
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.
I want to have the adsense code appear in the middle of the post paragraph , so is it possible?
I want ask: How to insert Ads in middle of articles?
Trackbacks: