Tuesday, December 2, 2008

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:

if (scratch99_fromasearchengine()) { ?>
INSERT YOUR ADSENSE CODE HERE

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.

No comments:

Post a Comment