Gallery2 AdSense: no module?
1st October 2006
If you ever need to add AdSense units to your Gallery2 installation, and find no module for this – try following the text below. I compiled it from several sources (primary menalto.com Gallery2 forum), and did everything written here myself here.
Primary source of information was this post, but I recommend that you follow instructions here.
To add AdSense to your Gallery2 sidebar (when using the Matrix theme as a sample):
- copy themes/matrix/templates/sidebar.tpl to theme/matrix/templates/local/sidebar.tpl on your server. This is needed to preserve your modifications when the Matrix theme will be upgraded.
- any further modifications should be done in your newly copied local/sidebar.tpl file.
- create the file which will contain all your AdSense code – e.g., let it be adsense_160x600.tpl for the Wide Scyscraper format. Put all your AdSense code into that file, and upload the file to your theme/matrix/templates/local/ folder on the server.
- now edit the sidebar.tpl file. My original file looked like this:[smarty]{* Show the sidebar blocks chosen for this theme *}
{foreach from=$theme.params.sidebarBlocks item=block}
{g->block type=$block.0 params=$block.1 class=”gbBlock”}
{/foreach}
{g->block type=”core.NavigationLinks” class=”gbBlock”}[/smarty] I added [smarty]{if $user.isGuest == 1}
{* adsense code *}
{g->theme include=”adsense_160x600.tpl” class=”gbBlock”}
{/if}[/smarty] and final file looked like this:[smarty]{*
* $Revision: 1.2 $
* If you want to customize this file, do not edit it directly since future upgrades
* may overwrite it. Instead, copy it into a new directory called “local” and edit that
* version. Gallery will look for that file first and use it if it exists.
*}{* Show the sidebar blocks chosen for this theme *}
{foreach from=$theme.params.sidebarBlocks item=block}
{g->block type=$block.0 params=$block.1 class=”gbBlock”}
{/foreach}
{g->block type=”core.NavigationLinks” class=”gbBlock”}
{if $user.isGuest == 1}
{* adsense code *}
{g->theme include=”adsense_160x600.tpl” class=”gbBlock”}
{/if}[/smarty]
- well, basically you are done. If you need/want to, you can wrap the inserted code into some styled container, to define the look of the block, like:[smarty]{if $user.isGuest == 1}
{* adsense code *}
{g->theme include=”adsense_160x600.tpl” class=”gbBlock”}
{/if}[/smarty]
- you can use any AdSense format you like, but you must mind the width of the sidebar in your theme – so that site layout is not broken by too wide AdSense units.
Finally, some more hints and explanations.
The smarty variables for users in Gallery2 are:
- $user.isGuest
- $user.isRegisteredUser
- $user.isAdmin
As you might have noticed, in the code above AdSense unit is shown ONLY IF the page visitor is a Guest. Thus ads will not be shown to Registered Users and Admin. I did not manage to find this condition in Google’s AdSense Policy, but the source post author states that showing Google Ads in password-protected areas is a violation of Google’s Terms Of Service (TOS). Again – I did not find that, but in good faith I provide the code which limits ads to guest-only users. If you want to show ads to any user class – just remove the first {if} and last {/if} lines from the code. Any knowledgeable comments on this (preferrably with URLs) are very welcome, as personally I see nothing bad in putting AdSense onto password-protected pages in the case of the Gallery, where photos are the main type of content.
To add multiple ad units – create a single file for each distinct ad format/type you want to use, and include it using the code shown above. Just keep in mind limitations imposed by Google on the number of ad units of different types on a single page.
Hope this helps, and good luck to you.
February 7th, 2009 at 20:04
thank you very much. Was quite useful. Initially I was trying to create a sideblock itself using modules. But this way is much better for simple sites. The mistake I did was that I did not use the right div tags in the sidebar.tpl so I the ads wouldnt load.
June 21st, 2009 at 21:54
Dobra instrukcja, sam szukałem metod i tylko udało mi się w Gallery2 wkleić kod AdSense w nagłówek strony. Ale może i jakaś wtyczka się pojawi, przecież ten skrypt jest dalej rozwijany.
June 22nd, 2009 at 9:03
Dzięki!
Przepraszamy za jakość tekstu – to automatycznie przetÅ‚umaczone. Pierwotny jÄ™zyk tego bloga jest angielski.
Proszę zobaczyć w wersji angielskiej dla prawidłowego kodu (wydaje się, że tłumaczenia automatycznego odszkodowania kodu).