Shylock Adsense Plugin - Hack To Avoid Smart Pricing
January 24th, 2008 by Stephen Cronin (2,052 views)In my last post, I talked about Adsense Smart Pricing and how blogs can avoid it by only showing ads to search engine visitors.
One option for WordPress is the Who Sees Ads plugin, which has the ability to do this. I prefer the Shylock Adsense plugin for the reasons given in my last post: automatic placement of ads; no need to enter placeholder in each post; easy to move ads from, say, top right to top left, etc.
The problem with Shylock Adsense is that although it can show ads on old posts only (mostly search engine traffic), there is no option to show ads only to search engine traffic. I really wanted this functionality, so I added a few lines of code to the plugin to enable it. In this post I explain how to do it.
Hacking Shylock Adsense ONLY
If you are only using Shylock Adsense to show ads, then this section is for you. If you have Adsense in other places, such as the sidebar, skip this section and continue to the Hacking Shylock Adsense And Sidebars section.
First, edit the shylock_adsense.php file which comes with the plugin. Look for the following code (on line 325 in version 1.2):
function shylock_adsense_filter($content){
global $id,$user_level;
$output = $content;
and replace it with:
function scratch99_fromasearchengine(){
$ref = $_SERVER['HTTP_REFERER'];
$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
foreach ($SE as $source) {
if (strpos($ref,$source)!==false) return true;
}
return false;
}
function shylock_adsense_filter($content){
global $id,$user_level;
$output = $content;
if (scratch99_fromasearchengine()) {
Then go down to about line 364 and look for:
return $output;
and replace it with:
} return $output;
Upload the edited file to the wp-content/plugin directory on your server, overwriting the original file.
shylock_adsense.php file, replacing the one you’ve changed.Shylock Adsense will now only show ads to search engine visitors. Any existing limitations will still be in place, but there is no longer any need for them - if the plugin is set to show ads after a certain number of days, you should change this so they are shown immediately.
Hacking Shylock Adsense AND Sidebars
Note: If you only show ads using Shylock Adsense, the previous section is for you, NOT this one.
I display ads in my sidebar, as well as in the post body. There is little point in hacking Shylock Adsense so that ads only appear to search engine traffic in the post body, if regular visitors can see them in the sidebar! Therefore, I came up with a solution to restrict ads in both places.
If we are going to use it from multiple places, the best place for the function checking if visitors are from a search engine is not in the Shylock Adsense plugin. What if we stop using the plugin in future and disable it? Then the function will stop working in other places, such as the side bar. I decided to place this function in the functions.php file in my theme folder.
1. Functions.php
Go to the wp-content/theme/yourtheme folder (where yourtheme is the name of your theme) and make a copy of the functions.php file (in case things go wrong). Then edit the file, go to the very bottom and replace:
?>
with:
function scratch99_fromasearchengine(){
$ref = $_SERVER['HTTP_REFERER'];
$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
foreach ($SE as $source) {
if (strpos($ref,$source)!==false) return true;
}
return false;
} ?>
Upload the edited file to your theme directory on your server, overwriting the original file.
2. Shylock_adsense.php
Edit the shylock_adsense.php file which comes with the plugin. Look for the following code (on line 325 in version 1.2):
function shylock_adsense_filter($content){
global $id,$user_level;
$output = $content;
and replace it with:
function shylock_adsense_filter($content){
global $id,$user_level;
$output = $content;
if (function_exists('scratch99_fromasearchengine')) {
if (scratch99_fromasearchengine()) {
Then go down to about line 364 and look for:
return $output;
and replace it with:
} } return $output;
Upload the edited file to the wp-content/plugin directory on your server, overwriting the original file.
shylock_adsense.php file, replacing the one you’ve changed.3. Sidebar Widget
To do this, you need to have a widget with can execute php code. I use Otto’s ExecPHP plugin, but there are others available.
If you don’t have a plugin which can do this, download ExecPHP and install it. This will add a PHP Code 1 widget to the Presentation - Widgets page in the Admin area. Drag this widget to wherever you’d like the ads to appear, click on the configure icon and enter the following into the text box:
<?php if (function_exists('scratch99_fromasearchengine')) {
if (scratch99_fromasearchengine()) { ?>
INSERT YOUR ADSENSE CODE HERE
<?php } } ?>
replacing INSERT YOUR ADSENSE CODE HERE with your Adsense code.
Close the text box, Save Changes and you’ll now have ads which appear in the sidebar for search engine visitors only.
4. Other Places
Although I’ve done this in a sidebar widget, the same code should work equally well in sidebar.php - or for that matter, wherever you want to use it (maybe single.php or index.php).
Testing The Changes
This applies to both hacks above. You should no longer be able to see Adsense on your site when you visit it as you normally do. Now you need to visit your site via a search engine to see if the ads appear.
You could do this by searching for your site name or url, or you could search for a relatively obscure phrase from one of your posts, contained in quote marks (eg “I’ve concluded that I wasn’t already smart priced”).
Once you find one of your posts in the search results, click it, go to your site and see if ads appear. If they do, you’ve hacked the plugin successfully.
But Wait There’s More
While this post has been written from the perspective of serving Adsense ads to search visitors only, you could use this technique to show anything to search engine visitors - other ads, custom messages, you name it…
Final Thoughts
This hack should help you avoid being smart priced by Adsense. I hope you find it of use.
I’ll be contacting the author of Shylock Adsense to see if this functionality can be added to the plugin in future. In my view, this would be a logical extension to what is already a great plugin, especially as more people become aware of Adsense Smart Pricing.
Tags: adsense, blogging, hacks, widgets, wordpress plugins















Really great post. I will try this and post (comment) my results here. Thanks
well, this is a really nice post. thank you for tips.
That’s a great idea. I’m going to bookmark this page and come back to it after I’ve had my beauty sleep. I know that adsense still works, it just depends on the visitor you are attracting.
April
Pain and Jan, thanks for stopping by.
April, Thanks. Yes it definitely depends on the type of visitors you get. There are quite a few different factors to being successful with Adsense, this is just one of them. By the way, you’ve got some really nice photos on your site - I love Scotland.
An excellent hack, Stephen. I hacked it once myself, but for different reasons. If I switch back to using this plugin, instead of WSA, I’ll definitely be using this hack.
It definitely sounds good…I’m not very “techy” though but I will give it a shot. Thanks
RT, Thanks. Sorry about the delay in responding. I live on a university campus. It has become a ghost town because everyone’s gone home for the Spring Festival - even my ISP! No access at home now and limited access at work.
I remember your post on hacking Shylock Adsense. In fact, if you google “Shylock Hack” (without the quote marks), your post is number 1 and my post number 3 (with BlogCatalog page referencing my post at number 2). Interestingly, you’re number 1 for “Shylock Adsense hack”, but I’ve slipped to number 4 and it’s my introduction post, not this post. Weird!
PokerStars, I hope it works for you…
Actually, I wasn’t expecting an answer.
Ah well, sorry to disappoint, but I try to answer all comments if I can!
BTW, Stephen, you need to update your copyright statement (in the footer).
Hey Stephen,
After having this in my to-do list, I finally took the plunge and implemented in both our blogs so that we display Adsense only to search engine traffic.
Our CTR before this was anywhere between 1% - 4%. Today, in ShanKri-la the CTR has gone up atleast 4 times including the value of clicks. I didn’t expect to see such a difference the very next day. I don’t think it’s a coincidence but it’ll be interesting to see how much of a difference it makes overall this week. I haven’t had a click yet in spicytasty.com yet, so I can’t comment on that right now.
Either way, I wanted to thank you for this awesome hack as I prefer Shylock Adsense over the others too.
On another note, is there a way to use your iFrame widget plugin besides the sidebar widgets, for example using your function in theme template elsewhere.
Cheers!
RT, Thanks - I’ve fixed it up now.
Hi K, wow - that is a quick improvement. I’d be interested to hear if that’s maintained. Let me know how it goes!
As for the IFrameWidgets plugin, there’s no way to use it outside of the sidebar at the moment, but I’m planning on creating a solution in future. Just when I’ll get to it is another matter - I still haven’t rebadged the current version as v1.0 and ‘released it’ (ie added it to the various lists of plugins). I’m guessing it will be about a month’s time (after Chinese New Year is out of the way).
[…] Cronin of More Than Scratch the Surface recently completed a Shylock AdSense plugin hack to avoid smart pricing, a hack that's designed to target only search engine visitors. He ripped the relevant code […]
I better bookmark this page, so when the time comes and start running the ads I do it the right way …
[…] visitors. There are now at least two WordPress plugins that can do exactly that: Who Sees Ads? and Shylock AdSense […]
[…] ads only to search engine visitors and still use the Shylock Adsense plugin to insert the ads, check out this hack from Stephen Cronin that lets you do that. It’s a great idea and it will also be an option in […]
thanks for the hacks dude!
Fahmishah, No worries! Hope it helps…
[…] anything else, sorry, I can’t give you an alternative), but not the original one: check out this hacked version of Shylock AdSense, which makes your ads to be displayed only to people coming to your website from search engines. […]
This is a great and simple modification to make to your blog, from the testing I’ve done from various search engines and other sources, it displays just as it should - I’m interested to see how that affects my CTRs and income.
Thanks for a really cool addition to my website, much appreciated!
Zath, Thanks… I hope it helps with CTR and income - if you have time, I’d love to hear how it goes…
Thanks very much for this Stephen, I can confirm that after 2 weeks of testing this out, that it really does help with increasing CTR and the CPC seems to be more consistent.
I have just one suggestion to improve this - is there anyway that ads can be shown to search engine visitors beyond the first page that they view? I’m guessing that would involve too much tracking as opposed to just looking at the origin of a visitor?
Thanks for a great piece of coding and I’ll probably put it on my other sites too following this experiment.
[…] una piccola modifica, Scratch99 ha dotato del precedente plugin di una piccola, ma importante […]
[…] Shylock Adsense Plugin - Hack To Avoid Adsense Smart Pricing - Stephen Cronin put together this great tutorial for Wordpress users who make use of the Shylock plugin for inserting their adsense ads. The goal is to show ads only to visitors from search engines thus allowing you to avoid smart pricing from Google Adsense. I’ve been using his tips for a little while now and I’m beginning to see positive results. […]
Thanks a lot! I was searching this modification but didn’t found it! Now I can modify the plugin and use it!
Hey Stephen!
Wow, what a post you have here! I have a few blogs that use Adsense, so I’ll be sure to give this one a try.
Stumbled!
[…] on -If one of your posts gets on Diggs or other social media, remove ads for a few days -Use Shylock Adsense plugin. This is another version that allows you to show ads only to visitors who come from search engines. […]