Gosh aren’t we lucky today, more than one piece in a day of WordPress stupidity from yours truly! ;) This time however, it is in the form of a plugin and is primarily aimed at those wanting Google AdSense to be WordPress theme aware as they have a theme selector or some such, or are using content negotiation to serve up XHTML as application/xhtml+xml via my method.
For the latter, as you may well be (un-)aware, Google’s AdSense code does not play nicely in such situations so the current work-around is to use an <object> element, which then references an HTML file sent as text/html, and it is this document which contains all the fancy Google AdSense JavaScript.
Just download google-adsense.tar.gz and extract it. Edit google.html and put the relavant JavaScript variables for your account into it, save it and then put this in the directory of your current active theme, say ./wp-content/themes/your-theme/. Next, place the file google-adsense.php into ./wp-content/plugins/ and activate the plugin. To insert an AdSense block, the most basic template tag for it is:
<?php google_adsense(); ?>
In text/html mode (the plugin figures this out automagically), the ./wp-content/themes/your-theme/google.html file is read and just the important AdSense bit is output into your page, something resembling this in fact:
<script type="text/javascript"><!--
google_ad_client = "MY_CLIENT_ID";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
In application/xhtml+xml mode (anything other than text/html or text/plain to be precise), it will output an <object> which references to ./wp-content/themes/your-theme/google.html, something like so:
<object data="http://domain.tld/wp-content/themes/your-theme/google.html" type="text/html"></object>
Obviously, if you are using more than one theme, then the google.html template needs to be in those themes directories too! The template function also accepts a parameter, which appends a suffix to the template file it’s trying to use. A basic example would be:
<?php google_adsense('leaderboard'); ?>
… and:
<?php google_adsense('skyscraper'); ?>
Instead of looking for [THEME_PATH]/google.html, the above two examples will look for [THEME_PATH]/google-leaderboard.html and [THEME_PATH]/google-skyscraper.html respectively. What CSS to apply to your placed AdSense I’ll leave completely up to you as one man’s meat is another’s cabbage.
So that’s basically it, happy WordPress plugging-ining and Google AdSense (XHTML or otherwise) advert-serving! :D
Heh, another good WordPress tweaking “guide”.
Comment by MHobbit — 00:56:28 UTC on the 14th of May, 2005
Plugins aren’t tweaks in my book, though the way I had implimented this little script meant it was theme aware, which as far as I know hasn’t been done by any WordPressers… however they serve their XHTML.
The only downside is that the more I work with WordPress, the more I’m reminded of phpBB 1.x code though “with WordPress characteristics”. :P
Comment by Jonathan Stanley — 14:38:51 UTC on the 14th of May, 2005
Great thing! Took me some while to find you, but this is exactly what I was looking for
One problem though, it doesn’t work!
Wp insists on inserting BR / behind every line from google.html when displaying the resulting page, even though they’re not there in google.html.
Have you got a solution for this?
This is exactly the same problem I have when inserting the full adsense code into my pages, and I though your plugin would solve it. But not -yet- ;)
Comment by Chris — 10:55:00 UTC on the 9th of June, 2005
Hmmm, all I can say is “it works for me”…. however what you describe with it not working via the old hardcoding method sounds symptomatic of a web host that is mangling pages with some post processing outside of users control. I suggest you slap them with an email to their support and ask what the heck they think they are playing at. :P
Comment by Jonathan Stanley — 11:14:28 UTC on the 9th of June, 2005
*Slap slap slap - right in my own face* I host me myself ;)
Running PHP on IIS, and everything is working fine.
I have the same code hardcoded in the rightside template and that one is working fine. The problem only occurs on the posts and pages, so it’s somewhere inside WP.
Comment by Chris — 12:53:23 UTC on the 9th of June, 2005
If that’s the case, I can only guess that it’s some other plugin/hack you’ve got in WP that’s trampling over the page output. I can’t think of anything offhand that might be the cause, then again I’ve got very few plugins enabled, as compared to what appears to be “at least a few dozen” some WP users seem to have active. :P
Comment by Jonathan Stanley — 13:03:49 UTC on the 9th of June, 2005
I _knew_ you’d have something like this… thanks :) .
Comment by Tjaard — 19:44:30 UTC on the 18th of August, 2005
But of course! ;) Oh, and glad it’s of use to all the 3 of the “Draconian XHTML” WordPressers… :D
Comment by Jonathan Stanley — 14:58:18 UTC on the 19th of August, 2005
thanx for the plugin!
Comment by Riky Kurniawan — 14:52:49 UTC on the 20th of November, 2005