Shylock Adsense Plugin – Hack To Avoid Smart Pricing

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.

Note: If you make these changes, you will lose some clicks and a lot of impressions. If you are currently smart priced, these changes should help fix that (after about a week), so you are likely to earn more. If you are not smart priced, you may actually lose money from these changes, although it safeguards you from the danger of being smart priced in future. Read my last post about Adsense Smart Pricing and blogs for a better understanding of the issues here. For my site, I’ve concluded that I wasn’t already smart priced. I do not seem to be losing any clicks, but your site may be different.

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.

If you experience any problems, simply upload an unmodified version of the 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.

Note: The scratch99_fromasearchengine function is based on the ‘only show search engine’ functionality from the Who Sees Ads plugin.

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.

Note, this is not perfect, as the functionality will be lost if the theme is changed.

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.

If you experience any problems, simply upload an unmodified version of the 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.

40 responses on “Shylock Adsense Plugin – Hack To Avoid Smart Pricing

  1. April - Of Scotland

    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

  2. Stephen Cronin Post author

    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.

  3. RT Cunningham

    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.

  4. Stephen Cronin Post author

    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…

  5. K-IntheHouse

    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!

  6. Stephen Cronin Post author

    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).

  7. Zath

    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!

    1. Stephen Cronin Post author

      Zath, Thanks… I hope it helps with CTR and income – if you have time, I’d love to hear how it goes…

      1. Zath

        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.

  8. Mark

    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!

  9. Rhys@ WEBlog

    Hey Stephen – You are a genius: This is real magic and just what I was searching for, a method of detecting Search Engine clients. I married two of your bits of code above, and then I “include” this code as inc_adsense.php anywhere I like and Viola, Google ads appear or hide as required.

    See a working in the sidebar top here weblog.biznz.net – and all much easier than installing the plugin – it only works on WordPress anyway.

  10. Shaun @ Make Money

    Personally, I prefer to edit the code myself to optimize the adsense placings. Top left part of the post gets the highest CTR, so that’s where I usually put it. Do you get higher CTRs when you manage it your way?

  11. chi@Chrome Browser

    Good post. I do have one wordpress blog I’m developing and I do expect tons of repeat visitors to the forum. Wish they had the same plugin for vbulletin or phpbb forums. I think sites with forums get the most repeat visitors and could really get you smart priced in a hurry. Anyhow thanks for the hack.

  12. Tom@Make Money Online

    Hi Stephen,

    Does the Adsense preview in the plugin count as an impression. If so, how do we modify the plugin to remove the preview?

    By the way, there’s a small change in the code for the current plugin. Instead of “shylock_adsense_filter” it should be “whydowork_adsense_filter”

    Thanks!

  13. jeff@High yield certificate of deposit

    The shylock hack is a good one. Thanks for figuring it out it saves everyone alot of work. This should help people earn more from adsense.

  14. Bailey@Rakeback

    This is a great! From the testing I’ve done with various search engines 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!

  15. Expert@Make Easy Money On Internet

    I’m using adsense to make money on my blog, but I’m also trying my luck in affiliate marketing, it seems though it really works out well.

  16. jaydee@wedding photographer sussex

    Great instructions Stephen. Any idea if Shylock updated his plugin? If not maybe its time for you to create an alternative that does this out of the box?

  17. david@wpsitelaunch.com

    Good question, since it’s been roughly a year, what’s googles take on this plugin? and are there any updates yet?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Quiz: